question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hi, is there a way to only initialize the store at the moment that a component has used it? And is there a way to have a callback when there are no active listeners? svelte/store has this and is quite similar to zustand.

My usecase is a graphql subscription that is only needed temporarily.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
dai-shicommented, Oct 8, 2020

So, if I were to do something like this, it could be non lazy store, but with listener count.

const useStore = create((set, get) => ({
  profile: { loading: true },
  observer: null,
  listeners: 0,
  subscribe: () => {
    let { listeners, observer } = get()
    listeners += 1
    if (listeners === 1) {
      observer = graphqlSubscription.subscribe(profile => set({ profile }))
    }
    set({ listeners, observer })
  },
  unsubscribe: () => {
    let { listeners, observer } = get()
    listeners -= 1
    if (listeners === 0) {
      observer.unsubscribe()
      observer = null
    }
    set({ listeners, observer })
  },
}))

const selectProfile = (state) => state.profile

const useProfile = () => {
  const profile = useStore(selectProfile)
  useEffect(() => {
    useStore.getState().subscribe()
    return () => useStore.getState().unsubscribe()
  }, [])
  return profile
}

Just an idea… There might be a better way, like subscribe function returning unsubscribe function.

0reactions
dai-shicommented, Oct 23, 2020

I’m not sure if I understand what createLazy is like, but in general, we would be strict about making breaking changes and adding new core features, as we think the api is basically complete. A PR for middleware would be appreciated (although, I can’t say for sure if it would be merged at this point.)

Read more comments on GitHub >

github_iconTop Results From Across the Web

LazyOne: Funny Family Pajamas – Men, Women, Kids, Pets ...
Add some fun to bed time with our cute & funny pajamas for the whole family! From infant onesies to our Flapjacks for...
Read more >
Lazy store (@lazy_storee) • Instagram photos and videos
2569 Followers, 2529 Following, 227 Posts - See Instagram photos and videos from Welcome to Lazy store (@lazy_storee)
Read more >
The Lazy Store - Facebook
The Lazy Store. Nov 6, 2016󰞋󰟠. Love for Pom-poms Priced at 120+shipping. To place your order DM us or whatsapp us at 9582044680...
Read more >
Lazy One - Amazon.com
Lazy One Logo. Lazy One. + Follow. Home · Best Sellers · Women. Back; Women · Adult Onesie Pajamas · Women's Regular Sets...
Read more >
Cowboy Boots, Cowboy Hats, Western Apparel | Lazy J Ranch ...
The Lazy J is a ranch apparel store with two store fronts located in the Stockyards and Willow Park Texas. Lazy J exists...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found