summaryrefslogtreecommitdiffstats
path: root/dom/workers/test/multi_sharedWorker_sharedWorker.js
blob: 3c3e4c5780dd8e125156003bcba7b2b26abbbe4c (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
 * Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */
"use strict";

if (self.name != "FrameWorker") {
  throw new Error("Bad worker name: " + self.name);
}

var registeredPorts = [];
var errorCount = 0;
var storedData;

self.onconnect = function (event) {
  var port = event.ports[0];

  if (registeredPorts.length) {
    let data = {
      type: "connect",
    };

    registeredPorts.forEach(function (registeredPort) {
      registeredPort.postMessage(data);
    });
  }

  port.onmessage = function (msg) {
    switch (msg.data.command) {
      case "start":
        break;

      case "error":
        throw new Error("Expected");

      case "store":
        storedData = msg.data.data;
        break;

      case "retrieve":
        var data = {
          type: "result",
          data: storedData,
        };
        port.postMessage(data);
        break;

      default:
        throw new Error("Unknown command '" + error.data.command + "'");
    }
  };

  registeredPorts.push(port);
};

self.onerror = function (message, filename, lineno) {
  if (!errorCount++) {
    var data = {
      type: "worker-error",
      message,
      filename,
      lineno,
    };

    registeredPorts.forEach(function (registeredPort) {
      registeredPort.postMessage(data);
    });

    // Prevent the error from propagating the first time only.
    return true;
  }
  return undefined;
};