Skip to content

Commit

Permalink
fix: avoid redundant chat & push observers
Browse files Browse the repository at this point in the history
  • Loading branch information
th0rgall committed Oct 10, 2023
1 parent 8905b1b commit d16e563
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/lib/api/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ export const createChatObserver = () => {
}
})
);

console.log('Chats initialized');
hasInitialized.set(true);

// Special check for iOS PWA on startup
Expand Down
14 changes: 8 additions & 6 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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
];
}
Expand Down

0 comments on commit d16e563

Please sign in to comment.