Skip to content

Commit

Permalink
feat: Introduce the InitilizationArgProvider to allow the creation of…
Browse files Browse the repository at this point in the history
… the initialization arg lazily (#90)

Introduce the InitilizationArgProvider to allow the creation of the
initialization arg lazily
  • Loading branch information
littleGnAl committed Oct 24, 2023
1 parent 53fe31e commit b0f6be2
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion example/integration_test/iris_event_smoke_test.dart
Expand Up @@ -21,6 +21,6 @@ void main() {
irisEvent.onEventPtr;
irisEvent.dispose();
},
timeout: const Timeout(Duration(minutes: 30)),
timeout: const Timeout(Duration(minutes: 20)),
);
}
4 changes: 2 additions & 2 deletions example/integration_test/iris_method_channel_smoke_test.dart
Expand Up @@ -27,7 +27,7 @@ class _FakeNativeBindingDelegate extends PlatformBindingsDelegateInterface {
}

@override
CreateApiEngineResult createApiEngine(List<Object> args) {
CreateApiEngineResult createApiEngine(List<InitilizationArgProvider> args) {
return CreateApiEngineResult(
IrisApiEngineHandle(ffi.Pointer<ffi.Void>.fromAddress(100)),
extraData: <String, Object>{'extra_handle': 1000},
Expand Down Expand Up @@ -89,6 +89,6 @@ void main() {

expect(hotRestartListenerCalled, true);
},
timeout: const Timeout(Duration(minutes: 10)),
timeout: const Timeout(Duration(minutes: 20)),
);
}
15 changes: 8 additions & 7 deletions lib/src/iris_handles.dart
Expand Up @@ -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;

Expand All @@ -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);
}
3 changes: 2 additions & 1 deletion lib/src/iris_method_channel.dart
Expand Up @@ -37,7 +37,8 @@ class IrisMethodChannel {
});
}

Future<InitilizationResult?> initilize(List<int> args) async {
Future<InitilizationResult?> initilize(
List<InitilizationArgProvider> args) async {
if (_initilized) {
return null;
}
Expand Down
13 changes: 5 additions & 8 deletions lib/src/platform/io/iris_method_channel_internal_io.dart
Expand Up @@ -66,7 +66,7 @@ class _InitilizationArgs {
final SendPort eventPortSendPort;
final SendPort? onExitSendPort;
final PlatformBindingsProvider provider;
final List<int> argNativeHandles;
final List<InitilizationArgProvider> argNativeHandles;
}

class InitilizationResultIO implements InitilizationResult {
Expand Down Expand Up @@ -316,7 +316,7 @@ class _IrisMethodChannelNative {
}

CreateApiEngineResult initilize(
SendPort sendPort, List<ffi.Pointer<ffi.Void>> args) {
SendPort sendPort, List<InitilizationArgProvider> args) {
_irisEvent.initialize();
_irisEvent.registerEventHandler(sendPort);

Expand Down Expand Up @@ -412,10 +412,6 @@ class IrisMethodChannelInternalIO implements IrisMethodChannelInternal {
final SendPort? onExitSendPort = args.onExitSendPort;
final PlatformBindingsProvider provider = args.provider;

final List<ffi.Pointer<ffi.Void>> argsInner = args.argNativeHandles
.map<ffi.Pointer<ffi.Void>>((e) => ffi.Pointer.fromAddress(e))
.toList();

final apiCallPort = ReceivePort();

final nativeBindingDelegate = provider.provideNativeBindingDelegate();
Expand All @@ -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;
Expand Down Expand Up @@ -503,7 +499,8 @@ class IrisMethodChannelInternalIO implements IrisMethodChannelInternal {
}

@override
Future<InitilizationResult?> initilize(List<int> args) async {
Future<InitilizationResult?> initilize(
List<InitilizationArgProvider> args) async {
if (_initilized) {
return null;
}
Expand Down
6 changes: 5 additions & 1 deletion lib/src/platform/iris_method_channel_interface.dart
Expand Up @@ -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<InitilizationResult?> initilize(List<int> args);
Future<InitilizationResult?> initilize(List<InitilizationArgProvider> args);

// Future<CallApiResult> invokeMethod(IrisMethodCall methodCall);

Expand Down
2 changes: 1 addition & 1 deletion lib/src/platform/platform_bindings_delegate_interface.dart
Expand Up @@ -16,7 +16,7 @@ class CreateApiEngineResult {
abstract class PlatformBindingsDelegateInterface {
void initialize();

CreateApiEngineResult createApiEngine(List<Object> args);
CreateApiEngineResult createApiEngine(List<InitilizationArgProvider> args);

int callApi(
IrisMethodCall methodCall,
Expand Down
3 changes: 2 additions & 1 deletion lib/src/platform/web/iris_method_channel_internal_web.dart
Expand Up @@ -100,7 +100,8 @@ class IrisMethodChannelInternalWeb implements IrisMethodChannelInternal {
}

@override
Future<InitilizationResult?> initilize(List<int> args) async {
Future<InitilizationResult?> initilize(
List<InitilizationArgProvider> args) async {
_platformBindingsDelegate =
_nativeBindingsProvider.provideNativeBindingDelegate();
final createApiEngineResult =
Expand Down
26 changes: 26 additions & 0 deletions test/iris_method_channel_test.dart
Expand Up @@ -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(
Expand Down Expand Up @@ -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();
},
);
}
17 changes: 15 additions & 2 deletions test/platform/fake/fake_platform_binding_delegate_io.dart
Expand Up @@ -89,9 +89,22 @@ class FakeNativeBindingDelegate extends PlatformBindingsDelegateInterface {
}

@override
CreateApiEngineResult createApiEngine(List<Object> args) {
CreateApiEngineResult createApiEngine(List<InitilizationArgProvider> args) {
final engineHandle =
IrisApiEngineHandle(ffi.Pointer<ffi.Void>.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<ffi.Void>.fromAddress(100)),
engineHandle,
extraData: <String, Object>{'extra_handle': 1000},
);
}
Expand Down
17 changes: 15 additions & 2 deletions 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';
Expand Down Expand Up @@ -51,9 +52,21 @@ class FakeNativeBindingDelegate extends PlatformBindingsDelegateInterface {
}

@override
CreateApiEngineResult createApiEngine(List<Object> args) {
CreateApiEngineResult createApiEngine(List<InitilizationArgProvider> 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: <String, Object>{'extra_handle': 1000},
);
}
Expand Down

0 comments on commit b0f6be2

Please sign in to comment.