tangled
alpha
login
or
join now
ptr.pet
/
bsky-repost-likes
2
fork
atom
its for when you want to get like notifications for your reposts
2
fork
atom
overview
issues
pulls
pipelines
fix: make reconnect backoff work properly
ptr.pet
8 months ago
7c0c486f
e3fea54b
verified
This commit was signed with the committer's
known signature
.
ptr.pet
SSH Key Fingerprint:
SHA256:Abmvag+juovVufZTxyWY8KcVgrznxvBjQpJesv071Aw=
+22
-9
5 changed files
expand all
collapse all
unified
split
extension
entrypoints
background.ts
utils
store.ts
webapp
src
App.tsx
types.ts
ws.ts
+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
9
+
let reconnectBackoff = 1000 * 1;
10
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
59
-
backoff: 1000 * 1,
61
61
+
doRetry: () => {
62
62
+
console.log("ws", websocket, " status ", connectionStatus);
63
63
+
if (websocket !== null && connectionStatus === "error") {
64
64
+
const b = reconnectBackoff;
65
65
+
reconnectBackoff *= 2;
66
66
+
return b;
67
67
+
} else return null;
68
68
+
},
60
69
}).then((ws) => {
61
70
websocket = ws ?? null;
62
71
});
63
72
};
64
73
const disconnect = () => {
65
65
-
setConnectionStatus("disconnecting...");
74
74
+
setConnectionStatus("disconnected");
75
75
+
setError(null);
66
76
websocket?.close();
67
77
websocket = null;
68
78
};
+3
extension/utils/store.ts
···
1
1
+
import { ConnectionStatus } from "bsky-repost-likes-monitor";
2
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
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
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
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
11
-
backoff?: number;
11
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
81
-
if (ev.code === 1006 && cb.backoff) {
81
81
+
if (ev.code === 1006) {
82
82
cb.setConnectionStatus("error");
83
83
-
cb.setError(`websocket closed abnormally: (${ev.code}) ${ev.reason}`);
84
84
-
const newData = { backoff: cb.backoff * 2, ...cb };
85
85
-
setTimeout(() => connect(newData), cb.backoff);
83
83
+
const backoff = (cb.doRetry ?? (() => null))();
84
84
+
if (backoff) {
85
85
+
cb.setError(`websocket closed abnormally: (${ev.code}) ${ev.reason}`);
86
86
+
setTimeout(() => connect(cb), backoff);
87
87
+
}
86
88
} else if (ev.code === 1000 || ev.code === 1001 || ev.code === 1005) {
87
89
cb.setError(null);
88
90
} else {