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/. */
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.
13 * A transport that uses a WorkerDebugger to send packets from the main
14 * thread to a worker thread.
16 class MainThreadWorkerDebuggerTransport {
17 constructor(dbg, id) {
22 onMessage: this._onMessage.bind(this),
27 this._dbg.addListener(this._dbgListener);
31 if (this._dbgListener) {
32 this._dbg.removeListener(this._dbgListener);
34 this._dbgListener = null;
35 this.hooks?.onTransportClosed();
39 this._dbg.postMessage(
49 throw new Error("Can't send bulk data from worker threads!");
53 const packet = JSON.parse(message);
54 if (packet.type !== "message" || packet.id !== this._id || !this.hooks) {
58 this.hooks.onPacket(packet.message);
62 exports.MainThreadWorkerDebuggerTransport = MainThreadWorkerDebuggerTransport;
65 * A transport that uses a WorkerDebuggerGlobalScope to send packets from a
66 * worker thread to the main thread.
68 function WorkerThreadWorkerDebuggerTransport(scope, id) {
71 this._onMessage = this._onMessage.bind(this);
74 WorkerThreadWorkerDebuggerTransport.prototype = {
75 constructor: WorkerThreadWorkerDebuggerTransport,
78 this._scope.addEventListener("message", this._onMessage);
82 this._scope.removeEventListener("message", this._onMessage);
83 this.hooks?.onTransportClosed();
87 this._scope.postMessage(
97 throw new Error("Can't send bulk data from worker threads!");
101 const packet = JSON.parse(event.data);
102 if (packet.type !== "message" || packet.id !== this._id) {
107 this.hooks.onPacket(packet.message);
112 exports.WorkerThreadWorkerDebuggerTransport =
113 WorkerThreadWorkerDebuggerTransport;