The broker which is used by the worker-timers package.
In most cases using this package via worker-timers
is probably the most convenient choice.
However worker-timers-broker
is published as a separate package on npm. It can be installed using the following command:
npm install worker-timers-broker
The package exports two functions.
The load()
function can be used to create a custom instance of worker-timers
by explicitly specifying the URL which points to the code of the worker-timers-worker
package.
import { load } from 'worker-timers-broker';
const { clearInterval, clearTimeout, setInterval, setTimeout } = load('./the/worker-timers-worker/file');
const intervalId = setInterval(() => {
// do something many times
}, 100);
clearInterval(intervalId);
const timeoutId = setTimeout(() => {
// do something once
}, 100);
clearTimeout(timeoutId);
The wrap()
function can be used to wrap a Web Worker instance which already runs the code of the worker-timers-worker
package.
In a project using Vite it can for example be used by leveraging query suffixes.
import { wrap } from 'worker-timers-broker';
import createWorkerTimersWorker from 'worker-timers-worker?worker';
const { clearInterval, clearTimeout, setInterval, setTimeout } = wrap(createWorkerTimersWorker());
const intervalId = setInterval(() => {
// do something many times
}, 100);
clearInterval(intervalId);
const timeoutId = setTimeout(() => {
// do something once
}, 100);
clearTimeout(timeoutId);
Please note the syntax for other build tools may vary.
To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.