init static method

Future<YCrdtWorld> init(
  1. WasmInstanceBuilder builder, {
  2. required YCrdtWorldImports imports,
})

Implementation

static Future<YCrdtWorld> init(
  WasmInstanceBuilder builder, {
  required YCrdtWorldImports imports,
}) async {
  late final WasmLibrary library;
  WasmLibrary getLib() => library;

  {
    const ft =
        FuncType([('function-id', U32()), ('event', YEvent._spec)], []);

    (ListValue, void Function()) execImportsEventCallback(ListValue args) {
      final args0 = args[0];
      final args1 = args[1];
      imports.eventCallback(
          functionId: args0! as int, event: YEvent.fromJson(args1));
      return (const [], () {});
    }

    final lowered = loweredImportFunction(
        r'$root#event-callback', ft, execImportsEventCallback, getLib);
    builder.addImport(r'$root', 'event-callback', lowered);
  }
  {
    const ft = FuncType(
        [('function-id', U32()), ('event', ListType(YEvent._spec))], []);

    (ListValue, void Function()) execImportsEventDeepCallback(
        ListValue args) {
      final args0 = args[0];
      final args1 = args[1];
      imports.eventDeepCallback(
          functionId: args0! as int,
          event: (args1! as Iterable).map(YEvent.fromJson).toList());
      return (const [], () {});
    }

    final lowered = loweredImportFunction(r'$root#event-deep-callback', ft,
        execImportsEventDeepCallback, getLib);
    builder.addImport(r'$root', 'event-deep-callback', lowered);
  }
  {
    const ft =
        FuncType([('function-id', U32()), ('event', YUndoEvent._spec)], []);

    (ListValue, void Function()) execImportsUndoEventCallback(
        ListValue args) {
      final args0 = args[0];
      final args1 = args[1];
      imports.undoEventCallback(
          functionId: args0! as int, event: YUndoEvent.fromJson(args1));
      return (const [], () {});
    }

    final lowered = loweredImportFunction(r'$root#undo-event-callback', ft,
        execImportsUndoEventCallback, getLib);
    builder.addImport(r'$root', 'undo-event-callback', lowered);
  }
  final instance = await builder.build();

  library = WasmLibrary(instance, int64Type: Int64TypeConfig.bigInt);
  return YCrdtWorld(imports: imports, library: library);
}