Skip to content
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

fix(computed-previous): Don't return null for initial value, but retu… #281

Merged
merged 1 commit into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/src/content/docs/utilities/Signals/computed-previous.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ contributors: ['enea-jahollari']
---

`computedPrevious` is a helper function that returns a `signal` that emits the previous value of the passed `signal`. It's useful when you want to keep track of the previous value of a signal.
It will emit `null` as the previous value when the signal is first created.
As the initial value it will emit the current value of the signal, and then it will emit the previous value every time the signal changes.

```ts
import { computedPrevious } from 'ngxtension/computed-previous';
Expand All @@ -21,7 +21,7 @@ import { computedPrevious } from 'ngxtension/computed-previous';
const a = signal(1);
const b = computedPrevious(a);

console.log(b()); // null
console.log(b()); // 1

a.set(2);
console.log(b()); // 1
Expand All @@ -33,5 +33,5 @@ console.log(b()); // 2
## API

```ts
computedPrevious<T>(signal: Signal<T>): Signal<T | null>
computedPrevious<T>(signal: Signal<T>): Signal<T>
```
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe(computedPrevious.name, () => {
const cmp = setup();

expect(cmp.value()).toEqual(0);
expect(cmp.previous()).toEqual(null);
expect(cmp.previous()).toEqual(0);

cmp.value.set(1);

Expand Down
28 changes: 16 additions & 12 deletions libs/ngxtension/computed-previous/src/computed-previous.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { computed, type Signal } from '@angular/core';
import { computed, untracked, type Signal } from '@angular/core';

/**
* Returns a signal that emits the previous value of the given signal.
* The first time the signal is emitted, the previous value will be `null`.
* The first time the signal is emitted, the previous value will be the same as the current value.
*
* @example
* ```ts
Expand All @@ -16,28 +16,32 @@ import { computed, type Signal } from '@angular/core';
*
* Logs:
* // Current value: 0
* // Previous value: null
* // Previous value: 0
*
* value.set(1);
*
* Logs:
* // Current value: 1
* // Previous value: 0
*
* value.set(2);
*
* Logs:
* // Current value: 2
* // Previous value: 1
*```

*
* @param s Signal to compute previous value for
* @returns Signal that emits previous value of `s`
*/
export function computedPrevious<T>(s: Signal<T>): Signal<T | null> {
export function computedPrevious<T>(s: Signal<T>): Signal<T> {
let current = null as T;
let previous = null as T;
let previous = untracked(() => s()); // initial value is the current value

return computed(() => {
const value = s();
if (value !== current) {
previous = current;
current = value;
}
return previous;
current = s();
const result = previous;
previous = current;
return result;
});
}
Loading