Skip to content

Commit

Permalink
fix: seamless automatic reconnection (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
ignatiusmb authored Apr 15, 2024
1 parent 86c67dc commit e5897e5
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/lib/runtime.svelte.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { type DebugNode, app } from './state.svelte';

const tabId = chrome.devtools.inspectedWindow.tabId;
const port = chrome.runtime.connect({ name: `${tabId}` });
let port = chrome.runtime.connect({ name: `${tabId}` });

port.postMessage({ source: 'svelte-devtools', tabId, type: 'bypass::ext/init' });

export const background = {
send(type: `bridge::${'ext' | 'page'}/${string}` | 'bypass::ext/page->refresh', payload?: any) {
port.postMessage({ source: 'svelte-devtools', tabId, type, payload });
try {
port.postMessage({ source: 'svelte-devtools', tabId, type, payload });
} catch {
// https://developer.chrome.com/docs/extensions/develop/concepts/messaging#port-lifetime
// chrome aggressively disconnects the port, not much we can do other than to reconnect
port = chrome.runtime.connect({ name: `${tabId}` });
background.send(type, payload); // retry immediately
}
},
};

Expand Down

0 comments on commit e5897e5

Please sign in to comment.