-
-
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 deepComputed function (#4539)
- Loading branch information
1 parent
ffc1d87
commit 269bd32
Showing
5 changed files
with
75 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { isSignal, signal } from '@angular/core'; | ||
import { deepComputed } from '../src'; | ||
|
||
describe('deepComputed', () => { | ||
it('creates a deep computed signal when computation result is an object literal', () => { | ||
const source = signal(0); | ||
const result = deepComputed(() => ({ count: { value: source() + 1 } })); | ||
|
||
expect(isSignal(result)).toBe(true); | ||
expect(isSignal(result.count)).toBe(true); | ||
expect(isSignal(result.count.value)).toBe(true); | ||
|
||
expect(result()).toEqual({ count: { value: 1 } }); | ||
expect(result.count()).toEqual({ value: 1 }); | ||
expect(result.count.value()).toBe(1); | ||
|
||
source.set(1); | ||
|
||
expect(result()).toEqual({ count: { value: 2 } }); | ||
expect(result.count()).toEqual({ value: 2 }); | ||
expect(result.count.value()).toBe(2); | ||
}); | ||
|
||
it('does not create a deep computed signal when computation result is an array', () => { | ||
const source = signal(0); | ||
const result = deepComputed(() => [{ value: source() + 1 }]); | ||
|
||
expect(isSignal(result)).toBe(true); | ||
expect(result()).toEqual([{ value: 1 }]); | ||
expect((result as any)[0]).toBe(undefined); | ||
}); | ||
}); |
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,8 @@ | ||
import { computed } from '@angular/core'; | ||
import { DeepSignal, toDeepSignal } from './deep-signal'; | ||
|
||
export function deepComputed<T extends object>( | ||
computation: () => T | ||
): DeepSignal<T> { | ||
return toDeepSignal(computed(computation)); | ||
} |
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,30 @@ | ||
# DeepComputed | ||
|
||
The `deepComputed` function creates a `DeepSignal` when a computation result is an object literal. | ||
It can be used as a regular computed signal, but it also contains computed signals for each nested property. | ||
|
||
```ts | ||
import { signal } from '@angular/core'; | ||
import { deepComputed } from '@ngrx/signals'; | ||
|
||
const limit = signal(25); | ||
const offset = signal(0); | ||
const totalItems = signal(100); | ||
|
||
const pagination = deepComputed(() => ({ | ||
currentPage: Math.floor(offset() / limit()) + 1, | ||
pageSize: limit(), | ||
totalPages: Math.ceil(totalItems() / limit()), | ||
})); | ||
|
||
console.log(pagination()); // logs: { currentPage: 1, pageSize: 25, totalPages: 4 } | ||
console.log(pagination.currentPage()); // logs: 1 | ||
console.log(pagination.pageSize()); // logs: 25 | ||
console.log(pagination.totalPages()); // logs: 4 | ||
``` | ||
|
||
<div class="alert is-helpful"> | ||
|
||
For enhanced performance, deeply nested signals are generated lazily and initialized only upon first access. | ||
|
||
</div> |
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