mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-10-05 23:37:04 +00:00
(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:
committed by
Nathan Shively-Sanders
parent
bbc89b4aba
commit
63c76e1060
Vendored
+14
-5
@@ -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;
|
||||
};
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user