Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ declare global {
_notify_push_online: boolean,
_notify_push_available: boolean,
_notify_push_error_count: number,
_notify_push_ready: boolean,
}
}

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

window._notify_push_listeners[name].push(handler);
if (window._notify_push_ws !== null && typeof window._notify_push_ws === "object") {
if (window._notify_push_ws !== null && typeof window._notify_push_ws === "object" && window._notify_push_ready) {
window._notify_push_ws.send('listen ' + name);
} else {
setupSocket(options);
Expand All @@ -73,10 +74,12 @@ function setupGlobals(options: NotifyPushOptions = {}) {
window._notify_push_online = true;
window._notify_push_available = false;
window._notify_push_error_count = 0;
window._notify_push_ready = false;

subscribe('networkOffline', () => {
window._notify_push_online = false;
window._notify_push_ws = null;
window._notify_push_ready = false;
});
subscribe('networkOnline', () => {
window._notify_push_error_count = 0;
Expand Down Expand Up @@ -127,6 +130,8 @@ async function setupSocket(options: NotifyPushOptions = {}) {
window._notify_push_ws.send(options.credentials.password)
}

window._notify_push_ready = true;

for (let name in window._notify_push_listeners) {
window._notify_push_ws.send('listen ' + name);
}
Expand Down Expand Up @@ -154,6 +159,7 @@ async function setupSocket(options: NotifyPushOptions = {}) {
window._notify_push_ws.onerror = window._notify_push_ws.onclose = () => {
window._notify_push_ws = null;
window._notify_push_error_count += 1;
window._notify_push_ready = false;

setTimeout(() => {
if (window._notify_push_online) {
Expand Down