forked from spartan-ng/spartan
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(tooltip): migrate to signals spartan-ng#430
- Loading branch information
1 parent
961af42
commit fb09a2b
Showing
6 changed files
with
249 additions
and
181 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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { Component, signal } from '@angular/core'; | ||
import { TestBed } from '@angular/core/testing'; | ||
import { computedPrevious } from './computed-previous'; | ||
|
||
describe(computedPrevious.name, () => { | ||
@Component({ standalone: true, template: '{{previous()}}' }) | ||
class TestComponent { | ||
public readonly value = signal(0); | ||
public readonly previous = computedPrevious(this.value); | ||
} | ||
|
||
function setup() { | ||
const fixture = TestBed.createComponent(TestComponent); | ||
fixture.detectChanges(); | ||
return fixture.componentInstance; | ||
} | ||
|
||
it('should work properly', () => { | ||
const cmp = setup(); | ||
|
||
expect(cmp.value()).toEqual(0); | ||
expect(cmp.previous()).toEqual(0); | ||
|
||
cmp.value.set(1); | ||
|
||
expect(cmp.value()).toEqual(1); | ||
expect(cmp.previous()).toEqual(0); | ||
|
||
cmp.value.set(2); | ||
|
||
expect(cmp.value()).toEqual(2); | ||
expect(cmp.previous()).toEqual(1); | ||
|
||
cmp.value.set(2); | ||
|
||
expect(cmp.value()).toEqual(2); | ||
expect(cmp.previous()).toEqual(1); | ||
|
||
cmp.value.set(3); | ||
|
||
expect(cmp.value()).toEqual(3); | ||
expect(cmp.previous()).toEqual(2); | ||
}); | ||
}); |
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,47 @@ | ||
import { computed, type Signal, untracked } 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 the same as the current value. | ||
* | ||
* @example | ||
* ```ts | ||
* const value = signal(0); | ||
* const previous = computedPrevious(value); | ||
* | ||
* effect(() => { | ||
* console.log('Current value:', value()); | ||
* console.log('Previous value:', previous()); | ||
* }); | ||
* | ||
* Logs: | ||
* // Current value: 0 | ||
* // 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> { | ||
let current = null as T; | ||
let previous = untracked(() => s()); // initial value is the current value | ||
|
||
return computed(() => { | ||
current = s(); | ||
const result = previous; | ||
previous = current; | ||
return result; | ||
}); | ||
} |
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.