From b0f6be203675bdfe89c86c0eb55c20b50519c3b1 Mon Sep 17 00:00:00 2001 From: Littlegnal <8847263+littleGnAl@users.noreply.github.com> Date: Tue, 24 Oct 2023 16:54:36 +0800 Subject: [PATCH] feat: Introduce the InitilizationArgProvider to allow the creation of the initialization arg lazily (#90) Introduce the InitilizationArgProvider to allow the creation of the initialization arg lazily --- .github/workflows/ci.yaml | 2 +- .../iris_event_smoke_test.dart | 2 +- .../iris_method_channel_smoke_test.dart | 4 +-- lib/src/iris_handles.dart | 15 ++++++----- lib/src/iris_method_channel.dart | 3 ++- .../io/iris_method_channel_internal_io.dart | 13 ++++------ .../iris_method_channel_interface.dart | 6 ++++- .../platform_bindings_delegate_interface.dart | 2 +- .../web/iris_method_channel_internal_web.dart | 3 ++- test/iris_method_channel_test.dart | 26 +++++++++++++++++++ .../fake_platform_binding_delegate_io.dart | 17 ++++++++++-- .../fake_platform_binding_delegate_web.dart | 17 ++++++++++-- 12 files changed, 83 insertions(+), 27 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index b67f709..b99eac7 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -270,7 +270,7 @@ jobs: - uses: actions/checkout@v1 - uses: subosito/flutter-action@v2 with: - flutter-version: '3.10.0' # Run the latest version + flutter-version: '3.13.0' # Run the latest version cache: true - uses: futureware-tech/simulator-action@v2 with: diff --git a/example/integration_test/iris_event_smoke_test.dart b/example/integration_test/iris_event_smoke_test.dart index 04f2917..effdcbc 100644 --- a/example/integration_test/iris_event_smoke_test.dart +++ b/example/integration_test/iris_event_smoke_test.dart @@ -21,6 +21,6 @@ void main() { irisEvent.onEventPtr; irisEvent.dispose(); }, - timeout: const Timeout(Duration(minutes: 30)), + timeout: const Timeout(Duration(minutes: 20)), ); } diff --git a/example/integration_test/iris_method_channel_smoke_test.dart b/example/integration_test/iris_method_channel_smoke_test.dart index f585c79..a9e760b 100644 --- a/example/integration_test/iris_method_channel_smoke_test.dart +++ b/example/integration_test/iris_method_channel_smoke_test.dart @@ -27,7 +27,7 @@ class _FakeNativeBindingDelegate extends PlatformBindingsDelegateInterface { } @override - CreateApiEngineResult createApiEngine(List args) { + CreateApiEngineResult createApiEngine(List args) { return CreateApiEngineResult( IrisApiEngineHandle(ffi.Pointer.fromAddress(100)), extraData: {'extra_handle': 1000}, @@ -89,6 +89,6 @@ void main() { expect(hotRestartListenerCalled, true); }, - timeout: const Timeout(Duration(minutes: 10)), + timeout: const Timeout(Duration(minutes: 20)), ); } diff --git a/lib/src/iris_handles.dart b/lib/src/iris_handles.dart index 0f88c26..0538806 100644 --- a/lib/src/iris_handles.dart +++ b/lib/src/iris_handles.dart @@ -14,8 +14,9 @@ abstract class IrisHandle { /// [IrisHandle] implementation that hold a value of [Object] type. And return the /// value when the callable function is called. -class _ObjectIrisHandle extends IrisHandle { - const _ObjectIrisHandle(this._h); +class ObjectIrisHandle extends IrisHandle { + /// Construct the [ObjectIrisHandle] + const ObjectIrisHandle(this._h); final Object _h; @@ -26,31 +27,31 @@ class _ObjectIrisHandle extends IrisHandle { } /// The [IrisHandle] of the iris's `IrisApiEngine` -class IrisApiEngineHandle extends _ObjectIrisHandle { +class IrisApiEngineHandle extends ObjectIrisHandle { /// Construct the [IrisApiEngineHandle] const IrisApiEngineHandle(Object h) : super(h); } /// The [IrisHandle] of the iris's `ApiParam` -class IrisApiParamHandle extends _ObjectIrisHandle { +class IrisApiParamHandle extends ObjectIrisHandle { /// Construct the [IrisApiParamHandle] const IrisApiParamHandle(Object h) : super(h); } /// The [IrisHandle] of the iris's `IrisCEventHandler` -class IrisCEventHandlerHandle extends _ObjectIrisHandle { +class IrisCEventHandlerHandle extends ObjectIrisHandle { /// Construct the [IrisCEventHandlerHandle] const IrisCEventHandlerHandle(Object h) : super(h); } /// The [IrisHandle] of the iris's `IrisEventHandler` -class IrisEventHandlerHandle extends _ObjectIrisHandle { +class IrisEventHandlerHandle extends ObjectIrisHandle { /// Construct the [IrisEventHandlerHandle] const IrisEventHandlerHandle(Object h) : super(h); } /// The [IrisHandle] of the `BufferParam` -class BufferParamHandle extends _ObjectIrisHandle { +class BufferParamHandle extends ObjectIrisHandle { /// Construct the [BufferParamHandle] const BufferParamHandle(Object h) : super(h); } diff --git a/lib/src/iris_method_channel.dart b/lib/src/iris_method_channel.dart index 4a51b75..2567dc4 100644 --- a/lib/src/iris_method_channel.dart +++ b/lib/src/iris_method_channel.dart @@ -37,7 +37,8 @@ class IrisMethodChannel { }); } - Future initilize(List args) async { + Future initilize( + List args) async { if (_initilized) { return null; } diff --git a/lib/src/platform/io/iris_method_channel_internal_io.dart b/lib/src/platform/io/iris_method_channel_internal_io.dart index 940025e..72b07b8 100644 --- a/lib/src/platform/io/iris_method_channel_internal_io.dart +++ b/lib/src/platform/io/iris_method_channel_internal_io.dart @@ -66,7 +66,7 @@ class _InitilizationArgs { final SendPort eventPortSendPort; final SendPort? onExitSendPort; final PlatformBindingsProvider provider; - final List argNativeHandles; + final List argNativeHandles; } class InitilizationResultIO implements InitilizationResult { @@ -316,7 +316,7 @@ class _IrisMethodChannelNative { } CreateApiEngineResult initilize( - SendPort sendPort, List> args) { + SendPort sendPort, List args) { _irisEvent.initialize(); _irisEvent.registerEventHandler(sendPort); @@ -412,10 +412,6 @@ class IrisMethodChannelInternalIO implements IrisMethodChannelInternal { final SendPort? onExitSendPort = args.onExitSendPort; final PlatformBindingsProvider provider = args.provider; - final List> argsInner = args.argNativeHandles - .map>((e) => ffi.Pointer.fromAddress(e)) - .toList(); - final apiCallPort = ReceivePort(); final nativeBindingDelegate = provider.provideNativeBindingDelegate(); @@ -428,7 +424,7 @@ class IrisMethodChannelInternalIO implements IrisMethodChannelInternal { final _IrisMethodChannelNative executor = _IrisMethodChannelNative( nativeBindingDelegate, irisEvent as IrisEventIO); final CreateApiEngineResult executorInitilizationResult = - executor.initilize(mainEventSendPort, argsInner); + executor.initilize(mainEventSendPort, args.argNativeHandles); int? debugIrisCEventHandlerNativeHandle; int? debugIrisEventHandlerNativeHandle; @@ -503,7 +499,8 @@ class IrisMethodChannelInternalIO implements IrisMethodChannelInternal { } @override - Future initilize(List args) async { + Future initilize( + List args) async { if (_initilized) { return null; } diff --git a/lib/src/platform/iris_method_channel_interface.dart b/lib/src/platform/iris_method_channel_interface.dart index e9cd945..40df4ef 100644 --- a/lib/src/platform/iris_method_channel_interface.dart +++ b/lib/src/platform/iris_method_channel_interface.dart @@ -96,8 +96,12 @@ class DestroyNativeEventHandlerListRequest extends IrisMethodCallListRequest { /// * You should not do some asynchronous jobs inside this listener. typedef HotRestartListener = void Function(Object? message); +abstract class InitilizationArgProvider { + IrisHandle provide(IrisApiEngineHandle apiEngineHandle); +} + abstract class IrisMethodChannelInternal { - Future initilize(List args); + Future initilize(List args); // Future invokeMethod(IrisMethodCall methodCall); diff --git a/lib/src/platform/platform_bindings_delegate_interface.dart b/lib/src/platform/platform_bindings_delegate_interface.dart index d976dea..f261222 100644 --- a/lib/src/platform/platform_bindings_delegate_interface.dart +++ b/lib/src/platform/platform_bindings_delegate_interface.dart @@ -16,7 +16,7 @@ class CreateApiEngineResult { abstract class PlatformBindingsDelegateInterface { void initialize(); - CreateApiEngineResult createApiEngine(List args); + CreateApiEngineResult createApiEngine(List args); int callApi( IrisMethodCall methodCall, diff --git a/lib/src/platform/web/iris_method_channel_internal_web.dart b/lib/src/platform/web/iris_method_channel_internal_web.dart index f88d88b..59bbbb1 100644 --- a/lib/src/platform/web/iris_method_channel_internal_web.dart +++ b/lib/src/platform/web/iris_method_channel_internal_web.dart @@ -100,7 +100,8 @@ class IrisMethodChannelInternalWeb implements IrisMethodChannelInternal { } @override - Future initilize(List args) async { + Future initilize( + List args) async { _platformBindingsDelegate = _nativeBindingsProvider.provideNativeBindingDelegate(); final createApiEngineResult = diff --git a/test/iris_method_channel_test.dart b/test/iris_method_channel_test.dart index 643aa8c..4752c19 100644 --- a/test/iris_method_channel_test.dart +++ b/test/iris_method_channel_test.dart @@ -9,6 +9,15 @@ import 'package:iris_method_channel/iris_method_channel.dart'; import 'platform/platform_cases.dart'; import 'platform/platform_tester.dart'; +class _TestInitilizationArgProvider extends InitilizationArgProvider { + bool called = false; + @override + IrisHandle provide(IrisApiEngineHandle apiEngineHandle) { + called = true; + return ObjectIrisHandle(called); + } +} + class _TestEventLoopEventHandler extends EventLoopEventHandler { @override bool handleEventInternal( @@ -802,4 +811,21 @@ void main() { await irisMethodChannel.dispose(); }, ); + + test( + 'Can pass InitilizationArgProvider', + () async { + final argProvider = _TestInitilizationArgProvider(); + await irisMethodChannel.initilize([argProvider]); + + final registerEventHandlerCallRecord = messenger.callApiRecords + .where((e) => e.methodCall.funcName == 'createApiEngine') + .toList(); + final resData = registerEventHandlerCallRecord[0].apiParam.data; + + expect(Map.from(jsonDecode(resData))['args'], true); + + await irisMethodChannel.dispose(); + }, + ); } diff --git a/test/platform/fake/fake_platform_binding_delegate_io.dart b/test/platform/fake/fake_platform_binding_delegate_io.dart index 4fdaca0..ae45d8f 100644 --- a/test/platform/fake/fake_platform_binding_delegate_io.dart +++ b/test/platform/fake/fake_platform_binding_delegate_io.dart @@ -89,9 +89,22 @@ class FakeNativeBindingDelegate extends PlatformBindingsDelegateInterface { } @override - CreateApiEngineResult createApiEngine(List args) { + CreateApiEngineResult createApiEngine(List args) { + final engineHandle = + IrisApiEngineHandle(ffi.Pointer.fromAddress(100)); + if (args.isNotEmpty) { + final value = args[0].provide(engineHandle)(); + final record = CallApiRecord( + const IrisMethodCall('createApiEngine', '{}'), + CallApiRecordApiParam( + 'createApiEngine', + jsonEncode({'args': value}), + ), + ); + apiCallPortSendPort.send(record); + } return CreateApiEngineResult( - IrisApiEngineHandle(ffi.Pointer.fromAddress(100)), + engineHandle, extraData: {'extra_handle': 1000}, ); } diff --git a/test/platform/fake/fake_platform_binding_delegate_web.dart b/test/platform/fake/fake_platform_binding_delegate_web.dart index 76701dc..f901070 100644 --- a/test/platform/fake/fake_platform_binding_delegate_web.dart +++ b/test/platform/fake/fake_platform_binding_delegate_web.dart @@ -1,3 +1,4 @@ +import 'dart:convert'; import 'dart:js' as js; import 'package:iris_method_channel/iris_method_channel.dart'; @@ -51,9 +52,21 @@ class FakeNativeBindingDelegate extends PlatformBindingsDelegateInterface { } @override - CreateApiEngineResult createApiEngine(List args) { + CreateApiEngineResult createApiEngine(List args) { + final engineHandle = IrisApiEngineHandle(FakeTypeWeb()); + if (args.isNotEmpty) { + final value = args[0].provide(engineHandle)(); + final record = CallApiRecord( + const IrisMethodCall('createApiEngine', '{}'), + CallApiRecordApiParam( + 'createApiEngine', + jsonEncode({'args': value}), + ), + ); + messenger.addCallApiRecord(record); + } return CreateApiEngineResult( - IrisApiEngineHandle(FakeTypeWeb()), + engineHandle, extraData: {'extra_handle': 1000}, ); }