its for when you want to get like notifications for your reposts

fix: make reconnect backoff work properly

ptr.pet 7c0c486f e3fea54b

verified
+22 -9
+12 -2
extension/entrypoints/background.ts
··· 6 6 import { onMessage, sendMessage } from "webext-bridge/background"; 7 7 8 8 let websocket: WebSocket | null = null; 9 + let reconnectBackoff = 1000 * 1; 10 + 9 11 let connectionStatus: ConnectionStatus = "disconnected"; 10 12 let error: string | null = null; 11 13 let items: Notification[] = []; ··· 56 58 }, 57 59 setConnectionStatus, 58 60 setError, 59 - backoff: 1000 * 1, 61 + doRetry: () => { 62 + console.log("ws", websocket, " status ", connectionStatus); 63 + if (websocket !== null && connectionStatus === "error") { 64 + const b = reconnectBackoff; 65 + reconnectBackoff *= 2; 66 + return b; 67 + } else return null; 68 + }, 60 69 }).then((ws) => { 61 70 websocket = ws ?? null; 62 71 }); 63 72 }; 64 73 const disconnect = () => { 65 - setConnectionStatus("disconnecting..."); 74 + setConnectionStatus("disconnected"); 75 + setError(null); 66 76 websocket?.close(); 67 77 websocket = null; 68 78 };
+3
extension/utils/store.ts
··· 1 + import { ConnectionStatus } from "bsky-repost-likes-monitor"; 2 + 1 3 const store = { 2 4 actorId: storage.defineItem<string>("sync:actorId", { fallback: "" }), 3 5 serviceDomain: storage.defineItem<string>("sync:serviceDomain", { 4 6 fallback: "likes.gaze.systems", 5 7 }), 8 + backoff: storage.defineItem<number>("local:backoff", { fallback: 1000 * 1 }), 6 9 }; 7 10 8 11 export default store;
-1
webapp/src/App.tsx
··· 30 30 }; 31 31 32 32 const disconnect = (): void => { 33 - setConnectionStatus("disconnecting..."); 34 33 ws()?.close(); 35 34 setWs(null); 36 35 };
-1
webapp/src/types.ts
··· 29 29 30 30 export type ConnectionStatus = 31 31 | "disconnected" 32 - | "disconnecting..." 33 32 | "connecting..." 34 33 | "connected" 35 34 | "error";
+7 -5
webapp/src/ws.ts
··· 8 8 pushNotification: (item: Notification) => void; 9 9 actorId: string; 10 10 serviceDomain: string; 11 - backoff?: number; 11 + doRetry?: () => number | null; 12 12 } 13 13 14 14 const handleResolver = new XrpcHandleResolver({ ··· 78 78 cb.setConnectionStatus("disconnected"); 79 79 console.log("WebSocket disconnected"); 80 80 // abnormal closure 81 - if (ev.code === 1006 && cb.backoff) { 81 + if (ev.code === 1006) { 82 82 cb.setConnectionStatus("error"); 83 - cb.setError(`websocket closed abnormally: (${ev.code}) ${ev.reason}`); 84 - const newData = { backoff: cb.backoff * 2, ...cb }; 85 - setTimeout(() => connect(newData), cb.backoff); 83 + const backoff = (cb.doRetry ?? (() => null))(); 84 + if (backoff) { 85 + cb.setError(`websocket closed abnormally: (${ev.code}) ${ev.reason}`); 86 + setTimeout(() => connect(cb), backoff); 87 + } 86 88 } else if (ev.code === 1000 || ev.code === 1001 || ev.code === 1005) { 87 89 cb.setError(null); 88 90 } else {