Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 4x 1x 1x | import { expose, Remote } from 'comlink';
import { TypeOf } from '../utility/type-of';
/**
* Type alias describing an exposed class in a remote context. Represented by
* wrapping a {@link Remote} in a {@link Promise}. Used and intended to be used
* in conjunction with the {@link Thread} decorator.
*
* @typeParam T - The {@link Remote} **Thread** type.
*
* @see {@link Thread}
*/
export type Thread<T> = Promise<Remote<T>>;
/**
* Class decorator factory. {@link expose}s an instance of the decorated class
* as {@link Worker} **Thread**.
*
* @returns A class constructor decorator.
*
* @example
* ExampleWorker **Thread**:
* ```ts
* import { Thread } from '@sgrud/core';
*
* @Thread()
* export class ExampleWorker {}
* ```
*
* @see {@link Spawn}
*/
export function Thread() {
/**
* @param constructor - The class `constructor` to be decorated.
* @throws A {@link ReferenceError} when the environment is incompatible.
*/
return function(constructor: new () => any): void {
if (TypeOf.function(globalThis.importScripts)) {
expose(new constructor());
} else if (TypeOf.process(globalThis.process)) {
const { isMainThread, parentPort } = require('worker_threads');
if (!isMainThread) {
const nodeEndpoint = require('comlink/dist/umd/node-adapter');
expose(new constructor(), nodeEndpoint(parentPort));
} else {
throw new TypeError(constructor.name);
}
} else {
throw new TypeError(constructor.name);
}
};
}
|