From 1c2ab2adc96eeb122cdb2beea131b84428a9f8ca Mon Sep 17 00:00:00 2001 From: ivanbuhov Date: Fri, 14 Oct 2016 18:16:09 +0300 Subject: [PATCH] Add typescript declarations for workers --- gruntfile.js | 5 +- tns-core-modules/declarations.d.ts | 2 +- tns-core-modules/tns-core-modules.es2016.d.ts | 1 + tns-core-modules/webworker.es2016.d.ts | 88 +++++++++++++++++++ 4 files changed, 93 insertions(+), 3 deletions(-) create mode 100644 tns-core-modules/webworker.es2016.d.ts diff --git a/gruntfile.js b/gruntfile.js index 01fd941df..710e97235 100644 --- a/gruntfile.js +++ b/gruntfile.js @@ -115,7 +115,8 @@ module.exports = function(grunt) { "!tns-core-modules.es6.d.ts", "!tns-core-modules.es2016.d.ts", "!tns-core-modules.base.d.ts", - "!references.d.ts" + "!references.d.ts", + "!webworker.es2016.d.ts" ].concat(localCfg.defaultExcludes).concat(es6Excludes).concat(angularExcludes)); dtsFiles.sort(); @@ -266,7 +267,7 @@ module.exports = function(grunt) { "!android17.d.ts", "!**/*.android.d.ts", "!ios.d.ts", - "!**/*.ios.d.ts", + "!**/*.ios.d.ts" ].concat(localCfg.defaultExcludes), dest: localCfg.outDir + "/", expand: true, diff --git a/tns-core-modules/declarations.d.ts b/tns-core-modules/declarations.d.ts index dff7efe59..506f356cc 100644 --- a/tns-core-modules/declarations.d.ts +++ b/tns-core-modules/declarations.d.ts @@ -165,4 +165,4 @@ declare var exports: any; interface Array { filter(pred: (a: T) => a is U): U[]; -} \ No newline at end of file +} diff --git a/tns-core-modules/tns-core-modules.es2016.d.ts b/tns-core-modules/tns-core-modules.es2016.d.ts index 8efc3f411..9a163f341 100644 --- a/tns-core-modules/tns-core-modules.es2016.d.ts +++ b/tns-core-modules/tns-core-modules.es2016.d.ts @@ -1,4 +1,5 @@ /// +/// /// /// diff --git a/tns-core-modules/webworker.es2016.d.ts b/tns-core-modules/webworker.es2016.d.ts new file mode 100644 index 000000000..a9ba4538a --- /dev/null +++ b/tns-core-modules/webworker.es2016.d.ts @@ -0,0 +1,88 @@ + +/** + * Used to create, destroy and communicate with worker threads + */ +declare class Worker { + + /** + * Creates a new worker with the given main script. + * @param script The first module to be loaded on the worker thread. + */ + public constructor(script: string); + + /** + * Sends message to the worker thread. + * @param message The message to be serialized and sent to the worker thread. + */ + public postMessage(message: any) : void; + + /** + * Terminates the execution the worker thread without calling `onclose` handler. + */ + public terminate() : void; + + /** + * Called by the runtime when a new message is received by the worker instance. + */ + public onmessage : Worker.OnMessageHandler; + + /** + * Called by the runtime when an uncaught error is propagated to the parent thread. + */ + public onerror : Worker.OnErrorHandler; +} + +/** + * Exists only in worker context. Returns the worker global object. + */ +declare var self: any; + +/** + * Exists only in worker context. It is called by the runtime when a new message is received by the worker thread. + */ +declare var onmessage : Worker.OnMessageHandler; + +/** + * 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. + */ +declare var onerror : Worker.OnErrorHandler; + +/** + * Exists only in worker context. Called before the worker is closed with the close() function. + */ +declare var onclose : Worker.OnCloseHandler; + +/** + * Exists only in worker context. Sends message to the parent thread. + */ +declare function postMessage(message: any) : void; + +/** + * Exists only in worker context. Closes the worker thread on the next run loop tick. + */ +declare function close() : void; + +declare namespace Worker { + + interface MessageEvent { + data: any; + } + + interface ErrorEvent { + message: string; + filename: string; + lineno: number; + } + + interface OnErrorHandler { + (error: ErrorEvent): boolean; + } + + interface OnMessageHandler { + (message: MessageEvent): void; + } + + interface OnCloseHandler { + (): void; + } +}