Closed
Description
I was thinking I could create internal subscriptions, say to firebase, within derived store start function, like with readable stores:
export const posts = derived(
user,
($user, set) => {
if (isClient && $user) {
console.log('user update, subbing posts.')
const unsub = firebase
.firestore()
.collection('posts')
.orderBy('createdAt', 'desc')
.onSnapshot(snap => {
console.log('posts snapshot:', snap.docs.length)
set(snap.docs.map(d => ({ id: d.id, ...d.data() })))
})
// this doesn't do anything because internally it's never called.
// derived creates a readable sub from $user, and usubs from *that* outer subscription
// no way for me to unsub from this *internal* subscription
return () => console.log('unsub posts') || unsub()
}
},
undefined
)
Use case would be any subscription creation that depends on values from other stores or firebase paths or auth state etc. Kind of like switchMap
from rxjs... reactive all the way down.
One useful aspect of the readable store is that if you don't return a stop function, the firebase listener remains active. It'd be nice to also have this use case inside derived
but you'd have to distinguish between stopping subscription when dependencies change to recreate it, and stopping when there's 0 subscribers.
Metadata
Metadata
Assignees
Labels
No labels