summaryrefslogtreecommitdiffstats
path: root/dom/base/test/worker_postMessages.js
blob: cc3d69ece4b30cb3426573adcd63b593a22f3d33 (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
function test_workers() {
  onmessage = function(e) {
    postMessage(e.data, e.ports);
  };

  onmessageerror = function(e) {
    postMessage("error");
  };
}

function test_sharedWorkers(port) {
  port.onmessage = function(e) {
    if (e.data == "terminate") {
      close();
    } else {
      port.postMessage(e.data, e.ports);
    }
  };

  port.onmessageerror = function(e) {
    port.postMessage("error");
  };
}

function test_broadcastChannel(obj) {
  var bc = new BroadcastChannel("postMessagesTest_inWorkers");
  bc.onmessage = function(e) {
    obj.postMessage(e.data);
  };

  bc.onmessageerror = function() {
    obj.postMessage("error");
  };
}

function test_messagePort(port) {
  port.onmessage = function(e) {
    postMessage(e.data, e.ports);
  };

  port.onmessageerror = function(e) {
    postMessage("error");
  };
}

onconnect = function(e) {
  e.ports[0].onmessage = ee => {
    if (ee.data == "sharedworkers") {
      test_sharedWorkers(e.ports[0]);
      e.ports[0].postMessage("ok");
    } else if (ee.data == "broadcastChannel") {
      test_broadcastChannel(e.ports[0]);
      e.ports[0].postMessage("ok");
    } else if (ee.data == "terminate") {
      close();
    }
  };
};

onmessage = function(e) {
  if (e.data == "workers") {
    test_workers();
    postMessage("ok");
  } else if (e.data == "broadcastChannel") {
    test_broadcastChannel(self);
    postMessage("ok");
  } else if (e.data == "messagePort") {
    test_messagePort(e.ports[0]);
    postMessage("ok");
  } else {
    postMessage("ko");
  }
};