Skip to content

Commit

Permalink
fix: Align web implementation to native implementation (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
littleGnAl authored Sep 11, 2023
1 parent 98ee75f commit 27bb000
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 134 deletions.
8 changes: 5 additions & 3 deletions lib/src/iris_method_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class IrisMethodChannel {
IrisMethodCall(eventKey.registerName, params)));

final nativeEventHandlerIntPtr = result.data['observerIntPtr'];
holder.nativeEventHandlerIntPtr = nativeEventHandlerIntPtr;
holder.eventHandlerHandle = nativeEventHandlerIntPtr;
} else {
result = CallApiResult(irisReturnCode: 0, data: {'result': 0});
}
Expand Down Expand Up @@ -176,13 +176,15 @@ class IrisMethodChannel {
if (holder != null) {
holder.removeEventHandler(scopedEvent.handler);
if (holder.getEventHandlers().isEmpty) {
final eventHandlerHandle = holder.eventHandlerHandle;
assert(eventHandlerHandle != null);
return _irisMethodChannelInternal
.execute(DestroyNativeEventHandlerRequest(
IrisMethodCall(
scopedEvent.unregisterName,
params,
rawBufferParams: [
BufferParam(BufferParamHandle(holder.nativeEventHandlerIntPtr), 1)
BufferParam(BufferParamHandle(eventHandlerHandle!()), 1)
],
),
));
Expand Down Expand Up @@ -210,7 +212,7 @@ class IrisMethodChannel {
'',
rawBufferParams: [
BufferParam(
BufferParamHandle(holder.nativeEventHandlerIntPtr), 1)
BufferParamHandle(holder.eventHandlerHandle!()), 1)
],
))
.toList();
Expand Down
2 changes: 1 addition & 1 deletion lib/src/platform/io/iris_method_channel_internal_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ class _IrisMethodChannelNative {
methodCall.params,
rawBufferParams: [BufferParam(BufferParamHandle(eventHandlerIntPtr), 1)],
));
result.data['observerIntPtr'] = eventHandlerIntPtr;
result.data['observerIntPtr'] = IrisEventHandlerHandle(eventHandlerIntPtr);
return result;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/src/platform/iris_method_channel_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ class EventHandlerHolder
final EventHandlerHolderKey key;
final Set<EventLoopEventHandler> _eventHandlers = {};

int nativeEventHandlerIntPtr = 0;
IrisEventHandlerHandle? eventHandlerHandle;

void addEventHandler(EventLoopEventHandler eventHandler) {
_eventHandlers.add(eventHandler);
Expand Down
45 changes: 16 additions & 29 deletions lib/src/platform/web/bindings/iris_api_common_bindings_js.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import 'package:js/js.dart';
// ignore_for_file: public_member_api_docs, non_constant_identifier_names

// NOTE:
// For compatibility to dart sdk >= 2.12, we only use the feature that are
// For compatibility to dart sdk >= 2.12, we only use the feature that are
// supported in `js: 0.6.3` at this time

@JS('AgoraWrapper.EventParam')
@JS('IrisCore.EventParam')
@anonymous
class EventParam {
// Must have an unnamed factory constructor with named arguments.
Expand Down Expand Up @@ -42,7 +42,7 @@ IrisEventMessage toIrisEventMessage(EventParam param) {

typedef ApiParam = EventParam;

@JS('AgoraWrapper.CallIrisApiResult')
@JS('IrisCore.CallIrisApiResult')
@anonymous
class CallIrisApiResult {
external factory CallIrisApiResult({
Expand All @@ -61,39 +61,26 @@ extension CallIrisApiResultExt on CallIrisApiResult {
}
}

typedef IrisCEventHandler = void Function(EventParam param);

@JS('AgoraWrapper.IrisEventHandlerHandle')
@JS('IrisCore.IrisEventHandler')
@anonymous
class IrisEventHandlerHandle {}
class IrisEventHandler {}

@JS('AgoraWrapper.IrisApiEngine')
@JS('IrisCore.IrisApiEngine')
@anonymous
class IrisApiEngine {}

@JS('AgoraWrapper.CreateIrisApiEngine')
external IrisApiEngine CreateIrisApiEngine();

@JS('AgoraWrapper.DestroyIrisApiEngine')
external int DestroyIrisApiEngine(IrisApiEngine engine_ptr);
@JS('IrisCore.createIrisApiEngine')
external IrisApiEngine createIrisApiEngine();

@JS('AgoraWrapper.CallIrisApi')
external int CallIrisApi(IrisApiEngine engine_ptr, ApiParam apiParam);
@JS('IrisCore.disposeIrisApiEngine')
external int disposeIrisApiEngine(IrisApiEngine engine_ptr);

@JS('AgoraWrapper.CallIrisApiAsync')
external Future<CallIrisApiResult> CallIrisApiAsync(
IrisApiEngine engine_ptr, ApiParam apiParam);
@JS('IrisCore.callIrisApi')
external int callIrisApi(IrisApiEngine engine_ptr, ApiParam apiParam);

typedef IrisCEventHandlerDartCallback = void Function(EventParam param);
typedef IrisEventHandlerFuncJS = void Function(EventParam param);
typedef IrisCEventHandler = IrisEventHandlerFuncJS;

@JS('AgoraWrapper.CreateIrisEventHandler')
external IrisEventHandlerHandle CreateIrisEventHandler(
@JS('IrisCore.createIrisEventHandler')
external IrisEventHandler createIrisEventHandler(
IrisCEventHandler event_handler);

@JS('AgoraWrapper.SetIrisRtcEngineEventHandler')
external IrisEventHandlerHandle SetIrisRtcEngineEventHandler(
IrisApiEngine engine_ptr, IrisEventHandlerHandle event_handler);

@JS('AgoraWrapper.UnsetIrisRtcEngineEventHandler')
external IrisEventHandlerHandle UnsetIrisRtcEngineEventHandler(
IrisApiEngine engine_ptr, IrisEventHandlerHandle event_handler);
44 changes: 0 additions & 44 deletions lib/src/platform/web/iris_event_web.dart

This file was deleted.

67 changes: 54 additions & 13 deletions lib/src/platform/web/iris_method_channel_internal_web.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import 'dart:async';
import 'package:flutter/foundation.dart' show VoidCallback, debugPrint;
import 'dart:js';
import 'package:flutter/foundation.dart' show VoidCallback;
import 'package:iris_method_channel/iris_method_channel.dart';
import 'package:iris_method_channel/src/platform/web/iris_event_web.dart';

import 'package:iris_method_channel/src/platform/web/bindings/iris_api_common_bindings_js.dart'
as js_binding;

// ignore_for_file: public_member_api_docs

Expand All @@ -13,10 +16,15 @@ class IrisMethodChannelInternalWeb implements IrisMethodChannelInternal {
IrisMethodChannelInternalWeb(this._nativeBindingsProvider);

final PlatformBindingsProvider _nativeBindingsProvider;
IrisEventWeb? _irisEventWeb;
IrisApiEngineHandle? _irisApiEngine;
PlatformBindingsDelegateInterface? _platformBindingsDelegate;

IrisEventMessageListener? _irisEventMessageListener;

js_binding.IrisCEventHandler? _irisEventHandlerFuncJS;

IrisEventHandlerHandle? _irisEventHandlerHandle;

@override
VoidCallback addHotRestartListener(HotRestartListener listener) {
return () {};
Expand All @@ -26,18 +34,42 @@ class IrisMethodChannelInternalWeb implements IrisMethodChannelInternal {
Future<void> dispose() async {
assert(_irisApiEngine != null);

_irisEventWeb?.dispose();
_irisEventWeb = null;

_platformBindingsDelegate?.destroyNativeApiEngine(_irisApiEngine!);

_platformBindingsDelegate
?.destroyIrisEventHandler(_irisEventHandlerHandle!);

_platformBindingsDelegate = null;
_irisApiEngine = null;
_irisEventHandlerFuncJS = null;
_irisEventHandlerHandle = null;
_irisEventMessageListener = null;
}

@override
Future<CallApiResult> execute(Request request) async {
if (request is CreateNativeEventHandlerRequest) {
return CallApiResult(irisReturnCode: 0, data: {'observerIntPtr': 0});
final methodCall = request.methodCall;

await _executeMethodCall(IrisMethodCall(
methodCall.funcName,
methodCall.params,
rawBufferParams: [
BufferParam(BufferParamHandle(_irisEventHandlerHandle!()), 1)
],
));

return CallApiResult(
irisReturnCode: 0,
data: {'observerIntPtr': _irisEventHandlerHandle},
);
} else if (request is DestroyNativeEventHandlerRequest) {
final methodCall = request.methodCall;
if (methodCall.funcName.isEmpty) {
return CallApiResult(irisReturnCode: 0, data: {'result': 0});
}

return _executeMethodCall(methodCall);
} else if (request is ApiCallRequest) {
final IrisMethodCall methodCall = request.methodCall;
return _executeMethodCall(methodCall);
Expand All @@ -61,6 +93,12 @@ class IrisMethodChannelInternalWeb implements IrisMethodChannelInternal {
return 0;
}

void _onEventFromJS(js_binding.EventParam param) {
if (_irisEventMessageListener != null) {
_irisEventMessageListener?.call(js_binding.toIrisEventMessage(param));
}
}

@override
Future<InitilizationResult?> initilize(List<int> args) async {
_platformBindingsDelegate =
Expand All @@ -69,10 +107,9 @@ class IrisMethodChannelInternalWeb implements IrisMethodChannelInternal {
_platformBindingsDelegate!.createApiEngine(args);
_irisApiEngine = createApiEngineResult.apiEnginePtr;

final irisEvent = _nativeBindingsProvider.provideIrisEvent() ??
IrisEventWeb(_irisApiEngine!);
_irisEventWeb = irisEvent as IrisEventWeb;
_irisEventWeb!.initialize();
_irisEventHandlerFuncJS = allowInterop(_onEventFromJS);
_irisEventHandlerHandle = _platformBindingsDelegate!.createIrisEventHandler(
IrisCEventHandlerHandle(_irisEventHandlerFuncJS!));

return InitilizationResultWeb();
}
Expand All @@ -87,7 +124,11 @@ class IrisMethodChannelInternalWeb implements IrisMethodChannelInternal {
results.add(result);
}
} else if (request is DestroyNativeEventHandlerListRequest) {
debugPrint('[listExecute] Not implemented request: $request');
final methodCalls = request.methodCalls;
for (final methodCall in methodCalls) {
final result = await _executeMethodCall(methodCall);
results.add(result);
}
}

return results;
Expand All @@ -98,6 +139,6 @@ class IrisMethodChannelInternalWeb implements IrisMethodChannelInternal {

@override
void setIrisEventMessageListener(IrisEventMessageListener listener) {
_irisEventWeb!.setIrisEventMessageListener(listener);
_irisEventMessageListener = listener;
}
}
Loading

0 comments on commit 27bb000

Please sign in to comment.