Backed out changeset f594e6f00208 (bug 1940883) for causing crashes in bug 1941164.
[gecko.git] / toolkit / components / promiseworker / tests / xpcshell / data / worker.js
blob065ab1765a326d7701aace5a33ddd17d6e10126a
1 /* Any copyright is dedicated to the Public Domain.
2  * http://creativecommons.org/publicdomain/zero/1.0/ */
4 "use strict";
6 // Trivial worker definition
8 /* import-globals-from /toolkit/components/workerloader/require.js */
9 importScripts("resource://gre/modules/workers/require.js");
10 var PromiseWorker = require("resource://gre/modules/workers/PromiseWorker.js");
12 var worker = new PromiseWorker.AbstractWorker();
14 worker.dispatch = async function (method, args = []) {
15   return await Agent[method](...args);
17 worker.postMessage = function (...args) {
18   self.postMessage(...args);
20 worker.close = function () {
21   self.close();
23 worker.log = function (...args) {
24   dump("Worker: " + args.join(" ") + "\n");
26 self.addEventListener("message", msg => worker.handleMessage(msg));
27 self.addEventListener("unhandledrejection", function (error) {
28   throw error.reason;
29 });
31 var Agent = {
32   bounce(...args) {
33     return args;
34   },
36   async bounceWithExtraCalls(...args) {
37     let result = await worker.callMainThread("echo", [
38       "Posting something unrelated",
39     ]);
40     args.push(result.ok);
41     return args;
42   },
44   throwError(msg) {
45     throw new Error(msg);
46   },