Skip to content

Commit

Permalink
Merge pull request #127 from LiveTL/reconnect-chat-port
Browse files Browse the repository at this point in the history
Reconnecting Chat Port
  • Loading branch information
KentoNishi authored Oct 6, 2023
2 parents dce6a96 + c6a0f6e commit 4dcfed2
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 16 deletions.
48 changes: 32 additions & 16 deletions src/components/Hyperchat.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,18 @@
Browser,
Theme,
YoutubeEmojiRenderMode,
chatUserActionsItems
chatUserActionsItems,
ChatReportUserOptions
} from '../ts/chat-constants';
import { isAllEmoji, isChatMessage, isPrivileged, responseIsAction } from '../ts/chat-utils';
import {
isAllEmoji,
isChatMessage,
isPrivileged,
responseIsAction,
useReconnect
} from '../ts/chat-utils';
import Button from 'smelte/src/components/Button';
import {
theme,
Expand Down Expand Up @@ -281,24 +290,31 @@
frameId: parseInt(paramsFrameId)
};
$port = chrome.runtime.connect({ name: JSON.stringify(frameInfo) });
let hasRun = false;
$port = useReconnect(() => {
const port = chrome.runtime.connect({
name: JSON.stringify(frameInfo)
}) as Chat.Port;
$port?.onMessage.addListener(onPortMessage);
port.onMessage.addListener(onPortMessage);
$port?.postMessage({
type: 'registerClient',
getInitialData: true
});
$port?.postMessage({
type: 'getTheme'
});
port.postMessage({
type: 'registerClient',
getInitialData: true
});
// service worker gets shut down after 30s of not receiving events
const interval = setInterval(() => $port?.postMessage({
type: 'ping'
}), 15_000);
if (!hasRun) {
port.postMessage({
type: 'getTheme'
});
}
hasRun = true;
return port;
});
return () => clearInterval(interval);
return () => $port?.destroy && $port?.destroy();
};
onMount(onLoad);
Expand Down
22 changes: 22 additions & 0 deletions src/ts/chat-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,25 @@ export const checkInjected = (error: string): boolean => {
}
return false;
};

export const useReconnect = <T extends Chat.Port>(connect: () => T): T & { destroy: () => void } => {
let actualPort = connect();
const onDisconnect = (): void => {
actualPort = connect();
actualPort.onDisconnect.addListener(onDisconnect);
};
actualPort.onDisconnect.addListener(onDisconnect);

return {
...actualPort,
get name() { return actualPort.name; },
get disconnect() { return actualPort.disconnect; },
get postMessage() { return actualPort.postMessage; },
get onMessage() { return actualPort.onMessage; },
get onDisconnect() { return actualPort.onDisconnect; },
destroy: () => {
actualPort.onDisconnect.removeListener(onDisconnect);
actualPort.disconnect();
}
};
};
1 change: 1 addition & 0 deletions src/ts/typings/chat.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ declare namespace Chat {
callback: (message: BackgroundResponse, port: Port) => void
) => void;
};
destroy?: () => void;
};

interface Interceptor {
Expand Down

0 comments on commit 4dcfed2

Please sign in to comment.