-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(signals): add patch function and remove $update method
- Loading branch information
1 parent
eeadd3f
commit 11c517d
Showing
4 changed files
with
50 additions
and
53 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
export { selectSignal } from './select-signal'; | ||
export { signalState } from './signal-state'; | ||
export { SignalStateUpdater } from './signal-state-models'; | ||
export { patch, signalState, SignalStateUpdater } from './signal-state'; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,52 @@ | ||
import { signal, WritableSignal } from '@angular/core'; | ||
import { toDeepSignal } from './deep-signal'; | ||
import { DeepSignal, toDeepSignal } from './deep-signal'; | ||
import { defaultEqual } from './select-signal'; | ||
import { | ||
NotAllowedStateCheck, | ||
SignalState, | ||
SignalStateUpdate, | ||
} from './signal-state-models'; | ||
|
||
type SignalState<State extends Record<string, unknown>> = DeepSignal<State> & | ||
SignalStateMeta<State>; | ||
|
||
const SIGNAL_STATE_META_KEY = Symbol('SIGNAL_STATE_META_KEY'); | ||
|
||
type SignalStateMeta<State extends Record<string, unknown>> = { | ||
[SIGNAL_STATE_META_KEY]: WritableSignal<State>; | ||
}; | ||
|
||
/** | ||
* Signal state cannot contain optional properties. | ||
*/ | ||
type NotAllowedStateCheck<State> = State extends Required<State> | ||
? State extends Record<string, unknown> | ||
? { [K in keyof State]: State[K] & NotAllowedStateCheck<State[K]> } | ||
: unknown | ||
: never; | ||
|
||
export type SignalStateUpdater<State extends Record<string, unknown>> = | ||
| Partial<State> | ||
| ((state: State) => Partial<State>); | ||
|
||
export function signalState<State extends Record<string, unknown>>( | ||
initialState: State & NotAllowedStateCheck<State> | ||
): SignalState<State> { | ||
const stateSignal = signal(initialState as State, { equal: defaultEqual }); | ||
const deepSignal = toDeepSignal(stateSignal.asReadonly()); | ||
(deepSignal as SignalState<State>).$update = | ||
signalStateUpdateFactory(stateSignal); | ||
Object.defineProperty(deepSignal, SIGNAL_STATE_META_KEY, { | ||
value: stateSignal, | ||
}); | ||
|
||
return deepSignal as SignalState<State>; | ||
} | ||
|
||
export function signalStateUpdateFactory<State extends Record<string, unknown>>( | ||
stateSignal: WritableSignal<State> | ||
): SignalStateUpdate<State>['$update'] { | ||
return (...updaters) => | ||
stateSignal.update((state) => | ||
updaters.reduce( | ||
(currentState: State, updater) => ({ | ||
...currentState, | ||
...(typeof updater === 'function' ? updater(currentState) : updater), | ||
}), | ||
state | ||
) | ||
); | ||
export function patch<State extends Record<string, unknown>>( | ||
signalState: SignalStateMeta<State>, | ||
...updaters: SignalStateUpdater<State>[] | ||
): void { | ||
signalState[SIGNAL_STATE_META_KEY].update((currentState) => | ||
updaters.reduce( | ||
(nextState: State, updater) => ({ | ||
...nextState, | ||
...(typeof updater === 'function' ? updater(nextState) : updater), | ||
}), | ||
currentState | ||
) | ||
); | ||
} |