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

Simplify/improve casting across library #27

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import 'package:sane/src/isolate_messages/interface.dart';
import 'package:sane/src/sane.dart';
import 'package:sane/src/structures.dart';

class ControlButtonOptionMessage implements IsolateMessage {
class ControlButtonOptionMessage
implements IsolateMessage<ControlButtonOptionResponse> {
ControlButtonOptionMessage({
required this.saneHandle,
required this.index,
Expand Down
13 changes: 7 additions & 6 deletions packages/sane/lib/src/isolate_messages/control_option.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import 'package:sane/src/isolate_messages/interface.dart';
import 'package:sane/src/sane.dart';
import 'package:sane/src/structures.dart';

class ControlValueOptionMessage<T> implements IsolateMessage {
class ControlValueOptionMessage<T>
implements IsolateMessage<ControlValueOptionResponse<T>> {
ControlValueOptionMessage({
required this.saneHandle,
required this.index,
Expand All @@ -16,7 +17,7 @@ class ControlValueOptionMessage<T> implements IsolateMessage {
final T? value;

@override
Future<ControlValueOptionResponse> handle(Sane sane) async {
Future<ControlValueOptionResponse<T>> handle(Sane sane) async {
switch (value) {
case final bool value:
return ControlValueOptionResponse<bool>(
Expand All @@ -26,7 +27,7 @@ class ControlValueOptionMessage<T> implements IsolateMessage {
action: action,
value: value,
),
);
) as ControlValueOptionResponse<T>;
case final int value:
return ControlValueOptionResponse<int>(
result: await sane.controlIntOption(
Expand All @@ -35,7 +36,7 @@ class ControlValueOptionMessage<T> implements IsolateMessage {
action: action,
value: value,
),
);
) as ControlValueOptionResponse<T>;
case final double value:
return ControlValueOptionResponse<double>(
result: await sane.controlFixedOption(
Expand All @@ -44,7 +45,7 @@ class ControlValueOptionMessage<T> implements IsolateMessage {
action: action,
value: value,
),
);
) as ControlValueOptionResponse<T>;
case final String value:
return ControlValueOptionResponse<String>(
result: await sane.controlStringOption(
Expand All @@ -53,7 +54,7 @@ class ControlValueOptionMessage<T> implements IsolateMessage {
action: action,
value: value,
),
);
) as ControlValueOptionResponse<T>;
default:
throw Exception('Invalid value type.');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import 'package:sane/src/isolate_messages/interface.dart';
import 'package:sane/src/sane.dart';
import 'package:sane/src/structures.dart';

class GetAllOptionDescriptorsMessage implements IsolateMessage {
class GetAllOptionDescriptorsMessage
implements IsolateMessage<GetAllOptionDescriptorsResponse> {
GetAllOptionDescriptorsMessage({required this.saneHandle});

final SaneHandle saneHandle;
Expand Down
2 changes: 1 addition & 1 deletion packages/sane/lib/src/isolate_messages/get_devices.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:sane/src/isolate_messages/interface.dart';
import 'package:sane/src/sane.dart';
import 'package:sane/src/structures.dart';

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

final bool localOnly;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import 'package:sane/src/isolate_messages/interface.dart';
import 'package:sane/src/sane.dart';
import 'package:sane/src/structures.dart';

class GetOptionDescriptorMessage implements IsolateMessage {
class GetOptionDescriptorMessage
implements IsolateMessage<GetOptionDescriptorResponse> {
GetOptionDescriptorMessage({
required this.saneHandle,
required this.index,
Expand Down
2 changes: 1 addition & 1 deletion packages/sane/lib/src/isolate_messages/get_parameters.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:sane/src/isolate_messages/interface.dart';
import 'package:sane/src/sane.dart';
import 'package:sane/src/structures.dart';

class GetParametersMessage implements IsolateMessage {
class GetParametersMessage implements IsolateMessage<GetParametersResponse> {
GetParametersMessage({required this.saneHandle});

final SaneHandle saneHandle;
Expand Down
2 changes: 1 addition & 1 deletion packages/sane/lib/src/isolate_messages/init.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:sane/src/isolate_messages/interface.dart';
import 'package:sane/src/sane.dart';

class InitMessage implements IsolateMessage {
class InitMessage implements IsolateMessage<InitResponse> {
@override
Future<InitResponse> handle(Sane sane) async {
return InitResponse(
Expand Down
4 changes: 2 additions & 2 deletions packages/sane/lib/src/isolate_messages/interface.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:sane/src/sane.dart';

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

abstract interface class IsolateResponse {}
2 changes: 1 addition & 1 deletion packages/sane/lib/src/isolate_messages/open.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:sane/src/isolate_messages/interface.dart';
import 'package:sane/src/sane.dart';
import 'package:sane/src/structures.dart';

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

final String deviceName;
Expand Down
2 changes: 1 addition & 1 deletion packages/sane/lib/src/isolate_messages/read.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:sane/src/isolate_messages/interface.dart';
import 'package:sane/src/sane.dart';
import 'package:sane/src/structures.dart';

class ReadMessage implements IsolateMessage {
class ReadMessage implements IsolateMessage<ReadResponse> {
ReadMessage({
required this.saneHandle,
required this.bufferSize,
Expand Down
69 changes: 29 additions & 40 deletions packages/sane/lib/src/sane_isolate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ class SaneIsolate implements Sane {
_isolate.kill(priority: Isolate.immediate);
}

Future<dynamic> _sendMessage(IsolateMessage message) async {
Future<T> _sendMessage<T extends IsolateResponse>(
IsolateMessage<T> message,
) async {
final replyPort = ReceivePort();

_sendPort.send(
_IsolateMessageEnveloppe(
_IsolateMessageEnvelope(
replyPort: replyPort.sendPort,
message: message,
),
Expand All @@ -65,14 +67,14 @@ class SaneIsolate implements Sane {
);
}

return response;
return response as T;
}

@override
Future<int> init({
AuthCallback? authCallback,
}) async {
final response = await _sendMessage(InitMessage()) as InitResponse;
final response = await _sendMessage(InitMessage());
return response.versionCode;
}

Expand All @@ -86,21 +88,17 @@ class SaneIsolate implements Sane {
required bool localOnly,
}) async {
final response = await _sendMessage(
GetDevicesMessage(
localOnly: localOnly,
),
) as GetDevicesResponse;
GetDevicesMessage(localOnly: localOnly),
);

return response.devices;
}

@override
Future<SaneHandle> open(String deviceName) async {
final response = await _sendMessage(
OpenMessage(
deviceName: deviceName,
),
) as OpenResponse;
OpenMessage(deviceName: deviceName),
);

return response.handle;
}
Expand All @@ -113,9 +111,7 @@ class SaneIsolate implements Sane {
@override
Future<void> close(SaneHandle handle) async {
await _sendMessage(
CloseMessage(
saneHandle: handle,
),
CloseMessage(saneHandle: handle),
);
}

Expand All @@ -129,7 +125,7 @@ class SaneIsolate implements Sane {
saneHandle: handle,
index: index,
),
) as GetOptionDescriptorResponse;
);

return response.optionDescriptor;
}
Expand All @@ -139,10 +135,8 @@ class SaneIsolate implements Sane {
SaneHandle handle,
) async {
final response = await _sendMessage(
GetAllOptionDescriptorsMessage(
saneHandle: handle,
),
) as GetAllOptionDescriptorsResponse;
GetAllOptionDescriptorsMessage(saneHandle: handle),
);

return response.optionDescriptors;
}
Expand All @@ -161,7 +155,7 @@ class SaneIsolate implements Sane {
action: action,
value: value,
),
) as ControlValueOptionResponse<bool>;
);

return response.result;
}
Expand All @@ -180,7 +174,7 @@ class SaneIsolate implements Sane {
action: action,
value: value,
),
) as ControlValueOptionResponse<int>;
);

return response.result;
}
Expand All @@ -199,7 +193,7 @@ class SaneIsolate implements Sane {
action: action,
value: value,
),
) as ControlValueOptionResponse<double>;
);

return response.result;
}
Expand All @@ -218,7 +212,7 @@ class SaneIsolate implements Sane {
action: action,
value: value,
),
) as ControlValueOptionResponse<String>;
);

return response.result;
}
Expand All @@ -233,7 +227,7 @@ class SaneIsolate implements Sane {
saneHandle: handle,
index: index,
),
) as ControlButtonOptionResponse;
);

return response.result;
}
Expand All @@ -244,17 +238,15 @@ class SaneIsolate implements Sane {
GetParametersMessage(
saneHandle: handle,
),
) as GetParametersResponse;
);

return response.parameters;
}

@override
Future<void> start(SaneHandle handle) async {
await _sendMessage(
StartMessage(
saneHandle: handle,
),
StartMessage(saneHandle: handle),
);
}

Expand All @@ -265,17 +257,15 @@ class SaneIsolate implements Sane {
saneHandle: handle,
bufferSize: bufferSize,
),
) as ReadResponse;
);

return response.bytes;
}

@override
Future<void> cancel(SaneHandle handle) async {
await _sendMessage(
CancelMessage(
saneHandle: handle,
),
CancelMessage(saneHandle: handle),
);
}

Expand Down Expand Up @@ -305,25 +295,24 @@ void _isolateEntryPoint(_IsolateEntryPointArgs args) {
args.mainSendPort.send(isolateReceivePort.sendPort);

final sane = args.sane;
isolateReceivePort.listen((envellope) async {
envellope = envellope as _IsolateMessageEnveloppe;

isolateReceivePort.cast<_IsolateMessageEnvelope>().listen((envelope) async {
late IsolateResponse response;

try {
response = await envellope.message.handle(sane);
response = await envelope.message.handle(sane);
} on SaneException catch (exception, stackTrace) {
response = ExceptionResponse(
exception: exception,
stackTrace: stackTrace,
);
}

envellope.replyPort.send(response);
envelope.replyPort.send(response);
});
}

class _IsolateMessageEnveloppe {
_IsolateMessageEnveloppe({
class _IsolateMessageEnvelope {
_IsolateMessageEnvelope({
required this.replyPort,
required this.message,
});
Expand Down