From 5d9bd9d90f15c21911d47867e9ca25ae916f9b34 Mon Sep 17 00:00:00 2001 From: Craftplacer <22963120+Craftplacer@users.noreply.github.com> Date: Thu, 7 Nov 2024 00:47:23 +0100 Subject: [PATCH] Simplify/improve casting across library --- .../control_button_option.dart | 3 +- .../src/isolate_messages/control_option.dart | 13 ++-- .../get_all_option_descriptors.dart | 3 +- .../lib/src/isolate_messages/get_devices.dart | 2 +- .../get_option_descriptor.dart | 3 +- .../src/isolate_messages/get_parameters.dart | 2 +- .../sane/lib/src/isolate_messages/init.dart | 2 +- .../lib/src/isolate_messages/interface.dart | 4 +- .../sane/lib/src/isolate_messages/open.dart | 2 +- .../sane/lib/src/isolate_messages/read.dart | 2 +- packages/sane/lib/src/sane_isolate.dart | 69 ++++++++----------- 11 files changed, 49 insertions(+), 56 deletions(-) diff --git a/packages/sane/lib/src/isolate_messages/control_button_option.dart b/packages/sane/lib/src/isolate_messages/control_button_option.dart index 8bff2f2..8176f48 100644 --- a/packages/sane/lib/src/isolate_messages/control_button_option.dart +++ b/packages/sane/lib/src/isolate_messages/control_button_option.dart @@ -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 { ControlButtonOptionMessage({ required this.saneHandle, required this.index, diff --git a/packages/sane/lib/src/isolate_messages/control_option.dart b/packages/sane/lib/src/isolate_messages/control_option.dart index 5b33f39..99f6942 100644 --- a/packages/sane/lib/src/isolate_messages/control_option.dart +++ b/packages/sane/lib/src/isolate_messages/control_option.dart @@ -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 implements IsolateMessage { +class ControlValueOptionMessage + implements IsolateMessage> { ControlValueOptionMessage({ required this.saneHandle, required this.index, @@ -16,7 +17,7 @@ class ControlValueOptionMessage implements IsolateMessage { final T? value; @override - Future handle(Sane sane) async { + Future> handle(Sane sane) async { switch (value) { case final bool value: return ControlValueOptionResponse( @@ -26,7 +27,7 @@ class ControlValueOptionMessage implements IsolateMessage { action: action, value: value, ), - ); + ) as ControlValueOptionResponse; case final int value: return ControlValueOptionResponse( result: await sane.controlIntOption( @@ -35,7 +36,7 @@ class ControlValueOptionMessage implements IsolateMessage { action: action, value: value, ), - ); + ) as ControlValueOptionResponse; case final double value: return ControlValueOptionResponse( result: await sane.controlFixedOption( @@ -44,7 +45,7 @@ class ControlValueOptionMessage implements IsolateMessage { action: action, value: value, ), - ); + ) as ControlValueOptionResponse; case final String value: return ControlValueOptionResponse( result: await sane.controlStringOption( @@ -53,7 +54,7 @@ class ControlValueOptionMessage implements IsolateMessage { action: action, value: value, ), - ); + ) as ControlValueOptionResponse; default: throw Exception('Invalid value type.'); } diff --git a/packages/sane/lib/src/isolate_messages/get_all_option_descriptors.dart b/packages/sane/lib/src/isolate_messages/get_all_option_descriptors.dart index b1da559..8412f13 100644 --- a/packages/sane/lib/src/isolate_messages/get_all_option_descriptors.dart +++ b/packages/sane/lib/src/isolate_messages/get_all_option_descriptors.dart @@ -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 { GetAllOptionDescriptorsMessage({required this.saneHandle}); final SaneHandle saneHandle; diff --git a/packages/sane/lib/src/isolate_messages/get_devices.dart b/packages/sane/lib/src/isolate_messages/get_devices.dart index 5faab61..d54bf17 100644 --- a/packages/sane/lib/src/isolate_messages/get_devices.dart +++ b/packages/sane/lib/src/isolate_messages/get_devices.dart @@ -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 { GetDevicesMessage({required this.localOnly}); final bool localOnly; diff --git a/packages/sane/lib/src/isolate_messages/get_option_descriptor.dart b/packages/sane/lib/src/isolate_messages/get_option_descriptor.dart index 09a639b..ea02661 100644 --- a/packages/sane/lib/src/isolate_messages/get_option_descriptor.dart +++ b/packages/sane/lib/src/isolate_messages/get_option_descriptor.dart @@ -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 { GetOptionDescriptorMessage({ required this.saneHandle, required this.index, diff --git a/packages/sane/lib/src/isolate_messages/get_parameters.dart b/packages/sane/lib/src/isolate_messages/get_parameters.dart index 93cf5c9..f152386 100644 --- a/packages/sane/lib/src/isolate_messages/get_parameters.dart +++ b/packages/sane/lib/src/isolate_messages/get_parameters.dart @@ -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 { GetParametersMessage({required this.saneHandle}); final SaneHandle saneHandle; diff --git a/packages/sane/lib/src/isolate_messages/init.dart b/packages/sane/lib/src/isolate_messages/init.dart index e591045..df035db 100644 --- a/packages/sane/lib/src/isolate_messages/init.dart +++ b/packages/sane/lib/src/isolate_messages/init.dart @@ -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 { @override Future handle(Sane sane) async { return InitResponse( diff --git a/packages/sane/lib/src/isolate_messages/interface.dart b/packages/sane/lib/src/isolate_messages/interface.dart index 24740d9..2515622 100644 --- a/packages/sane/lib/src/isolate_messages/interface.dart +++ b/packages/sane/lib/src/isolate_messages/interface.dart @@ -1,7 +1,7 @@ import 'package:sane/src/sane.dart'; -abstract interface class IsolateMessage { - Future handle(Sane sane); +abstract interface class IsolateMessage { + Future handle(Sane sane); } abstract interface class IsolateResponse {} diff --git a/packages/sane/lib/src/isolate_messages/open.dart b/packages/sane/lib/src/isolate_messages/open.dart index dca171a..13ca782 100644 --- a/packages/sane/lib/src/isolate_messages/open.dart +++ b/packages/sane/lib/src/isolate_messages/open.dart @@ -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 { OpenMessage({required this.deviceName}); final String deviceName; diff --git a/packages/sane/lib/src/isolate_messages/read.dart b/packages/sane/lib/src/isolate_messages/read.dart index f375463..e9ad229 100644 --- a/packages/sane/lib/src/isolate_messages/read.dart +++ b/packages/sane/lib/src/isolate_messages/read.dart @@ -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 { ReadMessage({ required this.saneHandle, required this.bufferSize, diff --git a/packages/sane/lib/src/sane_isolate.dart b/packages/sane/lib/src/sane_isolate.dart index f11968d..e666c59 100644 --- a/packages/sane/lib/src/sane_isolate.dart +++ b/packages/sane/lib/src/sane_isolate.dart @@ -45,11 +45,13 @@ class SaneIsolate implements Sane { _isolate.kill(priority: Isolate.immediate); } - Future _sendMessage(IsolateMessage message) async { + Future _sendMessage( + IsolateMessage message, + ) async { final replyPort = ReceivePort(); _sendPort.send( - _IsolateMessageEnveloppe( + _IsolateMessageEnvelope( replyPort: replyPort.sendPort, message: message, ), @@ -65,14 +67,14 @@ class SaneIsolate implements Sane { ); } - return response; + return response as T; } @override Future init({ AuthCallback? authCallback, }) async { - final response = await _sendMessage(InitMessage()) as InitResponse; + final response = await _sendMessage(InitMessage()); return response.versionCode; } @@ -86,10 +88,8 @@ class SaneIsolate implements Sane { required bool localOnly, }) async { final response = await _sendMessage( - GetDevicesMessage( - localOnly: localOnly, - ), - ) as GetDevicesResponse; + GetDevicesMessage(localOnly: localOnly), + ); return response.devices; } @@ -97,10 +97,8 @@ class SaneIsolate implements Sane { @override Future open(String deviceName) async { final response = await _sendMessage( - OpenMessage( - deviceName: deviceName, - ), - ) as OpenResponse; + OpenMessage(deviceName: deviceName), + ); return response.handle; } @@ -113,9 +111,7 @@ class SaneIsolate implements Sane { @override Future close(SaneHandle handle) async { await _sendMessage( - CloseMessage( - saneHandle: handle, - ), + CloseMessage(saneHandle: handle), ); } @@ -129,7 +125,7 @@ class SaneIsolate implements Sane { saneHandle: handle, index: index, ), - ) as GetOptionDescriptorResponse; + ); return response.optionDescriptor; } @@ -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; } @@ -161,7 +155,7 @@ class SaneIsolate implements Sane { action: action, value: value, ), - ) as ControlValueOptionResponse; + ); return response.result; } @@ -180,7 +174,7 @@ class SaneIsolate implements Sane { action: action, value: value, ), - ) as ControlValueOptionResponse; + ); return response.result; } @@ -199,7 +193,7 @@ class SaneIsolate implements Sane { action: action, value: value, ), - ) as ControlValueOptionResponse; + ); return response.result; } @@ -218,7 +212,7 @@ class SaneIsolate implements Sane { action: action, value: value, ), - ) as ControlValueOptionResponse; + ); return response.result; } @@ -233,7 +227,7 @@ class SaneIsolate implements Sane { saneHandle: handle, index: index, ), - ) as ControlButtonOptionResponse; + ); return response.result; } @@ -244,7 +238,7 @@ class SaneIsolate implements Sane { GetParametersMessage( saneHandle: handle, ), - ) as GetParametersResponse; + ); return response.parameters; } @@ -252,9 +246,7 @@ class SaneIsolate implements Sane { @override Future start(SaneHandle handle) async { await _sendMessage( - StartMessage( - saneHandle: handle, - ), + StartMessage(saneHandle: handle), ); } @@ -265,7 +257,7 @@ class SaneIsolate implements Sane { saneHandle: handle, bufferSize: bufferSize, ), - ) as ReadResponse; + ); return response.bytes; } @@ -273,9 +265,7 @@ class SaneIsolate implements Sane { @override Future cancel(SaneHandle handle) async { await _sendMessage( - CancelMessage( - saneHandle: handle, - ), + CancelMessage(saneHandle: handle), ); } @@ -305,12 +295,11 @@ 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, @@ -318,12 +307,12 @@ void _isolateEntryPoint(_IsolateEntryPointArgs args) { ); } - envellope.replyPort.send(response); + envelope.replyPort.send(response); }); } -class _IsolateMessageEnveloppe { - _IsolateMessageEnveloppe({ +class _IsolateMessageEnvelope { + _IsolateMessageEnvelope({ required this.replyPort, required this.message, });