How to use comlink - 10 common examples

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 / packages / wasm-terminal / src / command-runner / command-runner.ts View on Github external
// Data Callback
      Comlink.proxy(
        this._processDataCallback.bind(this, {
          commandOptionIndex,
          sync: false
        })
      ),
      // End Callback
      Comlink.proxy(
        this._processEndCallback.bind(this, {
          commandOptionIndex,
          processWorker
        })
      ),
      // Error Callback
      Comlink.proxy(
        this._processErrorCallback.bind(this, { commandOptionIndex })
      ),
      // Shared Array Bufer
      sharedStdinBuffer,
      // Stdin read callback
      Comlink.proxy(this._processStartStdinReadCallback.bind(this))
    );

    // Initialize the shared Stdin.
    // Index 0 will be number of elements in buffer
    const sharedStdin = new Int32Array(sharedStdinBuffer);
    sharedStdin[0] = -1;

    return {
      process,
      commandOptionIndex,
github wasmerio / wasmer-js / packages / wasm-terminal / src / command-runner / command-runner.ts View on Github external
),
      // End Callback
      Comlink.proxy(
        this._processEndCallback.bind(this, {
          commandOptionIndex,
          processWorker
        })
      ),
      // Error Callback
      Comlink.proxy(
        this._processErrorCallback.bind(this, { commandOptionIndex })
      ),
      // Shared Array Bufer
      sharedStdinBuffer,
      // Stdin read callback
      Comlink.proxy(this._processStartStdinReadCallback.bind(this))
    );

    // Initialize the shared Stdin.
    // Index 0 will be number of elements in buffer
    const sharedStdin = new Int32Array(sharedStdinBuffer);
    sharedStdin[0] = -1;

    return {
      process,
      commandOptionIndex,
      worker: processWorker,
      sharedStdin: sharedStdin
    };
  }
github wasmerio / wasmer-js / examples / wapm-shell / services / command-runner / command-runner.ts View on Github external
if (
          commandOptionIndex <
          this.commandOptionsForProcessesToRun.length - 1
        ) {
          // Try to spawn the next process, if we haven't already
          this.tryToSpawnProcess(commandOptionIndex + 1);
        } else {
          // We are now done!
          // Call the passed end callback
          this.isRunning = false;
          this.endCallback();
        }
      }),
      // Error Callback
      Comlink.proxy((error: string) => {
        this.xterm.write(
          `Program ${this.commandOptionsForProcessesToRun[commandOptionIndex].args[0]}: ${error}\r\n`
        );
        this.kill();
        this.endCallback();
      }),
      // Stdin
      this.initialStdinDataForNextProcess.length > 0
        ? this.initialStdinDataForNextProcess
        : undefined,
      // Binary Script (Fallback case)
      this.isUsingFallback ? binaryenScript : undefined
    );

    // Remove the initial stdin if we added it
    if (this.initialStdinDataForNextProcess.length > 0) {
github wasmerio / wasmer-js / packages / wasm-terminal / src / command-runner / command-runner.ts View on Github external
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,
      // Data Callback
      Comlink.proxy(
        this._processDataCallback.bind(this, {
          commandOptionIndex,
          sync: false
        })
      ),
      // End Callback
      Comlink.proxy(
        this._processEndCallback.bind(this, {
          commandOptionIndex,
          processWorker
        })
      ),
      // Error Callback
      Comlink.proxy(
        this._processErrorCallback.bind(this, { commandOptionIndex })
      ),
github wasmerio / wasmer-js / packages / wasm-terminal / src / command-runner / command-runner.ts View on Github external
// Data Callback
      Comlink.proxy(
        this._processDataCallback.bind(this, {
          commandOptionIndex,
          sync: false
        })
      ),
      // End Callback
      Comlink.proxy(
        this._processEndCallback.bind(this, {
          commandOptionIndex,
          processWorker
        })
      ),
      // Error Callback
      Comlink.proxy(
        this._processErrorCallback.bind(this, { commandOptionIndex })
      ),
      // Io Device Window
      Comlink.proxy(ioDeviceWindow),
      // Shared Array Bufer
      sharedStdinBuffer,
      // Stdin read callback
      Comlink.proxy(this._processStartStdinReadCallback.bind(this))
    );

    // Initialize the shared Stdin.
    // Index 0 will be number of elements in buffer
    const sharedStdin = new Int32Array(sharedStdinBuffer);
    sharedStdin[0] = -1;

    return {
github wasmerio / wasmer-js / packages / wasm-terminal / src / command-runner / command-runner.ts View on Github external
// @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, {
          commandOptionIndex,
          sync: false
        })
      ),
      // End Callback
      Comlink.proxy(
        this._processEndCallback.bind(this, {
          commandOptionIndex,
          processWorker
        })
      ),
      // Error Callback
      Comlink.proxy(
        this._processErrorCallback.bind(this, { commandOptionIndex })
      ),
      // Io Device Window
      Comlink.proxy(ioDeviceWindow),
      // Shared Array Bufer
      sharedStdinBuffer,
      // Stdin read callback
      Comlink.proxy(this._processStartStdinReadCallback.bind(this))
    );
github wasmerio / wasmer-js / packages / wasm-terminal / src / command-runner / command-runner.ts View on Github external
// @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, {
          commandOptionIndex,
          sync: false
        })
      ),
      // End Callback
      Comlink.proxy(
        this._processEndCallback.bind(this, {
          commandOptionIndex,
          processWorker
        })
      ),
      // Error Callback
      Comlink.proxy(
        this._processErrorCallback.bind(this, { commandOptionIndex })
      ),
      // Shared Array Bufer
      sharedStdinBuffer,
      // Stdin read callback
      Comlink.proxy(this._processStartStdinReadCallback.bind(this))
    );

    // Initialize the shared Stdin.
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,

comlink

Comlink makes WebWorkers enjoyable

Apache-2.0
Latest version published 14 days ago

Package Health Score

92 / 100
Full package analysis

Similar packages