@@ -17,10 +17,7 @@ const inspectedTabId = browser.devtools.inspectedWindow.tabId;
1717const devtoolsMachine = interpret (
1818 createDevtoolsMachine ( {
1919 actions : {
20- connectToClient : ( ) => {
21- clientPort . send ( { type : "connectToClient" } ) ;
22- startConnectTimeout ( ) ;
23- } ,
20+ connectToClient,
2421 startRequestInterval : ( ) => {
2522 clearTimeout ( connectTimeoutId ) ;
2623
@@ -40,7 +37,6 @@ const devtoolsMachine = interpret(
4037
4138let panelHidden = true ;
4239let connectTimeoutId : NodeJS . Timeout ;
43- let disconnectTimeoutId : NodeJS . Timeout ;
4440
4541const portAdapter = createPortMessageAdapter ( ( ) =>
4642 browser . runtime . connect ( { name : inspectedTabId . toString ( ) } )
@@ -51,24 +47,21 @@ const rpcClient = createRpcClient<DevtoolsRPCMessage>(portAdapter);
5147
5248devtoolsMachine . subscribe ( ( { value } ) => {
5349 if ( value === "connected" ) {
54- clearTimeout ( disconnectTimeoutId ) ;
50+ clearTimeout ( connectTimeoutId ) ;
5551 }
5652} ) ;
5753
58- // In case we can't connect to the tab, we should at least show something to the
59- // user when we've attempted to connect a max number of times.
60- function startConnectTimeout ( attempts = 0 ) {
54+ function connectToClient ( ) {
55+ clientPort . send ( { type : "connectToClient" } ) ;
56+ startConnectTimeout ( ) ;
57+ }
58+
59+ function startConnectTimeout ( ) {
60+ clearTimeout ( connectTimeoutId ) ;
61+
6162 connectTimeoutId = setTimeout ( ( ) => {
62- if ( attempts < 3 ) {
63- clientPort . send ( { type : "connectToClient" } ) ;
64- startConnectTimeout ( attempts + 1 ) ;
65- } else {
66- devtoolsMachine . send ( "timeout" ) ;
67- }
68- // Pick a threshold above the time it takes to determine if the client is
69- // found on the page. This ensures we don't reset that counter and provide a
70- // proper "not found" message.
71- } , 11_000 ) ;
63+ devtoolsMachine . send ( "clientNotFound" ) ;
64+ } , 10_000 ) ;
7265}
7366
7467clientPort . on ( "connectToDevtools" , ( message ) => {
@@ -83,20 +76,11 @@ clientPort.on("registerClient", (message) => {
8376} ) ;
8477
8578clientPort . on ( "disconnectFromDevtools" , ( ) => {
86- clearTimeout ( disconnectTimeoutId ) ;
8779 devtoolsMachine . send ( "disconnect" ) ;
88-
89- disconnectTimeoutId = setTimeout ( ( ) => {
90- devtoolsMachine . send ( "clientNotFound" ) ;
91- } , 10_000 ) ;
92- } ) ;
93-
94- clientPort . on ( "clientNotFound" , ( ) => {
95- clearTimeout ( connectTimeoutId ) ;
96- devtoolsMachine . send ( "clientNotFound" ) ;
80+ startConnectTimeout ( ) ;
9781} ) ;
9882
99- clientPort . send ( { type : " connectToClient" } ) ;
83+ connectToClient ( ) ;
10084
10185function startRequestInterval ( ms = 500 ) {
10286 let id : NodeJS . Timeout ;
@@ -163,11 +147,6 @@ async function createDevtoolsPanel() {
163147 connectedToPanel = true ;
164148 }
165149
166- if ( devtoolsMachine . state . value === "initialized" ) {
167- clientPort . send ( { type : "connectToClient" } ) ;
168- startConnectTimeout ( ) ;
169- }
170-
171150 if ( devtoolsMachine . state . value === "connected" && panelHidden ) {
172151 unsubscribers . add ( startRequestInterval ( ) ) ;
173152 }
0 commit comments