From d16e563fef8eb5955f29261c98b596ad799939ef Mon Sep 17 00:00:00 2001 From: Thor Galle Date: Tue, 10 Oct 2023 13:21:43 +0300 Subject: [PATCH] fix: avoid redundant chat & push observers --- src/lib/api/chat.ts | 2 ++ src/routes/+layout.svelte | 14 ++++++++------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/lib/api/chat.ts b/src/lib/api/chat.ts index 780bc78c..1f9f165a 100644 --- a/src/lib/api/chat.ts +++ b/src/lib/api/chat.ts @@ -80,6 +80,8 @@ export const createChatObserver = () => { } }) ); + + console.log('Chats initialized'); hasInitialized.set(true); // Special check for iOS PWA on startup diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index df7ce7a8..407f7066 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -74,16 +74,18 @@ const initializeUser = () => (unsubscribeFromUser = user.subscribe(async (latestUser) => { - // If the user logged in and has a verified email + // If the user logged in and has a verified email, or if it has changed if (!!latestUser && latestUser.emailVerified) { // without verified email: no messages, no garden, no chats firebaseObserverUnsubscribers = [ - // Leave the auth observer as-is (the value that last reacted) + // Leave the auth observer as-is, it should be initialized already unsubscribeFromAuthObserver, - // Subscribe to the chat observer - createChatObserver(), - // Subscribe to the push registration observer - createPushRegistrationObserver() + // Subscribe to the chat observer, if not initialized yet + unsubscribeFromChatObserver == null ? createChatObserver() : unsubscribeFromChatObserver, + // Subscribe to the push registration observer, if not initialized yet + unsubscribeFromPushRegistrationObserver == null + ? createPushRegistrationObserver() + : unsubscribeFromPushRegistrationObserver ]; }