How to use the comlink.wrap function in comlink

To help you get started, we’ve selected a few comlink examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github wasmerio / wasmer-js / examples / wapm-shell / services / command-runner / command-runner.ts View on Github external
async spawnProcess(commandOptionIndex: number) {
    // First set up our fallback if we need to
    let binaryenScript = "";
    if (this.isUsingFallback && this.binaryenScriptPromise) {
      binaryenScript = await this.binaryenScriptPromise;
    }

    // Generate our process
    const processWorker = new Worker("./workers/process/process.worker.js");
    const processComlink = Comlink.wrap(processWorker);

    // @ts-ignore
    const process: any = await new processComlink(
      this.commandOptionsForProcessesToRun[commandOptionIndex],
      // Data Callback
      Comlink.proxy((data: Uint8Array) => {
        if (
          commandOptionIndex <
          this.commandOptionsForProcessesToRun.length - 1
        ) {
          // Pass along to the next spawned process
          if (this.spawnedProcessToWorker.length > 1) {
            this.spawnedProcessToWorker[1].process.receiveStdinChunk(data);
          } else {
            const newInitialStdinData = new Uint8Array(
              data.length + this.initialStdinDataForNextProcess.length
github wasmerio / wasmer-js / packages / wasm-terminal / src / command-runner / command-runner.ts View on Github external
if (!this.wasmTerminalConfig.processWorkerUrl) {
      throw new Error("Terminal Config missing the Process Worker URL");
    }

    let processWorkerUrl = this.wasmTerminalConfig.processWorkerUrl;
    /*ROLLUP_REPLACE_INLINE
    processWorkerUrl = processWorkerInlinedUrl;
    ROLLUP_REPLACE_INLINE*/

    // Generate our process
    const workerBlobUrl = await this._getBlobUrlForProcessWorker(
      processWorkerUrl,
      this.wasmTty
    );
    const processWorker = new Worker(workerBlobUrl);
    const processComlink = Comlink.wrap(processWorker);

    // Generate our shared buffer
    const sharedStdinBuffer = new SharedArrayBuffer(8192);

    // Get our filesystem state
    const wasmFsJson = this.wasmTerminalConfig.wasmFs.toJSON();

    // @ts-ignore
    const process: any = await new processComlink(
      // Command Options
      this.commandOptionsForProcessesToRun[commandOptionIndex],
      // WasmFs File System JSON
      wasmFsJson,
      // Data Callback
      Comlink.proxy(
        this._processDataCallback.bind(this, {
github wasmerio / wasmer-js / packages / wasm-terminal / src / command-runner / command-runner.ts View on Github external
if (!this.wasmTerminalConfig.processWorkerUrl) {
      throw new Error("Terminal Config missing the Process Worker URL");
    }

    let processWorkerUrl = this.wasmTerminalConfig.processWorkerUrl;
    /*ROLLUP_REPLACE_INLINE
    processWorkerUrl = processWorkerInlinedUrl;
    ROLLUP_REPLACE_INLINE*/

    // Generate our process
    const workerBlobUrl = await this._getBlobUrlForProcessWorker(
      processWorkerUrl,
      this.wasmTty
    );
    const processWorker = new Worker(workerBlobUrl);
    const processComlink = Comlink.wrap(processWorker);

    // Generate our shared buffer
    const sharedStdinBuffer = new SharedArrayBuffer(8192);

    // Get our filesystem state
    const wasmFsJson = this.wasmTerminalConfig.wasmFs.toJSON();

    // Create our Io Device Window
    const ioDeviceWindow = new IoDeviceWindow();

    // @ts-ignore
    const process: any = await new processComlink(
      // Command Options
      this.commandOptionsForProcessesToRun[commandOptionIndex],
      // WasmFs File System JSON
      wasmFsJson,
github transcend-io / penumbra / src / workers.ts View on Github external
export async function createPenumbraWorker(
  url: URL | string,
): Promise {
  const worker = new Worker(url /* , { type: 'module' } */);
  const penumbraWorker: PenumbraWorker = {
    worker,
    comlink: wrap(worker),
    initialized: false,
  };
  const Link = penumbraWorker.comlink;
  const setup = new Link().then(async (thread: PenumbraWorkerAPI) => {
    await thread.setup(proxy(reDispatchEvent));
  });
  await setup;
  penumbraWorker.initialized = true;
  return penumbraWorker;
}
/** Initializes web worker threads */
github casual-simulation / aux / src / aux-vm-browser / partitions / ProxyClientPartition.ts View on Github external
constructor(config: ProxyClientPartitionConfig) {
        this._bridge = wrap(config.port);
        this.private = config.private;

        console.log('Got Bridge: ', this._bridge);

        this.state = {};

        this._onBotsAdded = new Subject();
        this._onBotsRemoved = new Subject();
        this._onBotsUpdated = new Subject();
        this._onError = new Subject();
        this._onEvents = new Subject();
        this._onStatusUpdated = new Subject();
    }
github parcel-bundler / parcel / packages / core / repl / src / parcel / index.js View on Github external
import {wrap as ComlinkWrap} from 'comlink';

const ParcelWorker = ComlinkWrap(new Worker('./ParcelWorker.js'));
export const workerLoaded = new ParcelWorker();

export async function getFS() {
  return (await workerLoaded).getFS();
}

export async function getZip() {
  return (await workerLoaded).getZip();
}

export default async function bundle(assets, options) {
  return (await workerLoaded).bundle(assets, options);
}
github ritz078 / raaga / utils / MidiPlayer / MidiPlayer.ts View on Github external
VISUALIZER_MESSAGES,
  VISUALIZER_MODE
} from "@enums/visualizerMessages";
import {
  getDelay,
  getNotesWithNoteEndEvent,
  NoteWithIdAndEvent
} from "@utils/MidiPlayer/MidiPlayer.utils";
import { Range } from "@utils/typings/Visualizer";
import { getInstrumentIdByValue } from "midi-instruments";
import { IMidiJSON } from "@typings/midi";
import { MidiSettings } from "@components/TrackList";
import { wrap } from "comlink";
import { OFFSCREEN_2D_CANVAS_SUPPORT } from "@enums/offscreen2dCanvasSupport";

const loadInstrumentWorker: any = wrap(new LoadInstrumentWorker());

export type IScheduleOptions = MidiSettings;

type IEventCallback = (
  notes: NoteWithIdAndEvent[],
  trackIndex: number,
  isLastEvent?: boolean
) => void;

export interface Sampler {
  connect: (master: any) => void;
  triggerAttack: (note: string, time?: number, velocity?: number) => void;
  triggerRelease: (note: string) => void;
  add: (key: string, buffer: ArrayBuffer, cb: () => void) => void;
  context: AudioContext;
  dispose: () => void;

comlink

Comlink makes WebWorkers enjoyable

Apache-2.0
Latest version published 1 year ago

Package Health Score

71 / 100
Full package analysis

Similar packages