Skip to content

Commit

Permalink
fix: new autoReconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
janek26 committed Oct 26, 2023
1 parent fd48dbe commit 7959464
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/shared/chrome.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ type AllowCleanUpFunction = void | (() => void);
/**
* Creates a port and recreates a new port if the old one disconnects
*
* @param chrome - chrome API
* @param createPort - function to create a port
* @param onConnect - callback when connected
*/
export async function autoConnect(
Expand All @@ -25,3 +25,20 @@ export async function autoConnect(
void autoConnect(createPort, onConnect);
});
}

/**
* Reconnects a port if it disconnects
* @param port - port to reconnect
* @param createPort - function to create a port
* @param onConnect - callback when connected
*/
export function autoReconnect(
port: chrome.runtime.Port,
createPort: () => chrome.runtime.Port,
onReconnect: (port: chrome.runtime.Port) => AllowCleanUpFunction,
) {
port.onDisconnect.addListener(() => {
console.log('Port disconnected, reconnecting...');
void autoConnect(createPort, onReconnect);
});
}

0 comments on commit 7959464

Please sign in to comment.