| 9 | // Port class implementation |
| 10 | class Port { |
| 11 | constructor(name = '') { |
| 12 | this.name = name; |
| 13 | this.sender = null; |
| 14 | this._otherPort = null; |
| 15 | this._isConnected = true; |
| 16 | |
| 17 | // Event handlers storage |
| 18 | this._onMessageHandlers = []; |
| 19 | this._onDisconnectHandlers = []; |
| 20 | |
| 21 | // Event capturing functionality |
| 22 | this._captureEnabled = false; |
| 23 | this._capturedEvents = []; |
| 24 | |
| 25 | // Create event objects with addListener/removeListener methods |
| 26 | this.onMessage = { |
| 27 | addListener: callback => { |
| 28 | if (typeof callback === 'function') { |
| 29 | this._onMessageHandlers.push(callback); |
| 30 | } |
| 31 | }, |
| 32 | removeListener: callback => { |
| 33 | const index = this._onMessageHandlers.indexOf(callback); |
| 34 | if (index > -1) { |
| 35 | this._onMessageHandlers.splice(index, 1); |
| 36 | } |
| 37 | } |
| 38 | }; |
| 39 | |
| 40 | this.onDisconnect = { |
| 41 | addListener: callback => { |
| 42 | if (typeof callback === 'function') { |
| 43 | this._onDisconnectHandlers.push(callback); |
| 44 | } |
| 45 | }, |
| 46 | removeListener: callback => { |
| 47 | const index = this._onDisconnectHandlers.indexOf(callback); |
| 48 | if (index > -1) { |
| 49 | this._onDisconnectHandlers.splice(index, 1); |
| 50 | } |
| 51 | } |
| 52 | }; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Send a message to the other end of the port |