-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lib): add deep signals and refactor mutations and lazy
- Loading branch information
Showing
13 changed files
with
107 additions
and
47 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* The code in this file is adapted from ngrx/signals | ||
* | ||
* ngrx is an open-source project licensed under the MIT license. | ||
* | ||
* For more information about the original code, see | ||
* https://github.com/ngrx/platform | ||
*/ | ||
import { computed, isSignal, Signal as NgSignal, untracked } from '@angular/core'; | ||
import { IsKnownRecord } from './tsHelpers'; | ||
|
||
// An extended Signal type that enables the correct typing | ||
// of nested signals with the `name` or `length` key. | ||
export interface Signal<T> extends NgSignal<T> { | ||
name: unknown; | ||
length: unknown; | ||
} | ||
|
||
export type DeepSignal<T> = Signal<T> & | ||
(IsKnownRecord<T> extends true | ||
? Readonly<{ | ||
[K in keyof T]: IsKnownRecord<T[K]> extends true ? DeepSignal<T[K]> : Signal<T[K]>; | ||
}> | ||
: unknown); | ||
|
||
export function toDeepSignal<T>(signal: Signal<T>): DeepSignal<T> { | ||
const value = untracked(() => signal()); | ||
if (!isRecord(value)) { | ||
return signal as DeepSignal<T>; | ||
} | ||
|
||
return new Proxy(signal, { | ||
get(target: any, prop) { | ||
if (!(prop in value)) { | ||
return target[prop]; | ||
} | ||
|
||
if (!isSignal(target[prop])) { | ||
Object.defineProperty(target, prop, { | ||
value: computed(() => target()[prop]), | ||
configurable: true, | ||
}); | ||
} | ||
|
||
return toDeepSignal(target[prop]); | ||
}, | ||
}); | ||
} | ||
|
||
function isRecord(value: unknown): value is Record<string, unknown> { | ||
return value?.constructor === Object; | ||
} |
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,4 @@ | ||
export * from './capitalize'; | ||
export * from './deep-signal'; | ||
export * from './shallow-equal'; | ||
export * from './tsHelpers'; |
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
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