Skip to content

Synchronous derived stores should not trigger require-stores-init #1073

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

Closed
mcous opened this issue Feb 12, 2025 · 3 comments · Fixed by #1086
Closed

Synchronous derived stores should not trigger require-stores-init #1073

mcous opened this issue Feb 12, 2025 · 3 comments · Fixed by #1086
Labels
enhancement New feature or request

Comments

@mcous
Copy link

mcous commented Feb 12, 2025

Overview

I've been using the 3.0.0-next version in our Svelte project, and I'm really liking the new recommended set of rules! However, I've had to disable require-stores-init, because it triggered on nearly every single usage of derived.

Proposal

I think require-stores-init makes a lot of sense for writable and readable stores - it's goal seems to be to prevent an implicitly undefined store values. However, I think synchronous derived stores should not trigger the require-stores-init lint. If a derived store is synchronous, there is no risk that its value can be implicitly undefined, because it will run its callback synchronously when it gets its first subscriber.

If you agree with this change, I'm happy to contribute a PR!

Alternatives

Alternatively, I could imagine a configuration option that allows you to selectively enable or disable require-stores-init for each store type. However, I think enforcing this rule on asynchronous derived stores is valuable, so I prefer my proposal above.

@mcous mcous added the enhancement New feature or request label Feb 12, 2025
@baseballyama baseballyama added the bug Something isn't working label Feb 13, 2025
@baseballyama baseballyama removed the bug Something isn't working label Feb 23, 2025
@baseballyama
Copy link
Member

It might be good to add an option like ignoreDerived (default is false).

@mcous
Copy link
Author

mcous commented Feb 24, 2025

Alternatively, with stores on the way out with Svelte 5, maybe it's simply better to remove require-stores-init from the recommended config. As currently defined, it's way too noisy in my real-life app to be useful.

ignoreDerived could be a pragmatic solution, but I don't think it's ideal for actual derived usage. In an ideal world, I do want to enforce an initial value on asynchronous derived stores:

const source = writable(0)

// synchronous, initial value not needed
const doubled = derived(source, ($source) => $source * 2)
 
// async., initial value needed
const doubledLater = derived(
  source,
  ($source, set) => {
    const id = setTimeout(() => set($source * 2), 1000)
    return () => clearTimeout(id)
  },
  0
)

@baseballyama
Copy link
Member

I decided to remove it from the recommended config.👍

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

Successfully merging a pull request may close this issue.

2 participants