Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No stop functionality in derived stores #2553

Closed
cdock1029 opened this issue Apr 24, 2019 · 0 comments · Fixed by #2730
Closed

No stop functionality in derived stores #2553

cdock1029 opened this issue Apr 24, 2019 · 0 comments · Fixed by #2730

Comments

@cdock1029
Copy link

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.

@cdock1029 cdock1029 changed the title Not stop functionality in derived stores No stop functionality in derived stores Apr 24, 2019
Rich-Harris added a commit that referenced this issue May 12, 2019
allow derivers to return cleanup functions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant