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

Autosubscription doesn't work with array of stores or stores inside objects, add $...a.b or $(a.b) syntax to make it work #4494

Closed
Mlocik97 opened this issue Apr 2, 2022 · 2 comments

Comments

@Mlocik97
Copy link
Contributor

Mlocik97 commented Apr 2, 2022

Describe the problem

Let say we have something like this:

<script>
	import {writable} from 'svelte/store';
	const a = writable(0);
	const b = {
		c : writable(0)
	};
	const r = [ writable(0), writable(1) ]
</script>

if you want to autosubscribe to a, it's simple, you just do $a, but if you want to subscribe to b.c, there is problem.

b.$c // returns undefined
$(b.c) // invalid syntax (it thinks `$` is function) instead `()` being to explicitly ordering how code is evaluated
${...b.c} // could work (or without {}), but doesn't work, syntax error

in case of array, it's same problem, doing

r.$0 // doesn't work, error
r.$['0'] // doesn't work, error
${...r[0]} // doesn't work, error

so if we want to autosubscribe to these methods, we must first extract store to variable.

Describe the proposed solution

Spread operator seems like cool feature for extracting properties from objects or arrays, so I propose feature:

$...a.b that would read property b inside object a, and subscribe to store that is inside of propety.
$...a[0] would subscribe to store that is inside array at index 0.

This would give ability to subscribe to really any store inside component, even that, that is inside object that is inside array...

in case of having:

const a = {
    b: writable({
         c: 'hello'
    })    
}

we would access it with: ($...a.b).c syntax, or similiar way for array: ($...a.b)[0]

Alternatives considered

Using () to tell what has greater priority in order of evaluation and allowing $() syntax, that looks like function, but would not be function. Aka $(b[0]) would fisrt extract index 0 from array b, and then subscribe to it. or $(b.c) will subscribe to store that is inside c in object b.

Importance

would make my life easier

Additional Information

No response

@Mlocik97
Copy link
Contributor Author

Mlocik97 commented Apr 2, 2022

ups, can you move it to Svelte repo, somehow I put it to wrong one.

@Mlocik97 Mlocik97 changed the title Autosubscription doesn't work with array of stores or stores inside objects Autosubscription doesn't work with array of stores or stores inside objects, add $... syntax to make it work Apr 2, 2022
@Mlocik97 Mlocik97 changed the title Autosubscription doesn't work with array of stores or stores inside objects, add $... syntax to make it work Autosubscription doesn't work with array of stores or stores inside objects, add $...a.b or $(a.b) syntax to make it work Apr 2, 2022
@mrkishi
Copy link
Member

mrkishi commented Apr 2, 2022

I'm going to close this one as there are already a couple of open issues discussing alternative syntaxes for store subscriptions at sveltejs/svelte#4079 and sveltejs/svelte#6373.

@mrkishi mrkishi closed this as completed Apr 2, 2022
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

No branches or pull requests

2 participants