-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: adjust increment api and remove add from NumberSignal (#2704)
* fix: adjust increment api and remove add from NumberSignal.ts This will introduce increment event for NumberSignal so that it is processed atomically on server based on the last seen value of NumberSignal without any retries. Also, this refactors the increment method to incrementBy, and also removes the add methods that were introduced in #2694 * remove unnecessary imports and chai-like usage * formatter:format * set the value locally when incrementBy is called * address sonar warnings --------- Co-authored-by: Anton Platonov <[email protected]>
- Loading branch information
Showing
13 changed files
with
318 additions
and
359 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { createIncrementStateEvent } from './events.js'; | ||
import { $update } from './FullStackSignal.js'; | ||
import { ValueSignal } from './ValueSignal.js'; | ||
|
||
/** | ||
* A signal that holds a number value. The underlying | ||
* value of this signal is stored and updated as a | ||
* shared value on the server. | ||
* | ||
* After obtaining the NumberSignal instance from | ||
* a server-side service that returns one, the value | ||
* can be updated using the `value` property, | ||
* and it can be read with or without the | ||
* `value` property (similar to a normal signal): | ||
* | ||
* @example | ||
* ```tsx | ||
* const counter = CounterService.counter(); | ||
* | ||
* return ( | ||
* <Button onClick={() => counter.incrementBy(1)}> | ||
* Click count: { counter } | ||
* </Button> | ||
* <Button onClick={() => counter.value = 0}>Reset</Button> | ||
* ); | ||
* ``` | ||
*/ | ||
export class NumberSignal extends ValueSignal<number> { | ||
/** | ||
* Increments the value by the specified delta. The delta can be negative to | ||
* decrease the value. | ||
* | ||
* This method differs from using the `++` or `+=` operators directly on the | ||
* signal value. It performs an atomic operation to prevent conflicts from | ||
* concurrent changes, ensuring that other users' modifications are not | ||
* accidentally overwritten. | ||
* | ||
* @param delta - The delta to increment the value by. The delta can be | ||
* negative. | ||
*/ | ||
incrementBy(delta: number): void { | ||
if (delta === 0) { | ||
return; | ||
} | ||
this.setValueLocal(this.value + delta); | ||
const event = createIncrementStateEvent(delta); | ||
this[$update](event); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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 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
Oops, something went wrong.