Skip to content

Commit

Permalink
Merge branch 'mv3'
Browse files Browse the repository at this point in the history
  • Loading branch information
KentoNishi committed Oct 6, 2023
2 parents aa27fe4 + 4dcfed2 commit 729f674
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 17 deletions.
47 changes: 30 additions & 17 deletions src/components/Hyperchat.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@
UNDONE_MSG
} from '../ts/chat-constants';
import '../ts/resize-tracker';
import { isAllEmoji, isChatMessage, isPrivileged, responseIsAction, createPopup, getRandomString } from '../ts/chat-utils';
import {
isAllEmoji,
isChatMessage,
isPrivileged,
responseIsAction,
useReconnect,
createPopup,
getRandomString
} from '../ts/chat-utils';
import Button from 'smelte/src/components/Button';
import {
theme,
Expand Down Expand Up @@ -401,27 +409,32 @@
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
});
if (paramsArchiveKey) $initialized = true;
else {
$port?.postMessage({
type: 'getTheme'
port.postMessage({
type: 'registerClient',
getInitialData: true
});
}
if (paramsArchiveKey) $initialized = true;
else {
if (!hasRun) {
port.postMessage({
type: 'getTheme'
});
}
}
hasRun = true;
// service worker gets shut down after 30s of not receiving events
const interval = setInterval(() => $port?.postMessage({
type: 'ping'
}), 15_000);
return port;
});
return () => clearInterval(interval);
return () => $port?.destroy && $port?.destroy();
};
const onRefresh = () => {
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 @@ -78,3 +78,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 @@ -155,6 +155,7 @@ declare namespace Chat {
callback: (message: BackgroundResponse, port: Port) => void
) => void;
};
destroy?: () => void;
};

interface Interceptor {
Expand Down

0 comments on commit 729f674

Please sign in to comment.