createCompressionRsInMemoryWorker function

Future<CompressionRsWorld> createCompressionRsInMemoryWorker({
  1. required CompressionRsWorldImports imports,
  2. Future<WasmModule> loadModule()?,
  3. WorkersConfig? workersConfig,
})

Creates a CompressionRsWorld with the given wasiConfig. It setsUp the dynamic library for wasm_run in native platforms and loads the compression_rs WASM module from the file system or from the url pointing to 'lib/compression_rs_wasm.wasm'.

If loadModule is provided, it will be used to load the WASM module. This can be useful if you want to provide a different configuration or implementation, or you are loading it from Flutter assets or from a different HTTP endpoint. By default, it will load the WASM module from the file system in lib/compression_rs_wasm.wasm either reading it directly in native platforms or with a GET request for Dart web.

This version of the function is used to create a world that supports asynchronous executions thorough OS threads or Web Workers. However, it only supports in memory inputs and outputs.

Implementation

Future<CompressionRsWorld> createCompressionRsInMemoryWorker({
  required CompressionRsWorldImports imports,
  Future<WasmModule> Function()? loadModule,
  WorkersConfig? workersConfig,
}) async {
  await WasmRunLibrary.setUp(override: false);

  final WasmModule module;
  if (loadModule != null) {
    module = await loadModule();
  } else {
    final uri = await WasmFileUris.uriForPackage(
      package: 'compression_rs',
      libPath: 'assets/compression_rs_wasm.threads.wasm',
      envVariable: 'COMPRESSION_RS_WASM_THREADS_PATH',
    );
    final uris = WasmFileUris(uri: uri);
    module = await uris.loadModule(
      config: ModuleConfig(
        wasmtime: ModuleConfigWasmtime(wasmThreads: true),
      ),
    );
  }
  final defaultNumWorkers = identical(0, 0.0) ? 2 : Platform.numberOfProcessors;
  final builder = module.builder(
    workersConfig:
        workersConfig ?? WorkersConfig(numberOfWorkers: defaultNumWorkers),
  );

  return CompressionRsWorld.init(builder, imports: imports);
}