summaryrefslogtreecommitdiffstats
path: root/toolkit/components/telemetry/tests/unit/file_UninstallPing.worker.js
blob: fcc8007a2fd88b4407387c3520db69e322a27af3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/. */

/* eslint-env mozilla/chrome-worker */

/* import-globals-from /toolkit/components/workerloader/require.js */
importScripts("resource://gre/modules/workers/require.js");

const PromiseWorker = require("resource://gre/modules/workers/PromiseWorker.js");

const Agent = {
  _file: null,
  open(path) {
    this._file = IOUtils.openFileForSyncReading(path);
  },
  close() {
    this._file.close();
  },
};

// This boilerplate connects the PromiseWorker to the Agent so
// that messages from the main thread map to methods on the
// Agent.
const worker = new PromiseWorker.AbstractWorker();
worker.dispatch = function (method, args = []) {
  return Agent[method](...args);
};
worker.postMessage = function (result, ...transfers) {
  self.postMessage(result, ...transfers);
};
worker.close = function () {
  self.close();
};
self.addEventListener("message", msg => worker.handleMessage(msg));
self.addEventListener("unhandledrejection", function (error) {
  throw error.reason;
});