mwg API
    Preparing search index...

    Function openWorker

    • Opens a worker running a classic script, in a browser (a blob worker, which the shipped file:// page's policy allows) or in Node (worker_threads in eval mode), and returns null where neither exists. spawn is the one-task-per-worker use of this; a long-lived service such as the audio core host keeps its worker and talks to it with messages.

      The script must be self-contained for the same reason a spawn task must: it runs with nothing but its own text in scope.

      Parameters

      Returns WorkerLink | null

      import { openWorker } from '@datamoc/mwg/threads';

      const link = openWorker('globalThis.onmessage = (event) => globalThis.postMessage(event.data * 2);', {
      message: (value) => console.log(value),
      fail: (error) => console.warn(error.message),
      });
      link?.post(21);