Skip to content

Allow subscribing to stores declared in a local scope #16259

Open
@Caleb-T-Owens

Description

@Caleb-T-Owens

Describe the problem

At GitButler, we’re looking to reduce our reliance on Svelte’s reactivity system. While powerful, it often exhibits subtle and hard-to-describe (and not-type-safe) behaviours that have been the source of several bugs.

I have been starting to spike this problem, and looking into switching back to either Rx.JS or svelte stores. As I started to look into converting our UiState system to stores, but there have been a couple ergonomic issues that I hope can be figured out.

Something we've ended up doing, is using @const declarations to access sub-portions of state inside loops IE:

{@const stackState = uiState.stack(stack.id)}
<p>Example: {stackState.foobar.current}</p>

With stores however, it's not possible to subscribe to a store that was declared locally. For example, we would like to do the following:

{@const stackState = uiState.stack(stack.id)}
{@const foobar = stackState.foobar}
<p>Example: {$foobar}</p>

Unfortunately this is not currently possible: https://svelte.dev/playground/0802979ba7f346fcaf846ada1471a944?version=5.34.9

Perhaps there was some historical significance for this, but with the reactivity system you can now:

Given this capability, it seems only natural to update the capabilities of the $ syntax.

Describe the proposed solution

Something that would be great to see in the near future would be to allow subscribing to arbitrary stores, IE:

{@const stackState = uiState.stack(stack.id)}
{@const foobar = stackState.foobar}
<p>Example: {$foobar}</p>

If it's possible to subscribe to arbitrary stores, then I would then be interested in exploring whether it would be possible to use $ syntax on properties as well, IE:

{@const stackState = uiState.stack(stack.id)}
<p>Example: {stackState.$foobar}</p>

However, that feels like a stretch goal to me, and the first step of simply making subscribing to arbitrary stores would be a great improvement.

Importance

would make my life easier

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions