Files
palemoon27/toolkit/devtools/shared/worker-helper.js
T
roytam1 7529dc68e8 import changes from `dev' branch of rmottola/Arctic-Fox:
- import VMX Blur from TenFourFox and adapt moz.build with HAVE_ALTIVEC (f23006226)
- Bug 1176083. Remove the now-dead code for the XPCOM version of setTimeout/setInterval. r=smaug (af84a61a6)
- Bug 1148593 - Pass JSContext to CallbackObject constructor. r=bz (548cda0cc)
- Bug 1164564 - Clean up the worker loader;r=jlong (c22a9241c)
- Bug 1164564 - Refactor Promise-backend.js so it can be required as a CommonJS module;r=jlong (6ced4a7f2)
- Bug 1084525 - Part 4: Add listPromises method to PromisesActor r=fitzgen (132c6b062)
- Bug 1084525 - Part 5: Add onNewPromise event handler to PromisesActor r=fitzgen (07098d58c)
- Bug 1084525 - Part 6: Add onPromiseSettled event handler to PromisesActor r=fitzgen (11d0c7430)
- Bug 1164483 - move worker helpers from frontend of devtools to backend r=jsantell (d5a0d16af)
- Bug 1164632 - use new worker helpers in debugger for pretty-printing r=jsantell (de356582a)
- do not run a test program to see if -maltivec is supported, since GCC always accepts it, even if an incompatible processor is selected. Offer explicit enable-altivec option and also set CXXFLAGS (c78bc8e43)
- extend removing of optimizations to all newer GCC versions, since it is a code issue, not compiler one (29639f4d5)
2021-04-19 11:02:56 +08:00

113 lines
3.1 KiB
JavaScript

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
(function (root, factory) {
"use strict";
if (typeof define === "function" && define.amd) {
define(factory);
} else if (typeof exports === "object") {
module.exports = factory();
} else {
root.workerHelper = factory();
}
}(this, function () {
"use strict";
/**
* This file is to only be included by ChromeWorkers. This exposes
* a `createTask` function to workers to register tasks for communication
* back to `devtools/toolkit/shared/worker`.
*
* Tasks can be send their responses via a return value, either a primitive
* or a promise.
*
* createTask(self, "average", function (data) {
* return data.reduce((sum, val) => sum + val, 0) / data.length;
* });
*
* createTask(self, "average", function (data) {
* return new Promise((resolve, reject) => {
* resolve(data.reduce((sum, val) => sum + val, 0) / data.length);
* });
* });
*
*
* Errors:
*
* Returning an Error value, or if the returned promise is rejected, this
* propagates to the DevToolsWorker as a rejected promise. If an error is
* thrown in a synchronous function, that error is also propagated.
*/
/**
* Takes a worker's `self` object, a task name, and a function to
* be called when that task is called. The task is called with the
* passed in data as the first argument
*
* @param {object} self
* @param {string} name
* @param {function} fn
*/
function createTask (self, name, fn) {
// Store a hash of task name to function on the Worker
if (!self._tasks) {
self._tasks = {};
}
// Create the onmessage handler if not yet created.
if (!self.onmessage) {
self.onmessage = createHandler(self);
}
// Store the task on the worker.
self._tasks[name] = fn;
}
/**
* Creates the `self.onmessage` handler for a Worker.
*
* @param {object} self
* @return {function}
*/
function createHandler (self) {
return function (e) {
let { id, task, data } = e.data;
let taskFn = self._tasks[task];
if (!taskFn) {
self.postMessage({ id, error: `Task "${task}" not found in worker.` });
return;
}
try {
let results;
handleResponse(taskFn(data));
} catch (e) {
handleError(e);
}
function handleResponse (response) {
// If a promise
if (response && typeof response.then === "function") {
response.then(val => self.postMessage({ id, response: val }), handleError);
}
// If an error object
else if (response instanceof Error) {
handleError(response);
}
// If anything else
else {
self.postMessage({ id, response });
}
}
function handleError (e="Error") {
self.postMessage({ id, error: e.message || e });
}
}
}
return { createTask: createTask };
}.bind(this)));