包详细信息

troika-worker-utils

protectwise2mMIT0.52.0

Utilities for executing code in Web Workers

自述文件

troika-worker-utils

This package provides utilities for making Web Workers easier to use.

Worker Modules

Troika's "Worker Modules" system, exposed by the defineWorkerModule export, allows you to define a function that will be executed with a web worker. This provides a simple interface for moving chunks of logic off the main thread, which can be critical in WebGL/WebXR scenes where frame rate cannot be interrupted by long-running code.

Similar utilities like Greenlet have existed for a while. However, defineWorkerModule introduces the ability for worker modules to depend on one another. This means you can define modular chunks of code in separate functions, and then inject them into a worker where they can reference and call each other.

defineWorkerModule(options)

This function defines a Worker Module. It takes an options object that can contain the following:

options.init

Required. This is the main function that initializes the module; it will be executed within the Worker the first time it is invoked. If any dependencies are defined, the resolved values of those dependencies will be passed in as arguments.

Its return value becomes the module's "value". That can be:

  • A function, which can be called any number of times from the main thread by calling the function returned from defineWorkerModule().

  • Any other value, which will be used as the value passed to the init of other worker modules using it as a dependency.

Note: As with any function-in-worker utility, the init function must not use any variables from the parent closure in which it is defined; its internal content must be completely standalone. Any external values you want to use must be passed in as dependencies.

options.dependencies

An optional array of dependencies required by the init function. Dependencies can be:

  • Primitives like strings, numbers, booleans

  • Functions; these will be stringified and rehydrated within the worker so they must not depend on anything from their parent closures

  • Other worker modules created by defineWorkerModule; these will be resolved within the worker, and therefore modules that provide functions can be called without having to cross the worker/main thread boundary.

options.getTransferables

An optional function that will be run in the worker just before posting the response value from a module call back to the main thread. This function will be passed that response value, and if it returns an array then that will be used as the "transferables" parameter to postMessage. Use this if there are values in the response that can/should be transfered rather than cloned.

options.name

An optional descriptive name for this module; this can be useful for debugging (it will be inserted as a comment into the Blob sent to the worker) but is not currently used for anything else.

options.workerId

By default all modules will run in the same dedicated worker, but if you want to use multiple workers you can pass a workerId string to indicate a specific worker to spawn. Note that each worker is completely standalone and no data or state will be shared between them. If a worker module is used as a dependency by worker modules using different workerIds, then that dependency will be re-registered in each worker.

Return Value

The value returned by defineWorkerModule is a function. If your options.init returned a function, then this will be how you can invoke that within the worker. Call it, and it will give you a Promise for its return value.

Contrived Example

import { defineWorkerModule } from 'troika-worker-utils'

// A simple module with a value:
const workerModuleA = defineWorkerModule({
  init: function() {
    return Math.PI
  }
})

// A module that depends on the previous module:
const workerModuleB = defineWorkerModule({
  dependencies: [
    workerModuleA
  ],
  init: function(moduleAValue) {
    // moduleAValue here is "I'm the value of Module A!" from the first init function

    // This return function can be invoked by calling workerModuleB in the main thread:
    let callCount = 0
    return function(arg) {
      return `Called module B ${++callCount} times, ` 
        + `with arg "${arg}". Module A's value was ${moduleAValue}.`
    }
  }
})

workerModuleB('foo') // "Called module B 1 times, with arg "foo". Module A's value was 3.141592653589793."
workerModuleB('bar') // "Called module B 2 times, with arg "bar". Module A's value was 3.141592653589793."

更新日志

Change Log

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

0.52.0 (2024-11-11)

Note: Version bump only for package troika-worker-utils

0.51.1 (2024-11-11)

Bug Fixes

  • troika-worker-utils: Avoid no-workers log errors when forcing main thread (d396e51), closes #337

0.51.0 (2024-11-10)

Features

  • troika-worker-utils: Add a handle to force running worker modules on main thread (4f8bc13)

0.50.0 (2024-10-11)

Note: Version bump only for package troika-worker-utils

0.49.0 (2023-10-08)

Note: Version bump only for package troika-worker-utils

0.48.0 (2023-09-09)

Note: Version bump only for package troika-worker-utils

0.47.2 (2023-05-15)

Note: Version bump only for package troika-worker-utils

0.47.0 (2022-12-15)

Features

  • remove custom Thenable polyfill in favor of native promises (7af402e)

0.46.0 (2022-03-05)

Note: Version bump only for package troika-worker-utils

0.45.0 (2022-01-02)

Note: Version bump only for package troika-worker-utils

0.44.0 (2021-11-14)

Note: Version bump only for package troika-worker-utils

0.43.0 (2021-09-20)

Bug Fixes

  • remove warnings about many open worker module requests (164fb8f), closes #156

Features

  • troika-worker-utils: add a terminateWorker function (33b8455)

0.42.0 (2021-05-17)

Note: Version bump only for package troika-worker-utils

0.41.0 (2021-04-19)

Note: Version bump only for package troika-worker-utils

0.40.0 (2021-02-28)

Note: Version bump only for package troika-worker-utils

0.39.0 (2021-02-15)

Note: Version bump only for package troika-worker-utils

0.38.1 (2021-02-03)

Bug Fixes

  • troika-worker-utils: properly track open requests count (a01d903)

0.38.0 (2021-01-24)

Note: Version bump only for package troika-worker-utils

0.37.0 (2021-01-18)

Note: Version bump only for package troika-worker-utils

0.36.0 (2020-12-04)

Note: Version bump only for package troika-worker-utils

0.35.0 (2020-11-16)

Note: Version bump only for package troika-worker-utils

0.34.1 (2020-10-20)

Bug Fixes

  • check for process env 'test' (4f7f8f2)
  • check if process is undefined (2b6d56a)

0.34.0 (2020-10-19)

Note: Version bump only for package troika-worker-utils

0.33.0 (2020-10-02)

Bug Fixes

  • add "sideEffects":false to package.json files to assist treeshaking (61109b2)
  • add PURE annotations to make troika-three-text treeshakeable (8e76b5c)
  • remove redundant "browser" and defunct "jsnext:main" fields from package.json files (0abec40)

0.32.0 (2020-09-16)

Note: Version bump only for package troika-worker-utils

0.31.0 (2020-08-11)

Note: Version bump only for package troika-worker-utils

0.30.0 (2020-07-16)

Bug Fixes

  • troika-worker-utils: decrease main thread message level from warn to log (d7cee6d)

0.29.0 (2020-07-06)

Note: Version bump only for package troika-worker-utils

0.28.0 (2020-06-09)

Note: Version bump only for package troika-worker-utils

0.27.0 (2020-06-02)

Note: Version bump only for package troika-worker-utils

0.26.1 (2020-05-26)

Bug Fixes

  • troika-worker-modules: silence fallback warning in non-browser environments (3dedb8f)

0.26.0 (2020-05-24)

Features

  • troika-worker-utils: add main thread fallback when web workers are not allowed (c754d0b)

0.25.0 (2020-05-19)

Note: Version bump only for package troika-worker-utils

0.24.0 (2020-04-27)

Note: Version bump only for package troika-worker-utils

0.23.0 (2020-04-16)

Features

  • troika-worker-modules: improve rehydration of functions in worker (8f63090), closes #31

0.22.0 (2020-04-02)

Note: Version bump only for package troika-worker-utils

0.21.0 (2020-03-27)

Note: Version bump only for package troika-worker-utils

0.20.0 (2020-03-16)

Features

  • troika-worker-utils: export function for stringifying functions (977634b)

0.19.0 (2020-02-28)

Note: Version bump only for package troika-worker-utils

0.18.0 (2020-02-21)

Note: Version bump only for package troika-worker-utils