(feat): add sharedworker options (#40069)

package: sharedworker
in addition to the url string, sharedworker constructor takes as an argument a name string or an options object.
reference: https://developer.mozilla.org/en-US/docs/Web/API/SharedWorker/SharedWorker
This commit is contained in:
Mohamed Boughaba
2019-11-06 09:03:39 -08:00
committed by Nathan Shively-Sanders
parent bbc89b4aba
commit 63c76e1060
2 changed files with 25 additions and 11 deletions
+14 -5
View File
@@ -1,8 +1,9 @@
// Type definitions for SharedWorker
// Project: http://www.w3.org/TR/workers/
// Definitions by: Toshiya Nakakura <https://github.com/nakakura>
// M. Boughaba <https://github.com/mboughaba>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.8
// TypeScript Version: 3.0
declare namespace SharedWorker {
interface AbstractWorker extends EventTarget {
@@ -23,12 +24,20 @@ declare namespace SharedWorker {
}
}
interface SharedWorkerOptions {
credentials?: RequestCredentials;
name?: string;
type?: WorkerType;
}
declare var SharedWorker: {
prototype: SharedWorker.SharedWorker;
/***
/**
*
* @param {string} stringUrl Pathname to JavaScript file
* @param {string} name Name of the worker to execute
* @param {string} stringUrl Pathname to JavaScript file
* @param {string|SharedWorkerOptions} [options] Name of the worker to execute
* or an object containing option properties
*/
new(stringUrl: string, name?: string): SharedWorker.SharedWorker;
new(stringUrl: string, options?: string | SharedWorkerOptions): SharedWorker.SharedWorker;
};
+11 -6
View File
@@ -1,25 +1,30 @@
var worker_with_name = new SharedWorker('worker.js', "worker");
var worker_with_name = new SharedWorker('worker.js', 'worker');
var worker_with_opts = new SharedWorker('worker.js', {
name: 'worker',
type: 'module',
credentials: 'same-origin'
});
/* Caller */
let worker = new SharedWorker('worker.js');
worker.port.start();
worker.port.onmessage = function (e) {
worker.port.onmessage = function(e) {
console.log('Caller Received:', e.data);
}
};
worker.port.postMessage('Message');
/* Worker */
const _self: SharedWorker.SharedWorkerGlobalScope = self as any;
_self.onconnect = function (e) {
_self.onconnect = function(e) {
var port = e.ports[0];
port.addEventListener('message', function (e) {
port.addEventListener('message', function(e) {
console.log('Worker Received', e.data);
port.postMessage('Result');
});
port.start();
}
};