Skip to content

Commit fabdefd

Browse files
committed
Add a flag to track if notify_push is ready instead of relying on the Webservice readyState
Signed-off-by: Benjamin Frueh <[email protected]>
1 parent 1b23a8e commit fabdefd

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

lib/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ declare global {
1313
_notify_push_online: boolean,
1414
_notify_push_available: boolean,
1515
_notify_push_error_count: number,
16+
_notify_push_ready: boolean,
1617
}
1718
}
1819

@@ -57,7 +58,7 @@ export function listen(name: string, handler: (string, any) => void, options: No
5758
}
5859

5960
window._notify_push_listeners[name].push(handler);
60-
if (window._notify_push_ws !== null && typeof window._notify_push_ws === "object" && window._notify_push_ws.readyState === WebSocket.OPEN) {
61+
if (window._notify_push_ws !== null && typeof window._notify_push_ws === "object" && window._notify_push_ready) {
6162
window._notify_push_ws.send('listen ' + name);
6263
} else {
6364
setupSocket(options);
@@ -73,10 +74,12 @@ function setupGlobals(options: NotifyPushOptions = {}) {
7374
window._notify_push_online = true;
7475
window._notify_push_available = false;
7576
window._notify_push_error_count = 0;
77+
window._notify_push_ready = false;
7678

7779
subscribe('networkOffline', () => {
7880
window._notify_push_online = false;
7981
window._notify_push_ws = null;
82+
window._notify_push_ready = false;
8083
});
8184
subscribe('networkOnline', () => {
8285
window._notify_push_error_count = 0;
@@ -127,6 +130,8 @@ async function setupSocket(options: NotifyPushOptions = {}) {
127130
window._notify_push_ws.send(options.credentials.password)
128131
}
129132

133+
window._notify_push_ready = true;
134+
130135
for (let name in window._notify_push_listeners) {
131136
window._notify_push_ws.send('listen ' + name);
132137
}
@@ -154,6 +159,7 @@ async function setupSocket(options: NotifyPushOptions = {}) {
154159
window._notify_push_ws.onerror = window._notify_push_ws.onclose = () => {
155160
window._notify_push_ws = null;
156161
window._notify_push_error_count += 1;
162+
window._notify_push_ready = false;
157163

158164
setTimeout(() => {
159165
if (window._notify_push_online) {

0 commit comments

Comments
 (0)