bridgeCallbackGlobalHandler function

Pointer<JSValueConst>? bridgeCallbackGlobalHandler(
  1. Pointer<JSContext> ctx,
  2. Pointer<Utf8NullTerminated> channelName,
  3. Pointer<Utf8NullTerminated> message
)

Implementation

Pointer<JSValueConst>? bridgeCallbackGlobalHandler(
  Pointer<JSContext> ctx,
  Pointer<Utf8NullTerminated> channelName,
  Pointer<Utf8NullTerminated> message,
) {
  String channelNameStr = Utf8NullTerminated.fromUtf8(channelName);
  String messageStr = Utf8NullTerminated.fromUtf8(message);

  if (mapJsBridge.containsKey(channelNameStr)) {
    String? result = 'NO RESULT YET';
    try {
      result = mapJsBridge[channelNameStr]!.call(jsonDecode(messageStr));
    } on Error catch (e) {
      result = e.toString();
      print('ERROR ------ $e');
    } on Exception catch (e) {
      result = e.toString();
      print('EXCEPTION ------ $e');
    }
    if (result == null) {
      Pointer<JSValueConst> nullValue = calloc();
      QuickJsRuntime._jsGetNullValue(ctx, nullValue);
      return nullValue;
    } else {
      return QuickJsRuntime.jsEval(ctx, "'$result';").rawResult;
    }
  }

  return QuickJsRuntime.jsEval(
          ctx, "'No channel registered: ($channelNameStr) => $messageStr'")
      .rawResult;
}