Skip to content

Worker Helper

This file provides a helper function for creating worker instances in the @sodacore/core library. It bootstraps the Sodacore worker wrapper, which loads the specified module and sets up inter-process communication (IPC).


Features

  • Worker Creation: Simplifies the process of spawning a new worker using the Sodacore worker wrapper.
  • IPC Setup: Ensures the worker is initialized with the correct arguments for module loading and communication.

Usage

typescript
import { createWorker } from './helper/worker';

const worker = createWorker('my-worker', '/path/to/worker-module.js');
  • The worker will be started using the Sodacore worker wrapper and will load the specified module.

API

createWorker(name: string, path: string): Worker

  • name: The name to assign to the worker.
  • path: The path to the worker module to be loaded.
  • Returns: A new Worker instance.

How It Works

  • Constructs the worker wrapper URL using PATH_WORKER_WRAPPER and the current module URL.
  • Creates a new Worker with the specified name and passes the module path as an argument (--file=...).
  • Returns the created worker process.

Notes

  • The worker wrapper script is responsible for loading the actual worker module and setting up IPC.
  • This helper abstracts away the details of worker initialization and argument passing.

Released under the Apache-2.0 License.