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

chore: better store subscriptions #12277

Merged
merged 7 commits into from
Jul 3, 2024
Merged

Conversation

Rich-Harris
Copy link
Member

Noticed a few opportunities for improvement with our store logic, figured I'd quickly address them even though it's low priority. First, the compiler output is improved. Before:

const $$subscriptions = {};

$.unsubscribe_on_destroy($$subscriptions);

const $count = () => $.store_get(count, "$count", $$subscriptions);
let count = writable(0);

After (we can create the subscriptions and setup the teardown in a single step):

const $$stores = $.setup_stores();
const $count = () => $.store_get(count, "$count", $$stores);
let count = writable(0);

The runtime also gets a bit smaller. We can use the existing teardown function which is more efficient than creating a new on_destroy function, and we can simplify store_getlast_value was never written to, so whatever that logic was attempting to do, it wasn't doing it.

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

Copy link

changeset-bot bot commented Jul 3, 2024

🦋 Changeset detected

Latest commit: 24d5e7b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

const value = get(entry.value);
// This could happen if the store was cleaned up because the component was destroyed and there's a leak on the user side.
// In that case we don't want to fail with a cryptic Symbol error, but rather return the last value we got.
return value === UNINITIALIZED ? entry.last_value : value;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was in response to a real bug I investigated at some point. I think the fix was gradually removed over time (there was more logic there before) so that it's unused now. Let me see if I can dig up the PR and related issue to see if something like this is still necessary.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was from https://github.com/sveltejs/svelte-octane/commit/6ebe7118c8fe3f45d0b9787d1e9c92cc7e27d62d, but without any reference - I vaguely remember that it occured while testing the code out on some Svelte site and noticing the bug. But this error can't happen anymore now because back then the source value was set back to UNINITIALIZED which is no longer the case now - so all good.

@Rich-Harris Rich-Harris merged commit f2179c9 into main Jul 3, 2024
9 checks passed
@Rich-Harris Rich-Harris deleted the better-store-subscriptions branch July 3, 2024 13:30
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 this pull request may close these issues.

None yet

2 participants