-
Notifications
You must be signed in to change notification settings - Fork 77
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
Signal notifier numeric boundaries #430
base: main
Are you sure you want to change the base?
Conversation
As analyzed in issue ngxtension#428 (comment)
@@ -5,7 +5,7 @@ export function createNotifier() { | |||
|
|||
return { | |||
notify: () => { | |||
sourceSignal.update((v) => v + 1); | |||
sourceSignal.update((v) => (v >>> 0) + 1); |
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 can also be replaced by sourceSignal.set(Math.random() + 1)
Thanks @leonelvsc this seems sensible - my understanding is that it will wrap Maybe there is a consideration here as to whether people will use the version number in some logic, but considering this is a reasonably unlikely case in the first place maybe we can just add a note to the docs about behaviour with large numbers |
I've created a simple stackblitz showing the behaviour it will never reach MAX_SAFE_INT because of the
https://stackblitz.com/edit/stackblitz-starters-rwqgze?file=src%2Fmain.ts So another simplier solution is to initialize signal with null then on notify set "Symbol()" it will also work, if you need numeric values another solution could be Math.random() + 1 |
It doesn't specifically need to be numeric for the use cases outlined in the docs, just truthy/falsey works for the purpose of the init stuff so in that sense null/Symbol would work fine. But the version number is accessible and people could be relying on these incremental version numbers for something, so I think any change to that behaviour would be considered a breaking change But the bit shift approach still seems fine to me (unless I'm missing something), since it would be:
Which still satisfies the falsey/truthy requirement and keeps things incremental + infinite and non breaking |
Would you mind adding a test case in the PR for the overflow behaviour? |
It woud implicate the call of 2 ** 32 times to notify() if i don't modify the createNotifier signature to allow an initial value, is there any other way to test this ?
problem here i can't mock the internal signal because is defined as const can't patch metods and have a reference to that const signal |
Good point, we could separate out the update function, e.g: e.g: export function createNotifier(){}
export function notifierUpdateFn(version: number){} But I'm not a fan of modifying the API for the sake of tests. We can probably just consider it an implementation detail and leave it untested, if the existing tests pass we're good imo. I'll leave it up to you though and other reviewers can weigh in if they want. |
Well i think is better if we leave it untested but it's just my opinion. Test with the loop would look like:
but it takes 478 seconds to complete the test
|
We can skip that test I guess. |
As analyzed in issue #428 (comment)