2015-05-06 02:39:48 +03:00
|
|
|
/* 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/. */
|
|
|
|
|
2017-01-04 01:47:28 +03:00
|
|
|
/* eslint-env amd */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2018-03-12 21:24:38 +03:00
|
|
|
(function(root, factory) {
|
2015-05-21 19:06:32 +03:00
|
|
|
if (typeof define === "function" && define.amd) {
|
|
|
|
define(factory);
|
|
|
|
} else if (typeof exports === "object") {
|
|
|
|
module.exports = factory();
|
|
|
|
} else {
|
|
|
|
root.workerHelper = factory();
|
2015-05-06 02:39:48 +03:00
|
|
|
}
|
2018-03-12 21:24:38 +03:00
|
|
|
})(this, function() {
|
2015-05-21 19:06:32 +03:00
|
|
|
/**
|
|
|
|
* This file is to only be included by ChromeWorkers. This exposes
|
|
|
|
* a `createTask` function to workers to register tasks for communication
|
2015-10-07 23:40:24 +03:00
|
|
|
* back to `devtools/shared/worker`.
|
2015-05-21 19:06:32 +03:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2015-05-06 02:39:48 +03:00
|
|
|
|
2015-05-21 19:06:32 +03:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2015-09-03 01:16:29 +03:00
|
|
|
function createTask(self, name, fn) {
|
2015-05-21 19:06:32 +03:00
|
|
|
// Store a hash of task name to function on the Worker
|
|
|
|
if (!self._tasks) {
|
|
|
|
self._tasks = {};
|
2015-05-06 02:39:48 +03:00
|
|
|
}
|
|
|
|
|
2015-05-21 19:06:32 +03:00
|
|
|
// Create the onmessage handler if not yet created.
|
|
|
|
if (!self.onmessage) {
|
|
|
|
self.onmessage = createHandler(self);
|
2015-05-06 02:39:48 +03:00
|
|
|
}
|
|
|
|
|
2015-05-21 19:06:32 +03:00
|
|
|
// Store the task on the worker.
|
|
|
|
self._tasks[name] = fn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the `self.onmessage` handler for a Worker.
|
|
|
|
*
|
|
|
|
* @param {object} self
|
|
|
|
* @return {function}
|
|
|
|
*/
|
2015-09-03 01:16:29 +03:00
|
|
|
function createHandler(self) {
|
2018-03-12 21:24:38 +03:00
|
|
|
return function(e) {
|
2018-06-01 13:36:09 +03:00
|
|
|
const { id, task, data } = e.data;
|
|
|
|
const taskFn = self._tasks[task];
|
2015-05-21 19:06:32 +03:00
|
|
|
|
|
|
|
if (!taskFn) {
|
|
|
|
self.postMessage({ id, error: `Task "${task}" not found in worker.` });
|
|
|
|
return;
|
2015-05-06 02:39:48 +03:00
|
|
|
}
|
2015-05-21 19:06:32 +03:00
|
|
|
|
|
|
|
try {
|
|
|
|
handleResponse(taskFn(data));
|
2017-01-04 01:47:28 +03:00
|
|
|
} catch (ex) {
|
|
|
|
handleError(ex);
|
2015-05-06 02:39:48 +03:00
|
|
|
}
|
2015-05-21 19:06:32 +03:00
|
|
|
|
2015-09-03 01:16:29 +03:00
|
|
|
function handleResponse(response) {
|
2015-05-21 19:06:32 +03:00
|
|
|
// If a promise
|
|
|
|
if (response && typeof response.then === "function") {
|
|
|
|
response.then(
|
|
|
|
val => self.postMessage({ id, response: val }),
|
|
|
|
handleError
|
|
|
|
);
|
2017-01-04 01:47:28 +03:00
|
|
|
} else if (response instanceof Error) {
|
|
|
|
// If an error object
|
2015-05-21 19:06:32 +03:00
|
|
|
handleError(response);
|
2017-01-04 01:47:28 +03:00
|
|
|
} else {
|
|
|
|
// If anything else
|
2015-05-21 19:06:32 +03:00
|
|
|
self.postMessage({ id, response });
|
|
|
|
}
|
2015-05-06 02:39:48 +03:00
|
|
|
}
|
|
|
|
|
2016-05-17 21:25:54 +03:00
|
|
|
function handleError(error = "Error") {
|
2015-09-03 01:16:29 +03:00
|
|
|
try {
|
|
|
|
// First, try and structured clone the error across directly.
|
|
|
|
self.postMessage({ id, error });
|
2017-01-04 01:47:28 +03:00
|
|
|
} catch (x) {
|
2015-09-03 01:16:29 +03:00
|
|
|
// We could not clone whatever error value was given. Do our best to
|
|
|
|
// stringify it.
|
|
|
|
let errorString = `Error while performing task "${task}": `;
|
|
|
|
|
|
|
|
try {
|
|
|
|
errorString += error.toString();
|
2017-01-04 01:47:28 +03:00
|
|
|
} catch (ex) {
|
2015-09-03 01:16:29 +03:00
|
|
|
errorString += "<could not stringify error>";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ("stack" in error) {
|
|
|
|
try {
|
|
|
|
errorString += "\n" + error.stack;
|
2017-01-04 01:47:28 +03:00
|
|
|
} catch (err) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
2015-09-03 01:16:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
self.postMessage({ id, error: errorString });
|
|
|
|
}
|
2015-05-21 19:06:32 +03:00
|
|
|
}
|
2015-09-03 01:16:29 +03:00
|
|
|
};
|
2015-05-06 02:39:48 +03:00
|
|
|
}
|
2015-05-21 19:06:32 +03:00
|
|
|
|
2017-01-12 01:21:56 +03:00
|
|
|
return { createTask: createTask };
|
2017-01-04 01:47:28 +03:00
|
|
|
});
|