-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
feat: pass update function to store setup callbacks #6750
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
Merged
dummdidumm
merged 6 commits into
sveltejs:version-4
from
rmunn:feature/update-and-set-for-stores
May 4, 2023
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
10ab317
Pass update function to store setup callbacks
rmunn b2b7bd1
Document new update param to store callbacks
rmunn a7e2a61
StartStopNotifier type now requires update param
rmunn ef6e407
Update docs: update param is not optional
rmunn 2574346
Merge branch 'version-4' into feature/update-and-set-for-stores
dummdidumm 2f9fa6f
fix, changelog
dummdidumm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to be a non-optional parameter, else TS will error here if you try to use
update
without checking for its existance first.Which makes me think: Is this a breaking change for people implementing their own store contract? If so, yeah it needs to be an optional
update
parameter. @Conduitry what's your stance on this?If we leave it like this, this means we need to adjust the other signatures a bit to type them so that
update
is not optional as it is in fact provided forreadable
/writable
/derived
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Strange, the two unit tests I added ran just fine, and I'm not seeing any TS errors in VS code on my use of
update
in the tests. I thought it would be a breaking change (at least from Typescript's perspective if not from Javascript's perspective) to make this non-optional as then everyone who has writtenreadable(0, (set) => { });
would get Typescript complaining that they didn't take the second parameter. But then, I don't know all the subtleties of Typescript; maybe it's fine with omitting parameters to functions because Javascript allows that?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW, at no point in the Svelte code does the
update
function from StartStopNotifier get called; it's only passed to callbacks. And the callbacks can either take theupdate
parameter and use it, or not take it in which case it would obviously be an error for them to have a call toupdate(x => x+1)
. But I don't think making this optional will actually produce any Typescript errors, and I do think that making it non-optional will be a breaking change.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For function parameters, the inverse is true: If the function expects a function-parameter with x arguments, you can pass in a function with zero to x parameters.
You don't see errors because the repository runs TS in non-strict mode (no strict null checks).
See this playground for more: https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABACwKYBt1wBTDALkWwCdUBHQgZymJjAHMAaROABygH4qa76BKRAF4AfIgBucGABMBAbwC+AKEVpMObAJGIFfANyIA9AcRg4iVsTgAjdKgC2KjFmwlyzNlE2jZixH5bsAHRQcAAycADuqMQAwgCGlKga+kaI0ZbEiFaoEHEgiQFQiBBwIOhSWaiI4FKowHSoUoryfEA
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. And https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABACwKYBt1wBTDALkWwCdUBHQgZymJjAHMAaROAByiprvoEpEBeAHyIAbnBgATPgG8AvgCh5aTDmx8hiOTwDciAPR7EYOIlbE4AI3SoAtkoxZsJcuuHT9huDZhRYDFuyIMJRGJmaW1jaIqD5oxPKIiYikZAB0UHAAMnAA7qjEAMIAhpSoarrysjzyQA shows that omitting
update
will be no problem, so it won't break existing code. I'll make the change.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a breaking change for everyone who uses
StartStopNotifier
to implement their own store contract: https://www.typescriptlang.org/play?#code/C4TwDgpgBAysCGAnYcD2YByrgEsBmOEiUAvFABQDOEwAXFJcIjgHYDmANFAK5gAm8YBHqNm7AJSkAfFABuqHHwDcAKBV5uLAMa5ULKAFsQAeQDuLNIgjkAFhAA291PXgsQXRtzx56cJCmB0LFwCIkkAbxUoaIZgLzxyAHJUez5E8SUoAHosqDArWRxUbkp7ECgCFgguFlRTKCJEVERKKAAjCC14EuheASEoHFba+vgoKwBHbhwrPjykeAMaIhUAXyA