| 469 | } |
| 470 | |
| 471 | function connectExtensionPort() { |
| 472 | if (port) { |
| 473 | throw new Error('DevTools port was already connected'); |
| 474 | } |
| 475 | |
| 476 | const tabId = chrome.devtools.inspectedWindow.tabId; |
| 477 | port = chrome.runtime.connect({ |
| 478 | name: String(tabId), |
| 479 | }); |
| 480 | |
| 481 | // If DevTools port was reconnected and Bridge was already created |
| 482 | // We should subscribe bridge to this port events |
| 483 | // This could happen if service worker dies and all ports are disconnected, |
| 484 | // but later user continues the session and Chrome reconnects all ports |
| 485 | // Bridge object is still in-memory, though |
| 486 | if (lastSubscribedBridgeListener) { |
| 487 | port.onMessage.addListener(lastSubscribedBridgeListener); |
| 488 | } |
| 489 | |
| 490 | // This port may be disconnected by Chrome at some point, this callback |
| 491 | // will be executed only if this port was disconnected from the other end |
| 492 | // so, when we call `port.disconnect()` from this script, |
| 493 | // this should not trigger this callback and port reconnection |
| 494 | port.onDisconnect.addListener(() => { |
| 495 | port = null; |
| 496 | connectExtensionPort(); |
| 497 | }); |
| 498 | } |
| 499 | |
| 500 | function mountReactDevTools() { |
| 501 | reactPollingInstance = null; |