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

Self referencing derived store #4880

Closed
antony opened this issue May 21, 2020 · 6 comments · Fixed by #6750
Closed

Self referencing derived store #4880

antony opened this issue May 21, 2020 · 6 comments · Fixed by #6750

Comments

@antony
Copy link
Member

antony commented May 21, 2020

Describe the bug
This is half bug, half proposal.

The current implementation of derived stores doesn't provide a way to use the store's existing value in the new value (a-la .update).

You can see in the REPL my workaround for this is to use get(storeName) which... sort of works.

The issue is that you get a console error which pertains to whatever the error you previously encountered was, which is written every time the store updates.

Logs

 Error: Uncaught (in promise): thir is not defined

To Reproduce
https://svelte.dev/repl/df25b5983b8d464fb1e47af7a1d12d61?version=3.22.3

Expected behavior
The current behaviour is correct, aside from the error.

Information about your Environment:

  • Svelte version 3.22.3

Severity
I do need to use the current store value in a derived store, as my derived store builds a set of results which need to be concatenated. I can work around it though, so:

low

@pushkine
Copy link
Contributor

@antony
Copy link
Member Author

antony commented May 21, 2020

wow @pushkine - that's a nice workaround! time to refactor again :D

@Conduitry
Copy link
Member

@antony I'm not seeing any warnings or exceptions with your REPL.

@antony
Copy link
Member Author

antony commented May 23, 2020

@Conduitry break it - change console to consdole or something, and then hit the button a few times. It'll print the error for ever more.

@tanhauhau
Copy link
Member

can't replicate the bug, removing the bug label

@rmunn
Copy link
Contributor

rmunn commented Sep 21, 2021

Looks like #6737 is related to this; either a duplicate, or an overlapping request. If the optional second parameter of derived could be either set or (set, update) then this use case would be solved, too:

export const first = writable({ value: 1 })
const second = derived([ first ], ([ $first ], set) => {
	set({ value: $first.value + 1 })
})
export const third = derived([ second ], async ([ $second ], (set, update)) => {
	console.log('deriving third value')
	update($third => ({ $third.value + $second.value }))
}, { value: 1 })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants