Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Isolate message self handle #14

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions packages/sane/lib/src/isolate_messages/cancel.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import 'package:sane/src/isolate_messages/interface.dart';
import 'package:sane/src/sane.dart';
import 'package:sane/src/structures.dart';

class CancelMessage implements IsolateMessage {
CancelMessage({required this.handle});
CancelMessage({required this.saneHandle});

final SaneHandle handle;
final SaneHandle saneHandle;

@override
Future<CancelResponse> handle(Sane sane) async {
await sane.cancel(saneHandle);
return CancelResponse();
}
}

class CancelResponse implements IsolateResponse {}
12 changes: 9 additions & 3 deletions packages/sane/lib/src/isolate_messages/close.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import 'package:sane/sane.dart';
import 'package:sane/src/isolate_messages/interface.dart';
import 'package:sane/src/structures.dart';

class CloseMessage implements IsolateMessage {
CloseMessage({required this.handle});
CloseMessage({required this.saneHandle});

final SaneHandle handle;
final SaneHandle saneHandle;

@override
Future<CloseResponse> handle(Sane sane) async {
await sane.close(saneHandle);
return CloseResponse();
}
}

class CloseResponse implements IsolateResponse {}
15 changes: 13 additions & 2 deletions packages/sane/lib/src/isolate_messages/control_button_option.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import 'package:sane/src/isolate_messages/interface.dart';
import 'package:sane/src/sane.dart';
import 'package:sane/src/structures.dart';

class ControlButtonOptionMessage implements IsolateMessage {
ControlButtonOptionMessage({
required this.handle,
required this.saneHandle,
required this.index,
});

final SaneHandle handle;
final SaneHandle saneHandle;
final int index;

@override
Future<ControlButtonOptionResponse> handle(Sane sane) async {
return ControlButtonOptionResponse(
result: await sane.controlButtonOption(
handle: saneHandle,
index: index,
),
);
}
}

class ControlButtonOptionResponse implements IsolateResponse {
Expand Down
57 changes: 51 additions & 6 deletions packages/sane/lib/src/isolate_messages/control_option.dart
Original file line number Diff line number Diff line change
@@ -1,22 +1,67 @@
import 'package:sane/src/isolate_messages/interface.dart';
import 'package:sane/src/sane.dart';
import 'package:sane/src/structures.dart';

class ControlOptionMessage<T> implements IsolateMessage {
ControlOptionMessage({
required this.handle,
class ControlValueOptionMessage<T> implements IsolateMessage {
ControlValueOptionMessage({
required this.saneHandle,
required this.index,
required this.action,
this.value,
});

final SaneHandle handle;
final SaneHandle saneHandle;
final int index;
final SaneAction action;
final T? value;

@override
Future<ControlValueOptionResponse> handle(Sane sane) async {
switch (value) {
case final bool value:
return ControlValueOptionResponse<bool>(
result: await sane.controlBoolOption(
handle: saneHandle,
index: index,
action: action,
value: value,
),
);
case final int value:
return ControlValueOptionResponse<int>(
result: await sane.controlIntOption(
handle: saneHandle,
index: index,
action: action,
value: value,
),
);
case final double value:
return ControlValueOptionResponse<double>(
result: await sane.controlFixedOption(
handle: saneHandle,
index: index,
action: action,
value: value,
),
);
case final String value:
return ControlValueOptionResponse<String>(
result: await sane.controlStringOption(
handle: saneHandle,
index: index,
action: action,
value: value,
),
);
default:
throw Exception('Invalid value type.');
}
}
}

class ControlOptionResponse<T> implements IsolateResponse {
ControlOptionResponse({required this.result});
class ControlValueOptionResponse<T> implements IsolateResponse {
ControlValueOptionResponse({required this.result});

final SaneOptionResult<T> result;
}
9 changes: 8 additions & 1 deletion packages/sane/lib/src/isolate_messages/exit.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import 'package:sane/src/isolate_messages/interface.dart';
import 'package:sane/src/sane.dart';

class ExitMessage implements IsolateMessage {}
class ExitMessage implements IsolateMessage {
@override
Future<ExitResponse> handle(Sane sane) async {
await sane.exit();
return ExitResponse();
}
}

class ExitResponse implements IsolateResponse {}
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import 'package:sane/src/isolate_messages/interface.dart';
import 'package:sane/src/sane.dart';
import 'package:sane/src/structures.dart';

class GetAllOptionDescriptorsMessage implements IsolateMessage {
GetAllOptionDescriptorsMessage({required this.handle});
GetAllOptionDescriptorsMessage({required this.saneHandle});

final SaneHandle handle;
final SaneHandle saneHandle;

@override
Future<GetAllOptionDescriptorsResponse> handle(Sane sane) async {
return GetAllOptionDescriptorsResponse(
optionDescriptors: await sane.getAllOptionDescriptors(saneHandle),
);
}
}

class GetAllOptionDescriptorsResponse implements IsolateResponse {
Expand Down
8 changes: 8 additions & 0 deletions packages/sane/lib/src/isolate_messages/get_devices.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import 'package:sane/src/isolate_messages/interface.dart';
import 'package:sane/src/sane.dart';
import 'package:sane/src/structures.dart';

class GetDevicesMessage implements IsolateMessage {
GetDevicesMessage({required this.localOnly});

final bool localOnly;

@override
Future<GetDevicesResponse> handle(Sane sane) async {
return GetDevicesResponse(
devices: await sane.getDevices(localOnly: localOnly),
);
}
}

class GetDevicesResponse implements IsolateResponse {
Expand Down
12 changes: 10 additions & 2 deletions packages/sane/lib/src/isolate_messages/get_option_descriptor.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import 'package:sane/src/isolate_messages/interface.dart';
import 'package:sane/src/sane.dart';
import 'package:sane/src/structures.dart';

class GetOptionDescriptorMessage implements IsolateMessage {
GetOptionDescriptorMessage({
required this.handle,
required this.saneHandle,
required this.index,
});

final SaneHandle handle;
final SaneHandle saneHandle;
final int index;

@override
Future<GetOptionDescriptorResponse> handle(Sane sane) async {
return GetOptionDescriptorResponse(
optionDescriptor: await sane.getOptionDescriptor(saneHandle, index),
);
}
}

class GetOptionDescriptorResponse implements IsolateResponse {
Expand Down
12 changes: 10 additions & 2 deletions packages/sane/lib/src/isolate_messages/get_parameters.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import 'package:sane/src/isolate_messages/interface.dart';
import 'package:sane/src/sane.dart';
import 'package:sane/src/structures.dart';

class GetParametersMessage implements IsolateMessage {
GetParametersMessage({required this.handle});
GetParametersMessage({required this.saneHandle});

final SaneHandle handle;
final SaneHandle saneHandle;

@override
Future<GetParametersResponse> handle(Sane sane) async {
return GetParametersResponse(
parameters: await sane.getParameters(saneHandle),
);
}
}

class GetParametersResponse implements IsolateResponse {
Expand Down
10 changes: 9 additions & 1 deletion packages/sane/lib/src/isolate_messages/init.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import 'package:sane/src/isolate_messages/interface.dart';
import 'package:sane/src/sane.dart';

class InitMessage implements IsolateMessage {}
class InitMessage implements IsolateMessage {
@override
Future<InitResponse> handle(Sane sane) async {
return InitResponse(
versionCode: await sane.init(),
);
}
}

class InitResponse implements IsolateResponse {
InitResponse({required this.versionCode});
Expand Down
8 changes: 6 additions & 2 deletions packages/sane/lib/src/isolate_messages/interface.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
interface class IsolateMessage {}
import 'package:sane/src/sane.dart';

interface class IsolateResponse {}
abstract interface class IsolateMessage {
Future<IsolateResponse> handle(Sane sane);
}

abstract interface class IsolateResponse {}
8 changes: 8 additions & 0 deletions packages/sane/lib/src/isolate_messages/open.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import 'package:sane/src/isolate_messages/interface.dart';
import 'package:sane/src/sane.dart';
import 'package:sane/src/structures.dart';

class OpenMessage implements IsolateMessage {
OpenMessage({required this.deviceName});

final String deviceName;

@override
Future<OpenResponse> handle(Sane sane) async {
return OpenResponse(
handle: await sane.open(deviceName),
);
}
}

class OpenResponse implements IsolateResponse {
Expand Down
12 changes: 10 additions & 2 deletions packages/sane/lib/src/isolate_messages/read.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import 'dart:typed_data';

import 'package:sane/src/isolate_messages/interface.dart';
import 'package:sane/src/sane.dart';
import 'package:sane/src/structures.dart';

class ReadMessage implements IsolateMessage {
ReadMessage({
required this.handle,
required this.saneHandle,
required this.bufferSize,
});

final SaneHandle handle;
final SaneHandle saneHandle;
final int bufferSize;

@override
Future<ReadResponse> handle(Sane sane) async {
return ReadResponse(
bytes: await sane.read(saneHandle, bufferSize),
);
}
}

class ReadResponse implements IsolateResponse {
Expand Down
11 changes: 9 additions & 2 deletions packages/sane/lib/src/isolate_messages/set_io_mode.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import 'package:sane/src/isolate_messages/interface.dart';
import 'package:sane/src/sane.dart';
import 'package:sane/src/structures.dart';

class SetIOModeMessage implements IsolateMessage {
SetIOModeMessage({
required this.handle,
required this.saneHandle,
required this.ioMode,
});

final SaneHandle handle;
final SaneHandle saneHandle;
final SaneIOMode ioMode;

@override
Future<SetIOModeResponse> handle(Sane sane) async {
await sane.setIOMode(saneHandle, ioMode);
return SetIOModeResponse();
}
}

class SetIOModeResponse implements IsolateResponse {}
11 changes: 9 additions & 2 deletions packages/sane/lib/src/isolate_messages/start.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import 'package:sane/src/isolate_messages/interface.dart';
import 'package:sane/src/sane.dart';
import 'package:sane/src/structures.dart';

class StartMessage implements IsolateMessage {
StartMessage({required this.handle});
StartMessage({required this.saneHandle});

final SaneHandle handle;
final SaneHandle saneHandle;

@override
Future<StartResponse> handle(Sane sane) async {
await sane.start(saneHandle);
return StartResponse();
}
}

class StartResponse implements IsolateResponse {}
Loading