Bug 1921345 - Remove global constructor from js/src/vm/SavedStacks.cpp r=arai
[gecko.git] / devtools / shared / transport / worker-transport.js
blob903fd69cf4e050ec279daa6d75a2569a5e9b13b5
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 "use strict";
7 // Each worker debugger supports only a single connection to the main thread.
8 // However, its theoretically possible for multiple servers to connect to the
9 // same worker. Consequently, each transport has a connection id, to allow
10 // messages from multiple connections to be multiplexed on a single channel.
12 /**
13  * A transport that uses a WorkerDebugger to send packets from the main
14  * thread to a worker thread.
15  */
16 class MainThreadWorkerDebuggerTransport {
17   constructor(dbg, id) {
18     this._dbg = dbg;
19     this._id = id;
21     this._dbgListener = {
22       onMessage: this._onMessage.bind(this),
23     };
24   }
26   ready() {
27     this._dbg.addListener(this._dbgListener);
28   }
30   close() {
31     if (this._dbgListener) {
32       this._dbg.removeListener(this._dbgListener);
33     }
34     this._dbgListener = null;
35     this.hooks?.onTransportClosed();
36   }
38   send(packet) {
39     this._dbg.postMessage(
40       JSON.stringify({
41         type: "message",
42         id: this._id,
43         message: packet,
44       })
45     );
46   }
48   startBulkSend() {
49     throw new Error("Can't send bulk data from worker threads!");
50   }
52   _onMessage(message) {
53     const packet = JSON.parse(message);
54     if (packet.type !== "message" || packet.id !== this._id || !this.hooks) {
55       return;
56     }
58     this.hooks.onPacket(packet.message);
59   }
62 exports.MainThreadWorkerDebuggerTransport = MainThreadWorkerDebuggerTransport;
64 /**
65  * A transport that uses a WorkerDebuggerGlobalScope to send packets from a
66  * worker thread to the main thread.
67  */
68 function WorkerThreadWorkerDebuggerTransport(scope, id) {
69   this._scope = scope;
70   this._id = id;
71   this._onMessage = this._onMessage.bind(this);
74 WorkerThreadWorkerDebuggerTransport.prototype = {
75   constructor: WorkerThreadWorkerDebuggerTransport,
77   ready() {
78     this._scope.addEventListener("message", this._onMessage);
79   },
81   close() {
82     this._scope.removeEventListener("message", this._onMessage);
83     this.hooks?.onTransportClosed();
84   },
86   send(packet) {
87     this._scope.postMessage(
88       JSON.stringify({
89         type: "message",
90         id: this._id,
91         message: packet,
92       })
93     );
94   },
96   startBulkSend() {
97     throw new Error("Can't send bulk data from worker threads!");
98   },
100   _onMessage(event) {
101     const packet = JSON.parse(event.data);
102     if (packet.type !== "message" || packet.id !== this._id) {
103       return;
104     }
106     if (this.hooks) {
107       this.hooks.onPacket(packet.message);
108     }
109   },
112 exports.WorkerThreadWorkerDebuggerTransport =
113   WorkerThreadWorkerDebuggerTransport;