invokeModule function
dynamic
invokeModule(
- Pointer<
Void> callbackContext, - WebFController controller,
- String moduleName,
- String method,
- dynamic params,
- DartAsyncModuleCallback callback, {
- BindingOpItem? profileOp,
Implementation
dynamic invokeModule(Pointer<Void> callbackContext, WebFController controller, String moduleName, String method, params,
DartAsyncModuleCallback callback,
{BindingOpItem? profileOp}) {
WebFViewController currentView = controller.view;
dynamic result;
Stopwatch? stopwatch;
if (enableWebFCommandLog) {
stopwatch = Stopwatch()..start();
}
try {
Future<dynamic> invokeModuleCallback({String? error, data}) {
Completer<dynamic> completer = Completer();
// To make sure Promise then() and catch() executed before Promise callback called at JavaScript side.
// We should make callback always async.
Future.microtask(() {
if (controller.view != currentView || currentView.disposed || callback == nullptr) return;
Pointer<NativeFunction<NativeHandleInvokeModuleResult>> handleResult =
Pointer.fromFunction(_handleInvokeModuleResult);
if (error != null) {
Pointer<Utf8> errmsgPtr = error.toNativeUtf8();
_InvokeModuleResultContext context = _InvokeModuleResultContext(
completer, currentView, moduleName, method, params,
errmsgPtr: errmsgPtr, stopwatch: stopwatch);
callback(callbackContext, currentView.contextId, errmsgPtr, nullptr, context, handleResult);
} else {
Pointer<NativeValue> dataPtr = malloc.allocate(sizeOf<NativeValue>());
toNativeValue(dataPtr, data);
_InvokeModuleResultContext context = _InvokeModuleResultContext(
completer, currentView, moduleName, method, params,
data: dataPtr, stopwatch: stopwatch);
callback(callbackContext, currentView.contextId, nullptr, dataPtr, context, handleResult);
}
});
return completer.future;
}
if (enableWebFProfileTracking) {
WebFProfiler.instance.startTrackBindingSteps(profileOp!, 'moduleManager.invokeModule');
}
result = controller.module.moduleManager.invokeModule(moduleName, method, params, invokeModuleCallback);
if (enableWebFProfileTracking) {
WebFProfiler.instance.finishTrackBindingSteps(profileOp!);
}
} catch (e, stack) {
if (enableWebFCommandLog) {
print('Invoke module failed: $e\n$stack');
}
String error = '$e\n$stack';
if (callback == nullptr) return;
callback(callbackContext, currentView.contextId, error.toNativeUtf8(), nullptr, {}, nullptr);
}
if (enableWebFCommandLog) {
print('Invoke module name: $moduleName method: $method, params: $params '
'return: $result time: ${stopwatch!.elapsedMicroseconds}us');
}
return result;
}