1 |
|
2 | export interface Event {
|
3 | data: any;
|
4 | }
|
5 |
|
6 | export type CallBack = (...args: any[]) => any;
|
7 |
|
8 | export interface WorkerConstructor {
|
9 | new(jsfilepath: string): Worker;
|
10 | new(fun: (this: Worker) => void): Worker;
|
11 | new(): Worker;
|
12 | }
|
13 |
|
14 | export interface Worker {
|
15 |
|
16 | readonly thread: Thread;
|
17 |
|
18 | postMessage(data: any): void;
|
19 |
|
20 | onmessage(event: Event): void;
|
21 |
|
22 | terminate(): void;
|
23 |
|
24 | addEventListener(type: string, cb: CallBack): void;
|
25 |
|
26 | dispatchEvent(type: string);
|
27 |
|
28 | removeEventListener(type: string);
|
29 | }
|
30 |
|
31 | export interface Thread {
|
32 | readonly id: number;
|
33 |
|
34 | load(absolutePath: string, cb?: (this: Thread, err: any, data: any) => void): void;
|
35 |
|
36 |
|
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( ) destroys the thread.
|
52 | destroy(): this;
|
53 | }
|
54 |
|
55 | export 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 |
|
92 | export const Worker: WorkerConstructor;
|
93 |
|
94 | export function createPool(numThreads: number): ThreadPool;
|
95 |
|
96 | export function create(): Thread;
|
97 |
|
\ | No newline at end of file |