UNPKG

4.96 kBTypeScriptView Raw
1/// <reference lib="dom" />
2import { Observable } from "observable-fns";
3import { ObservablePromise } from "../observable-promise";
4import { $errors, $events, $terminate, $worker } from "../symbols";
5import { TransferDescriptor } from "../transferable";
6interface ObservableLikeSubscription {
7 unsubscribe(): any;
8}
9interface ObservableLike<T> {
10 subscribe(onNext: (value: T) => any, onError?: (error: any) => any, onComplete?: () => any): ObservableLikeSubscription;
11 subscribe(listeners: {
12 next?(value: T): any;
13 error?(error: any): any;
14 complete?(): any;
15 }): ObservableLikeSubscription;
16}
17export declare type StripAsync<Type> = Type extends Promise<infer PromiseBaseType> ? PromiseBaseType : Type extends ObservableLike<infer ObservableBaseType> ? ObservableBaseType : Type;
18export declare type StripTransfer<Type> = Type extends TransferDescriptor<infer BaseType> ? BaseType : Type;
19export declare type ModuleMethods = {
20 [methodName: string]: (...args: any) => any;
21};
22export declare type ProxyableArgs<Args extends any[]> = Args extends [arg0: infer Arg0, ...rest: infer RestArgs] ? [Arg0 extends Transferable ? Arg0 | TransferDescriptor<Arg0> : Arg0, ...RestArgs] : Args;
23export declare type ProxyableFunction<Args extends any[], ReturnType> = Args extends [] ? () => ObservablePromise<StripTransfer<StripAsync<ReturnType>>> : (...args: ProxyableArgs<Args>) => ObservablePromise<StripTransfer<StripAsync<ReturnType>>>;
24export declare type ModuleProxy<Methods extends ModuleMethods> = {
25 [method in keyof Methods]: ProxyableFunction<Parameters<Methods[method]>, ReturnType<Methods[method]>>;
26};
27export interface PrivateThreadProps {
28 [$errors]: Observable<Error>;
29 [$events]: Observable<WorkerEvent>;
30 [$terminate]: () => Promise<void>;
31 [$worker]: Worker;
32}
33export declare type FunctionThread<Args extends any[] = any[], ReturnType = any> = ProxyableFunction<Args, ReturnType> & PrivateThreadProps;
34export declare type ModuleThread<Methods extends ModuleMethods = any> = ModuleProxy<Methods> & PrivateThreadProps;
35interface AnyFunctionThread extends PrivateThreadProps {
36 (...args: any[]): ObservablePromise<any>;
37}
38interface AnyModuleThread extends PrivateThreadProps {
39}
40/** Worker thread. Either a `FunctionThread` or a `ModuleThread`. */
41export declare type Thread = AnyFunctionThread | AnyModuleThread;
42export declare type TransferList = Transferable[];
43/** Worker instance. Either a web worker or a node.js Worker provided by `worker_threads` or `tiny-worker`. */
44export interface Worker extends EventTarget {
45 postMessage(value: any, transferList?: TransferList): void;
46 /** In nodejs 10+ return type is Promise while with tiny-worker and in browser return type is void */
47 terminate(callback?: (error?: Error, exitCode?: number) => void): void | Promise<number>;
48}
49export interface ThreadsWorkerOptions extends WorkerOptions {
50 /** Prefix for the path passed to the Worker constructor. Web worker only. */
51 _baseURL?: string;
52 /** Resource limits passed on to Node worker_threads */
53 resourceLimits?: {
54 /** The maximum size of the main heap in MB. */
55 maxOldGenerationSizeMb?: number;
56 /** The maximum size of a heap space for recently created objects. */
57 maxYoungGenerationSizeMb?: number;
58 /** The size of a pre-allocated memory range used for generated code. */
59 codeRangeSizeMb?: number;
60 };
61 /** Data passed on to node.js worker_threads. */
62 workerData?: any;
63 /** Whether to apply CORS protection workaround. Defaults to true. */
64 CORSWorkaround?: boolean;
65}
66/** Worker implementation. Either web worker or a node.js Worker class. */
67export declare class WorkerImplementation extends EventTarget implements Worker {
68 constructor(path: string, options?: ThreadsWorkerOptions);
69 postMessage(value: any, transferList?: TransferList): void;
70 terminate(): void | Promise<number>;
71}
72/** Class to spawn workers from a blob or source string. */
73export declare class BlobWorker extends WorkerImplementation {
74 constructor(blob: Blob, options?: ThreadsWorkerOptions);
75 static fromText(source: string, options?: ThreadsWorkerOptions): WorkerImplementation;
76}
77export interface ImplementationExport {
78 blob: typeof BlobWorker;
79 default: typeof WorkerImplementation;
80}
81/** Event as emitted by worker thread. Subscribe to using `Thread.events(thread)`. */
82export declare enum WorkerEventType {
83 internalError = "internalError",
84 message = "message",
85 termination = "termination"
86}
87export interface WorkerInternalErrorEvent {
88 type: WorkerEventType.internalError;
89 error: Error;
90}
91export interface WorkerMessageEvent<Data> {
92 type: WorkerEventType.message;
93 data: Data;
94}
95export interface WorkerTerminationEvent {
96 type: WorkerEventType.termination;
97}
98export declare type WorkerEvent = WorkerInternalErrorEvent | WorkerMessageEvent<any> | WorkerTerminationEvent;
99export {};