Skip to content

Commit

Permalink
feat: add ping and delete unused comment
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonghakseo committed Apr 7, 2024
1 parent f63dc6e commit 9b6c5e3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 14 deletions.
6 changes: 1 addition & 5 deletions apps/chrome-extension/lib/background/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import 'webextension-polyfill';

/**
* Extension reloading is necessary because the browser automatically caches the css.
* If you do not use the css of the content script, please delete it.
*/

console.log('background loaded');
console.log("Edit 'apps/chrome-extension/lib/background/index.ts' and save to reload.");
21 changes: 13 additions & 8 deletions packages/hmr/lib/initClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ export default function initReloadClient({ id, onUpdate }: { id: string; onUpdat
let ws: WebSocket | null = null;
try {
ws = new WebSocket(LOCAL_RELOAD_SOCKET_URL);
ws.addEventListener('message', event => {
const message = MessageInterpreter.receive(String(event.data));
if (message.type === 'do_update' && message.id === id) {
sendUpdateCompleteMessage();
onUpdate();
return;
}
});
ws.onopen = () => {
ws?.addEventListener('message', event => {
const message = MessageInterpreter.receive(String(event.data));
if (message.type === 'ping') {
console.log('[HMR] Client OK');
}
if (message.type === 'do_update' && message.id === id) {
sendUpdateCompleteMessage();
onUpdate();
return;
}
});
};

ws.onclose = () => {
console.log(
Expand Down
9 changes: 9 additions & 0 deletions packages/hmr/lib/initReloadServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ function initReloadServer() {
}
});
});

function ping() {
clientsThatNeedToUpdate.forEach(ws => ws.send(MessageInterpreter.send({ type: 'ping' })));
setTimeout(() => {
ping();
}, 15_000);
}

ping();
}

initReloadServer();
3 changes: 2 additions & 1 deletion packages/hmr/lib/interpreter/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ type UpdateRequestMessage = {
id: string;
};
type UpdateCompleteMessage = { type: 'done_update' };
type PingMessage = { type: 'ping' };
type BuildCompletionMessage = { type: 'build_complete'; id: string };

export type SerializedMessage = string;

export type WebSocketMessage = UpdateCompleteMessage | UpdateRequestMessage | BuildCompletionMessage;
export type WebSocketMessage = UpdateCompleteMessage | UpdateRequestMessage | BuildCompletionMessage | PingMessage;

0 comments on commit 9b6c5e3

Please sign in to comment.