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

Suggestion: Use a single global readable store for live updates #29

Open
matthewrobb opened this issue Dec 13, 2022 · 4 comments
Open
Labels
enhancement New feature or request

Comments

@matthewrobb
Copy link

matthewrobb commented Dec 13, 2022

Somewhat like this:

import { readable } from "svelte/store"

const NOW = readable(new Date(), (set) => {
  const timer = setInterval(() => set(new Date()), 1000)
  return () => clearInterval(timer)
})

Effectively eliminating the NEED for configurable granularity (could still support special cases) while reducing the number of timers required for like a large list of things to 1.

@metonym metonym added the enhancement New feature or request label Dec 13, 2022
@matthewrobb
Copy link
Author

Actually this should work for any case:

import { readable, type Readable } from "svelte/store"

const LIVE_STORES: Record<number, Readable<Date>> = {}

function liveStore(interval: number = 1000) {
  return LIVE_STORES[interval] ??= readable(new Date(), (set) => {
    const timer = setInterval(() => set(new Date()), interval)
    return () => clearInterval(timer)
  })
}

@metonym
Copy link
Owner

metonym commented Dec 13, 2022

Interested in creating a PR?

@matthewrobb
Copy link
Author

Sure!

@matthewrobb
Copy link
Author

Not sure when I can get to this exactly. It's not quite so straight forward with how this lib is structured. You generally need to subscribe to a store at the beginning of the lifetime of a component instance but since the live prop can be dynamic we need a store that can dynamically adjust which timer it's using based on that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants