зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1805378 - [devtools] Convert WorkerDispatcher to ES Class. r=bomsy
Differential Revision: https://phabricator.services.mozilla.com/D164546
This commit is contained in:
Родитель
d044a504dc
Коммит
1083131754
|
@ -21,6 +21,7 @@ module.exports = {
|
|||
/[/\\]node_modules[/\\]devtools-/,
|
||||
/[/\\]node_modules[/\\]react-aria-components[/\\]/,
|
||||
"../../shared",
|
||||
"../shared/worker-utils.js",
|
||||
],
|
||||
presets: [
|
||||
"@babel/preset-react",
|
||||
|
|
|
@ -2503,7 +2503,7 @@
|
|||
"byName": {},
|
||||
"byBlocks": {},
|
||||
"usedIds": {
|
||||
"0": 0
|
||||
"1": 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2524,7 +2524,7 @@
|
|||
"byName": {},
|
||||
"byBlocks": {},
|
||||
"usedIds": {
|
||||
"0": 0
|
||||
"1": 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -76242,40 +76242,108 @@ exports.tokTypes = types;
|
|||
* file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
|
||||
|
||||
|
||||
function WorkerDispatcher() {
|
||||
this.msgId = 1;
|
||||
this.worker = null; // Map of message ids -> promise resolution functions, for dispatching worker responses
|
||||
function _classPrivateFieldInitSpec(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); }
|
||||
|
||||
this.pendingCalls = new Map();
|
||||
this._onMessage = this._onMessage.bind(this);
|
||||
}
|
||||
function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } }
|
||||
|
||||
function _classPrivateFieldGet(receiver, privateMap) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); return _classApplyDescriptorGet(receiver, descriptor); }
|
||||
|
||||
function _classApplyDescriptorGet(receiver, descriptor) { if (descriptor.get) { return descriptor.get.call(receiver); } return descriptor.value; }
|
||||
|
||||
function _classPrivateFieldSet(receiver, privateMap, value) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); _classApplyDescriptorSet(receiver, descriptor, value); return value; }
|
||||
|
||||
function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to " + action + " private field on non-instance"); } return privateMap.get(receiver); }
|
||||
|
||||
function _classApplyDescriptorSet(receiver, descriptor, value) { if (descriptor.set) { descriptor.set.call(receiver, value); } else { if (!descriptor.writable) { throw new TypeError("attempted to set read only private field"); } descriptor.value = value; } }
|
||||
|
||||
var _msgId = /*#__PURE__*/new WeakMap();
|
||||
|
||||
var _worker = /*#__PURE__*/new WeakMap();
|
||||
|
||||
var _pendingCalls = /*#__PURE__*/new WeakMap();
|
||||
|
||||
var _onMessage = /*#__PURE__*/new WeakMap();
|
||||
|
||||
class WorkerDispatcher {
|
||||
constructor() {
|
||||
_classPrivateFieldInitSpec(this, _msgId, {
|
||||
writable: true,
|
||||
value: 1
|
||||
});
|
||||
|
||||
_classPrivateFieldInitSpec(this, _worker, {
|
||||
writable: true,
|
||||
value: null
|
||||
});
|
||||
|
||||
_classPrivateFieldInitSpec(this, _pendingCalls, {
|
||||
writable: true,
|
||||
value: new Map()
|
||||
});
|
||||
|
||||
_classPrivateFieldInitSpec(this, _onMessage, {
|
||||
writable: true,
|
||||
value: ({
|
||||
data: result
|
||||
}) => {
|
||||
const items = _classPrivateFieldGet(this, _pendingCalls).get(result.id);
|
||||
|
||||
_classPrivateFieldGet(this, _pendingCalls).delete(result.id);
|
||||
|
||||
if (!items) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_classPrivateFieldGet(this, _worker)) {
|
||||
return;
|
||||
}
|
||||
|
||||
result.results.forEach((resultData, i) => {
|
||||
const {
|
||||
resolve,
|
||||
reject
|
||||
} = items[i];
|
||||
|
||||
if (resultData.error) {
|
||||
const err = new Error(resultData.message);
|
||||
err.metadata = resultData.metadata;
|
||||
reject(err);
|
||||
} else {
|
||||
resolve(resultData.response);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
WorkerDispatcher.prototype = {
|
||||
start(url) {
|
||||
// When running in debugger jest test, we don't have access to ChromeWorker
|
||||
if (typeof ChromeWorker == "function") {
|
||||
this.worker = new ChromeWorker(url);
|
||||
_classPrivateFieldSet(this, _worker, new ChromeWorker(url));
|
||||
} else {
|
||||
this.worker = new Worker(url);
|
||||
_classPrivateFieldSet(this, _worker, new Worker(url));
|
||||
}
|
||||
|
||||
this.worker.onerror = err => {
|
||||
_classPrivateFieldGet(this, _worker).onerror = err => {
|
||||
console.error(`Error in worker ${url}`, err.message);
|
||||
};
|
||||
|
||||
this.worker.addEventListener("message", this._onMessage);
|
||||
},
|
||||
_classPrivateFieldGet(this, _worker).addEventListener("message", _classPrivateFieldGet(this, _onMessage));
|
||||
}
|
||||
|
||||
stop() {
|
||||
if (!this.worker) {
|
||||
if (!_classPrivateFieldGet(this, _worker)) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.worker.removeEventListener("message", this._onMessage);
|
||||
this.worker.terminate();
|
||||
this.worker = null;
|
||||
this.pendingCalls.clear();
|
||||
},
|
||||
_classPrivateFieldGet(this, _worker).removeEventListener("message", _classPrivateFieldGet(this, _onMessage));
|
||||
|
||||
_classPrivateFieldGet(this, _worker).terminate();
|
||||
|
||||
_classPrivateFieldSet(this, _worker, null);
|
||||
|
||||
_classPrivateFieldGet(this, _pendingCalls).clear();
|
||||
}
|
||||
|
||||
task(method, {
|
||||
queue = false
|
||||
|
@ -76301,60 +76369,34 @@ WorkerDispatcher.prototype = {
|
|||
};
|
||||
|
||||
const flush = () => {
|
||||
var _this$msgId;
|
||||
|
||||
const items = calls.slice();
|
||||
calls.length = 0;
|
||||
|
||||
if (!this.worker) {
|
||||
if (!_classPrivateFieldGet(this, _worker)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const id = this.msgId++;
|
||||
this.worker.postMessage({
|
||||
const id = (_classPrivateFieldSet(this, _msgId, (_this$msgId = +_classPrivateFieldGet(this, _msgId)) + 1), _this$msgId);
|
||||
|
||||
_classPrivateFieldGet(this, _worker).postMessage({
|
||||
id,
|
||||
method,
|
||||
calls: items.map(item => item.args)
|
||||
});
|
||||
this.pendingCalls.set(id, items);
|
||||
|
||||
_classPrivateFieldGet(this, _pendingCalls).set(id, items);
|
||||
};
|
||||
|
||||
return (...args) => push(args);
|
||||
},
|
||||
}
|
||||
|
||||
invoke(method, ...args) {
|
||||
return this.task(method)(...args);
|
||||
},
|
||||
|
||||
_onMessage({
|
||||
data: result
|
||||
}) {
|
||||
const items = this.pendingCalls.get(result.id);
|
||||
this.pendingCalls.delete(result.id);
|
||||
|
||||
if (!items) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this.worker) {
|
||||
return;
|
||||
}
|
||||
|
||||
result.results.forEach((resultData, i) => {
|
||||
const {
|
||||
resolve,
|
||||
reject
|
||||
} = items[i];
|
||||
|
||||
if (resultData.error) {
|
||||
const err = new Error(resultData.message);
|
||||
err.metadata = resultData.metadata;
|
||||
reject(err);
|
||||
} else {
|
||||
resolve(resultData.response);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
function workerHandler(publicInterface) {
|
||||
return function (msg) {
|
||||
|
|
|
@ -88,40 +88,108 @@ return /******/ (function(modules) { // webpackBootstrap
|
|||
* file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
|
||||
|
||||
|
||||
function WorkerDispatcher() {
|
||||
this.msgId = 1;
|
||||
this.worker = null; // Map of message ids -> promise resolution functions, for dispatching worker responses
|
||||
function _classPrivateFieldInitSpec(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); }
|
||||
|
||||
this.pendingCalls = new Map();
|
||||
this._onMessage = this._onMessage.bind(this);
|
||||
}
|
||||
function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } }
|
||||
|
||||
function _classPrivateFieldGet(receiver, privateMap) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); return _classApplyDescriptorGet(receiver, descriptor); }
|
||||
|
||||
function _classApplyDescriptorGet(receiver, descriptor) { if (descriptor.get) { return descriptor.get.call(receiver); } return descriptor.value; }
|
||||
|
||||
function _classPrivateFieldSet(receiver, privateMap, value) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); _classApplyDescriptorSet(receiver, descriptor, value); return value; }
|
||||
|
||||
function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to " + action + " private field on non-instance"); } return privateMap.get(receiver); }
|
||||
|
||||
function _classApplyDescriptorSet(receiver, descriptor, value) { if (descriptor.set) { descriptor.set.call(receiver, value); } else { if (!descriptor.writable) { throw new TypeError("attempted to set read only private field"); } descriptor.value = value; } }
|
||||
|
||||
var _msgId = /*#__PURE__*/new WeakMap();
|
||||
|
||||
var _worker = /*#__PURE__*/new WeakMap();
|
||||
|
||||
var _pendingCalls = /*#__PURE__*/new WeakMap();
|
||||
|
||||
var _onMessage = /*#__PURE__*/new WeakMap();
|
||||
|
||||
class WorkerDispatcher {
|
||||
constructor() {
|
||||
_classPrivateFieldInitSpec(this, _msgId, {
|
||||
writable: true,
|
||||
value: 1
|
||||
});
|
||||
|
||||
_classPrivateFieldInitSpec(this, _worker, {
|
||||
writable: true,
|
||||
value: null
|
||||
});
|
||||
|
||||
_classPrivateFieldInitSpec(this, _pendingCalls, {
|
||||
writable: true,
|
||||
value: new Map()
|
||||
});
|
||||
|
||||
_classPrivateFieldInitSpec(this, _onMessage, {
|
||||
writable: true,
|
||||
value: ({
|
||||
data: result
|
||||
}) => {
|
||||
const items = _classPrivateFieldGet(this, _pendingCalls).get(result.id);
|
||||
|
||||
_classPrivateFieldGet(this, _pendingCalls).delete(result.id);
|
||||
|
||||
if (!items) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_classPrivateFieldGet(this, _worker)) {
|
||||
return;
|
||||
}
|
||||
|
||||
result.results.forEach((resultData, i) => {
|
||||
const {
|
||||
resolve,
|
||||
reject
|
||||
} = items[i];
|
||||
|
||||
if (resultData.error) {
|
||||
const err = new Error(resultData.message);
|
||||
err.metadata = resultData.metadata;
|
||||
reject(err);
|
||||
} else {
|
||||
resolve(resultData.response);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
WorkerDispatcher.prototype = {
|
||||
start(url) {
|
||||
// When running in debugger jest test, we don't have access to ChromeWorker
|
||||
if (typeof ChromeWorker == "function") {
|
||||
this.worker = new ChromeWorker(url);
|
||||
_classPrivateFieldSet(this, _worker, new ChromeWorker(url));
|
||||
} else {
|
||||
this.worker = new Worker(url);
|
||||
_classPrivateFieldSet(this, _worker, new Worker(url));
|
||||
}
|
||||
|
||||
this.worker.onerror = err => {
|
||||
_classPrivateFieldGet(this, _worker).onerror = err => {
|
||||
console.error(`Error in worker ${url}`, err.message);
|
||||
};
|
||||
|
||||
this.worker.addEventListener("message", this._onMessage);
|
||||
},
|
||||
_classPrivateFieldGet(this, _worker).addEventListener("message", _classPrivateFieldGet(this, _onMessage));
|
||||
}
|
||||
|
||||
stop() {
|
||||
if (!this.worker) {
|
||||
if (!_classPrivateFieldGet(this, _worker)) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.worker.removeEventListener("message", this._onMessage);
|
||||
this.worker.terminate();
|
||||
this.worker = null;
|
||||
this.pendingCalls.clear();
|
||||
},
|
||||
_classPrivateFieldGet(this, _worker).removeEventListener("message", _classPrivateFieldGet(this, _onMessage));
|
||||
|
||||
_classPrivateFieldGet(this, _worker).terminate();
|
||||
|
||||
_classPrivateFieldSet(this, _worker, null);
|
||||
|
||||
_classPrivateFieldGet(this, _pendingCalls).clear();
|
||||
}
|
||||
|
||||
task(method, {
|
||||
queue = false
|
||||
|
@ -147,60 +215,34 @@ WorkerDispatcher.prototype = {
|
|||
};
|
||||
|
||||
const flush = () => {
|
||||
var _this$msgId;
|
||||
|
||||
const items = calls.slice();
|
||||
calls.length = 0;
|
||||
|
||||
if (!this.worker) {
|
||||
if (!_classPrivateFieldGet(this, _worker)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const id = this.msgId++;
|
||||
this.worker.postMessage({
|
||||
const id = (_classPrivateFieldSet(this, _msgId, (_this$msgId = +_classPrivateFieldGet(this, _msgId)) + 1), _this$msgId);
|
||||
|
||||
_classPrivateFieldGet(this, _worker).postMessage({
|
||||
id,
|
||||
method,
|
||||
calls: items.map(item => item.args)
|
||||
});
|
||||
this.pendingCalls.set(id, items);
|
||||
|
||||
_classPrivateFieldGet(this, _pendingCalls).set(id, items);
|
||||
};
|
||||
|
||||
return (...args) => push(args);
|
||||
},
|
||||
}
|
||||
|
||||
invoke(method, ...args) {
|
||||
return this.task(method)(...args);
|
||||
},
|
||||
|
||||
_onMessage({
|
||||
data: result
|
||||
}) {
|
||||
const items = this.pendingCalls.get(result.id);
|
||||
this.pendingCalls.delete(result.id);
|
||||
|
||||
if (!items) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this.worker) {
|
||||
return;
|
||||
}
|
||||
|
||||
result.results.forEach((resultData, i) => {
|
||||
const {
|
||||
resolve,
|
||||
reject
|
||||
} = items[i];
|
||||
|
||||
if (resultData.error) {
|
||||
const err = new Error(resultData.message);
|
||||
err.metadata = resultData.metadata;
|
||||
reject(err);
|
||||
} else {
|
||||
resolve(resultData.response);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
function workerHandler(publicInterface) {
|
||||
return function (msg) {
|
||||
|
|
|
@ -88,40 +88,108 @@ return /******/ (function(modules) { // webpackBootstrap
|
|||
* file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
|
||||
|
||||
|
||||
function WorkerDispatcher() {
|
||||
this.msgId = 1;
|
||||
this.worker = null; // Map of message ids -> promise resolution functions, for dispatching worker responses
|
||||
function _classPrivateFieldInitSpec(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); }
|
||||
|
||||
this.pendingCalls = new Map();
|
||||
this._onMessage = this._onMessage.bind(this);
|
||||
}
|
||||
function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } }
|
||||
|
||||
function _classPrivateFieldGet(receiver, privateMap) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); return _classApplyDescriptorGet(receiver, descriptor); }
|
||||
|
||||
function _classApplyDescriptorGet(receiver, descriptor) { if (descriptor.get) { return descriptor.get.call(receiver); } return descriptor.value; }
|
||||
|
||||
function _classPrivateFieldSet(receiver, privateMap, value) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); _classApplyDescriptorSet(receiver, descriptor, value); return value; }
|
||||
|
||||
function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to " + action + " private field on non-instance"); } return privateMap.get(receiver); }
|
||||
|
||||
function _classApplyDescriptorSet(receiver, descriptor, value) { if (descriptor.set) { descriptor.set.call(receiver, value); } else { if (!descriptor.writable) { throw new TypeError("attempted to set read only private field"); } descriptor.value = value; } }
|
||||
|
||||
var _msgId = /*#__PURE__*/new WeakMap();
|
||||
|
||||
var _worker = /*#__PURE__*/new WeakMap();
|
||||
|
||||
var _pendingCalls = /*#__PURE__*/new WeakMap();
|
||||
|
||||
var _onMessage = /*#__PURE__*/new WeakMap();
|
||||
|
||||
class WorkerDispatcher {
|
||||
constructor() {
|
||||
_classPrivateFieldInitSpec(this, _msgId, {
|
||||
writable: true,
|
||||
value: 1
|
||||
});
|
||||
|
||||
_classPrivateFieldInitSpec(this, _worker, {
|
||||
writable: true,
|
||||
value: null
|
||||
});
|
||||
|
||||
_classPrivateFieldInitSpec(this, _pendingCalls, {
|
||||
writable: true,
|
||||
value: new Map()
|
||||
});
|
||||
|
||||
_classPrivateFieldInitSpec(this, _onMessage, {
|
||||
writable: true,
|
||||
value: ({
|
||||
data: result
|
||||
}) => {
|
||||
const items = _classPrivateFieldGet(this, _pendingCalls).get(result.id);
|
||||
|
||||
_classPrivateFieldGet(this, _pendingCalls).delete(result.id);
|
||||
|
||||
if (!items) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_classPrivateFieldGet(this, _worker)) {
|
||||
return;
|
||||
}
|
||||
|
||||
result.results.forEach((resultData, i) => {
|
||||
const {
|
||||
resolve,
|
||||
reject
|
||||
} = items[i];
|
||||
|
||||
if (resultData.error) {
|
||||
const err = new Error(resultData.message);
|
||||
err.metadata = resultData.metadata;
|
||||
reject(err);
|
||||
} else {
|
||||
resolve(resultData.response);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
WorkerDispatcher.prototype = {
|
||||
start(url) {
|
||||
// When running in debugger jest test, we don't have access to ChromeWorker
|
||||
if (typeof ChromeWorker == "function") {
|
||||
this.worker = new ChromeWorker(url);
|
||||
_classPrivateFieldSet(this, _worker, new ChromeWorker(url));
|
||||
} else {
|
||||
this.worker = new Worker(url);
|
||||
_classPrivateFieldSet(this, _worker, new Worker(url));
|
||||
}
|
||||
|
||||
this.worker.onerror = err => {
|
||||
_classPrivateFieldGet(this, _worker).onerror = err => {
|
||||
console.error(`Error in worker ${url}`, err.message);
|
||||
};
|
||||
|
||||
this.worker.addEventListener("message", this._onMessage);
|
||||
},
|
||||
_classPrivateFieldGet(this, _worker).addEventListener("message", _classPrivateFieldGet(this, _onMessage));
|
||||
}
|
||||
|
||||
stop() {
|
||||
if (!this.worker) {
|
||||
if (!_classPrivateFieldGet(this, _worker)) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.worker.removeEventListener("message", this._onMessage);
|
||||
this.worker.terminate();
|
||||
this.worker = null;
|
||||
this.pendingCalls.clear();
|
||||
},
|
||||
_classPrivateFieldGet(this, _worker).removeEventListener("message", _classPrivateFieldGet(this, _onMessage));
|
||||
|
||||
_classPrivateFieldGet(this, _worker).terminate();
|
||||
|
||||
_classPrivateFieldSet(this, _worker, null);
|
||||
|
||||
_classPrivateFieldGet(this, _pendingCalls).clear();
|
||||
}
|
||||
|
||||
task(method, {
|
||||
queue = false
|
||||
|
@ -147,60 +215,34 @@ WorkerDispatcher.prototype = {
|
|||
};
|
||||
|
||||
const flush = () => {
|
||||
var _this$msgId;
|
||||
|
||||
const items = calls.slice();
|
||||
calls.length = 0;
|
||||
|
||||
if (!this.worker) {
|
||||
if (!_classPrivateFieldGet(this, _worker)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const id = this.msgId++;
|
||||
this.worker.postMessage({
|
||||
const id = (_classPrivateFieldSet(this, _msgId, (_this$msgId = +_classPrivateFieldGet(this, _msgId)) + 1), _this$msgId);
|
||||
|
||||
_classPrivateFieldGet(this, _worker).postMessage({
|
||||
id,
|
||||
method,
|
||||
calls: items.map(item => item.args)
|
||||
});
|
||||
this.pendingCalls.set(id, items);
|
||||
|
||||
_classPrivateFieldGet(this, _pendingCalls).set(id, items);
|
||||
};
|
||||
|
||||
return (...args) => push(args);
|
||||
},
|
||||
}
|
||||
|
||||
invoke(method, ...args) {
|
||||
return this.task(method)(...args);
|
||||
},
|
||||
|
||||
_onMessage({
|
||||
data: result
|
||||
}) {
|
||||
const items = this.pendingCalls.get(result.id);
|
||||
this.pendingCalls.delete(result.id);
|
||||
|
||||
if (!items) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this.worker) {
|
||||
return;
|
||||
}
|
||||
|
||||
result.results.forEach((resultData, i) => {
|
||||
const {
|
||||
resolve,
|
||||
reject
|
||||
} = items[i];
|
||||
|
||||
if (resultData.error) {
|
||||
const err = new Error(resultData.message);
|
||||
err.metadata = resultData.metadata;
|
||||
reject(err);
|
||||
} else {
|
||||
resolve(resultData.response);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
function workerHandler(publicInterface) {
|
||||
return function (msg) {
|
||||
|
|
|
@ -4,38 +4,35 @@
|
|||
|
||||
"use strict";
|
||||
|
||||
function WorkerDispatcher() {
|
||||
this.msgId = 1;
|
||||
this.worker = null;
|
||||
class WorkerDispatcher {
|
||||
#msgId = 1;
|
||||
#worker = null;
|
||||
// Map of message ids -> promise resolution functions, for dispatching worker responses
|
||||
this.pendingCalls = new Map();
|
||||
this._onMessage = this._onMessage.bind(this);
|
||||
}
|
||||
#pendingCalls = new Map();
|
||||
|
||||
WorkerDispatcher.prototype = {
|
||||
start(url) {
|
||||
// When running in debugger jest test, we don't have access to ChromeWorker
|
||||
if (typeof ChromeWorker == "function") {
|
||||
this.worker = new ChromeWorker(url);
|
||||
this.#worker = new ChromeWorker(url);
|
||||
} else {
|
||||
this.worker = new Worker(url);
|
||||
this.#worker = new Worker(url);
|
||||
}
|
||||
this.worker.onerror = err => {
|
||||
this.#worker.onerror = err => {
|
||||
console.error(`Error in worker ${url}`, err.message);
|
||||
};
|
||||
this.worker.addEventListener("message", this._onMessage);
|
||||
},
|
||||
this.#worker.addEventListener("message", this.#onMessage);
|
||||
}
|
||||
|
||||
stop() {
|
||||
if (!this.worker) {
|
||||
if (!this.#worker) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.worker.removeEventListener("message", this._onMessage);
|
||||
this.worker.terminate();
|
||||
this.worker = null;
|
||||
this.pendingCalls.clear();
|
||||
},
|
||||
this.#worker.removeEventListener("message", this.#onMessage);
|
||||
this.#worker.terminate();
|
||||
this.#worker = null;
|
||||
this.#pendingCalls.clear();
|
||||
}
|
||||
|
||||
task(method, { queue = false } = {}) {
|
||||
const calls = [];
|
||||
|
@ -57,35 +54,35 @@ WorkerDispatcher.prototype = {
|
|||
const items = calls.slice();
|
||||
calls.length = 0;
|
||||
|
||||
if (!this.worker) {
|
||||
if (!this.#worker) {
|
||||
return;
|
||||
}
|
||||
|
||||
const id = this.msgId++;
|
||||
this.worker.postMessage({
|
||||
const id = this.#msgId++;
|
||||
this.#worker.postMessage({
|
||||
id,
|
||||
method,
|
||||
calls: items.map(item => item.args),
|
||||
});
|
||||
|
||||
this.pendingCalls.set(id, items);
|
||||
this.#pendingCalls.set(id, items);
|
||||
};
|
||||
|
||||
return (...args) => push(args);
|
||||
},
|
||||
}
|
||||
|
||||
invoke(method, ...args) {
|
||||
return this.task(method)(...args);
|
||||
},
|
||||
}
|
||||
|
||||
_onMessage({ data: result }) {
|
||||
const items = this.pendingCalls.get(result.id);
|
||||
this.pendingCalls.delete(result.id);
|
||||
#onMessage = ({ data: result }) => {
|
||||
const items = this.#pendingCalls.get(result.id);
|
||||
this.#pendingCalls.delete(result.id);
|
||||
if (!items) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this.worker) {
|
||||
if (!this.#worker) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -100,8 +97,8 @@ WorkerDispatcher.prototype = {
|
|||
resolve(resultData.response);
|
||||
}
|
||||
});
|
||||
},
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
function workerHandler(publicInterface) {
|
||||
return function(msg) {
|
||||
|
|
Загрузка…
Ссылка в новой задаче