UNPKG

4.34 kBTypeScriptView Raw
1
2export interface Event {
3 data: any;
4}
5
6export type CallBack = (...args: any[]) => any;
7
8export interface WorkerConstructor {
9 new(jsfilepath: string): Worker;
10 new(fun: (this: Worker) => void): Worker;
11 new(): Worker;
12}
13
14export interface Worker {
15 // Returns the underlying thread object; see the next section for details. Note that this attribute is implementation-specific, and not part of W3C Web Worker API.
16 readonly thread: Thread;
17 // worker.postMessage({ x: 1, y: 2 }) sends a data structure into the worker. The worker can receive it using the onmessage handler.
18 postMessage(data: any): void;
19 // worker.onmessage = function (event) { console.log(event.data) }; receives data from the worker's postMessage calls.
20 onmessage(event: Event): void;
21 // terminates the worker thread.
22 terminate(): void;
23 // worker.addEventListener('message', callback) is equivalent to setting worker.onmesssage = callback.
24 addEventListener(type: string, cb: CallBack): void;
25 // Currently unimplemented.
26 dispatchEvent(type: string);
27 // Currently unimplemented.
28 removeEventListener(type: string);
29}
30
31export interface Thread {
32 readonly id: number;
33 // thread.load( absolutePath [, cb] ) reads the file at absolutePath and thread.eval(fileContents, cb).
34 load(absolutePath: string, cb?: (this: Thread, err: any, data: any) => void): void;
35
36 // thread.eval( program [, cb]) converts program.toString() and eval()s it in the thread's global context, and (if provided) returns the completion value to cb(err, completionValue).
37 eval<T extends { toString(): string; }>(program: T, cb?: (this: Thread, err: any, data: any) => void): this;
38
39 // thread.on( eventType, listener ) registers the listener listener(data) for any events of eventType that the thread thread may emit.
40 on(eventType: string, listener: (data: any) => void): this;
41
42 // thread.once(eventType, listener) is like thread.on(), but the listener will only be called once.
43 once(eventType: string, listener: (data: any) => void): this;
44
45 // thread.removeAllListeners([eventType]) deletes all listeners for all eventTypes.If eventType is provided, deletes all listeners only for the event type eventType.
46 removeAllListeners(eventType?: string): this;
47
48 // thread.emit(eventType, eventData[, eventData ... ]) emits an event of eventType with eventData inside the thread thread.All its arguments are.toString()ed.
49 emit(eventType: string, ...eventDatas: any[]): this;
50
51 // thread.destroy( /* no arguments */) destroys the thread.
52 destroy(): this;
53}
54
55export interface ThreadPool {
56 // threadPool.load( absolutePath [, cb] ) runs thread.load( absolutePath [, cb] ) in all the pool's threads.
57 load(absolutePath: string, cb?: (this: Thread, err: any, data: any) => void): this;
58
59 readonly any: {
60 // threadPool.any.eval( program, cb ) is like thread.eval(), but in any of the pool's threads.
61 eval(program: any, cb?: (this: Thread, err: any, data: any) => void): ThreadPool;
62
63 // threadPool.any.emit( eventType, eventData [, eventData ... ] ) is like thread.emit(), but in any of the pool's threads.
64 emit(eventType: string, ...eventData: any[]): ThreadPool;
65 }
66
67 readonly all: {
68 // threadPool.all.eval( program, cb ) is like thread.eval(), but in all the pool's threads.
69 eval(program: any, cb?: (this: Thread, err: any, data: any) => void): ThreadPool;
70
71 // threadPool.all.emit( eventType, eventData [, eventData ... ] ) is like thread.emit(), but in all the pool's threads.
72 emit(eventType: string, ...eventData: any[]): ThreadPool;
73 }
74
75
76 // threadPool.on( eventType, listener ) is like thread.on(), but in all of the pool's threads.
77 on(eventType: string, listener: CallBack): this;
78
79 // threadPool.totalThreads() returns the number of threads in this pool: as supplied in .createPool( number )
80 totalThreads(): number;
81
82 // threadPool.idleThreads() returns the number of threads in this pool that are currently idle (sleeping)
83 idleThreads(): number;
84
85 // threadPool.pendingJobs() returns the number of jobs pending.
86 pendingJobs(): number;
87
88 // threadPool.destroy( [ rudely ] ) waits until pendingJobs() is zero and then destroys the pool. If rudely is truthy, then it doesn't wait for pendingJobs === 0.
89 destroy(rudely?: boolean): void;
90}
91
92export const Worker: WorkerConstructor;
93
94export function createPool(numThreads: number): ThreadPool;
95
96export function create(): Thread;
97
\No newline at end of file