|
| 1 | + |
| 2 | +/** |
| 3 | + * Used to create, destroy and communicate with worker threads |
| 4 | + */ |
| 5 | +declare class Worker { |
| 6 | + |
| 7 | + /** |
| 8 | + * Creates a new worker with the given main script. |
| 9 | + * @param script The first module to be loaded on the worker thread. |
| 10 | + */ |
| 11 | + public constructor(script: string); |
| 12 | + |
| 13 | + /** |
| 14 | + * Sends message to the worker thread. |
| 15 | + * @param message The message to be serialized and sent to the worker thread. |
| 16 | + */ |
| 17 | + public postMessage(message: any) : void; |
| 18 | + |
| 19 | + /** |
| 20 | + * Terminates the execution the worker thread without calling `onclose` handler. |
| 21 | + */ |
| 22 | + public terminate() : void; |
| 23 | + |
| 24 | + /** |
| 25 | + * Called by the runtime when a new message is received by the worker instance. |
| 26 | + */ |
| 27 | + public onmessage : Worker.OnMessageHandler; |
| 28 | + |
| 29 | + /** |
| 30 | + * Called by the runtime when an uncaught error is propagated to the parent thread. |
| 31 | + */ |
| 32 | + public onerror : Worker.OnErrorHandler; |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * Exists only in worker context. Returns the worker global object. |
| 37 | + */ |
| 38 | +declare var self: any; |
| 39 | + |
| 40 | +/** |
| 41 | + * Exists only in worker context. It is called by the runtime when a new message is received by the worker thread. |
| 42 | + */ |
| 43 | +declare var onmessage : Worker.OnMessageHandler; |
| 44 | + |
| 45 | +/** |
| 46 | + * Exists only in worker context. Handles uncaught errors in the worker thread. If return false the error is propagated to the parent context and passed to Worker.onerror handler. |
| 47 | + */ |
| 48 | +declare var onerror : Worker.OnErrorHandler; |
| 49 | + |
| 50 | +/** |
| 51 | + * Exists only in worker context. Called before the worker is closed with the close() function. |
| 52 | + */ |
| 53 | +declare var onclose : Worker.OnCloseHandler; |
| 54 | + |
| 55 | +/** |
| 56 | + * Exists only in worker context. Sends message to the parent thread. |
| 57 | + */ |
| 58 | +declare function postMessage(message: any) : void; |
| 59 | + |
| 60 | +/** |
| 61 | + * Exists only in worker context. Closes the worker thread on the next run loop tick. |
| 62 | + */ |
| 63 | +declare function close() : void; |
| 64 | + |
| 65 | +declare namespace Worker { |
| 66 | + |
| 67 | + interface MessageEvent { |
| 68 | + data: any; |
| 69 | + } |
| 70 | + |
| 71 | + interface ErrorEvent { |
| 72 | + message: string; |
| 73 | + filename: string; |
| 74 | + lineno: number; |
| 75 | + } |
| 76 | + |
| 77 | + interface OnErrorHandler { |
| 78 | + (error: ErrorEvent): boolean; |
| 79 | + } |
| 80 | + |
| 81 | + interface OnMessageHandler { |
| 82 | + (message: MessageEvent): void; |
| 83 | + } |
| 84 | + |
| 85 | + interface OnCloseHandler { |
| 86 | + (): void; |
| 87 | + } |
| 88 | +} |
0 commit comments