From b227f3171f90f0d97e1ab3891aa6ade4663c7618 Mon Sep 17 00:00:00 2001 From: littleGnAl Date: Wed, 18 Oct 2023 19:54:37 +0800 Subject: [PATCH] feat: Introduce the InitilizationArgProvider to allow create the initilization arg lazily --- lib/src/iris_handles.dart | 15 ++++++++------- lib/src/iris_method_channel.dart | 3 ++- .../io/iris_method_channel_internal_io.dart | 13 +++++-------- .../platform/iris_method_channel_interface.dart | 8 +++++++- .../platform_bindings_delegate_interface.dart | 2 +- .../web/iris_method_channel_internal_web.dart | 3 ++- .../fake/fake_platform_binding_delegate_io.dart | 2 +- .../fake/fake_platform_binding_delegate_web.dart | 2 +- 8 files changed, 27 insertions(+), 21 deletions(-) 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..d27c229 100644 --- a/lib/src/platform/iris_method_channel_interface.dart +++ b/lib/src/platform/iris_method_channel_interface.dart @@ -96,8 +96,14 @@ 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); +} + +// typedef InitilizationArgProvider = Object Function(Object 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/platform/fake/fake_platform_binding_delegate_io.dart b/test/platform/fake/fake_platform_binding_delegate_io.dart index 4fdaca0..cfffe38 100644 --- a/test/platform/fake/fake_platform_binding_delegate_io.dart +++ b/test/platform/fake/fake_platform_binding_delegate_io.dart @@ -89,7 +89,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}, diff --git a/test/platform/fake/fake_platform_binding_delegate_web.dart b/test/platform/fake/fake_platform_binding_delegate_web.dart index 76701dc..17a3765 100644 --- a/test/platform/fake/fake_platform_binding_delegate_web.dart +++ b/test/platform/fake/fake_platform_binding_delegate_web.dart @@ -51,7 +51,7 @@ class FakeNativeBindingDelegate extends PlatformBindingsDelegateInterface { } @override - CreateApiEngineResult createApiEngine(List args) { + CreateApiEngineResult createApiEngine(List args) { return CreateApiEngineResult( IrisApiEngineHandle(FakeTypeWeb()), extraData: {'extra_handle': 1000},