Skip to content
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 .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ jobs:

strategy:
matrix:
node-version: [16]
node-version: [22]

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
Expand Down
36 changes: 21 additions & 15 deletions esm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ class Computed extends Signal {
*/
s
/**
* @param {(v: T) => T} _
* @param {T} v
* @param {(v: T) => T} _
* @param {T} v
* @param {{ equals?: Equals<T> }} o
* @param {boolean} f
* @param {boolean} f
*/
constructor(_, v, o, f) {
super(_);
Expand All @@ -65,17 +65,23 @@ class Computed extends Signal {
this.r = new Set; // related signals
this.s = new Reactive(v, o); // signal
}
peek() { return this.s.peek() }
get value() {
if (this.$) {
const prev = computedSignal;
computedSignal = this;
try { this.s.value = this._(this.s._) }
finally {
this.$ = false;
computedSignal = prev;
}
refresh() {
if (!this.$) return

const prev = computedSignal;
computedSignal = this;
try { this.s.value = this._(this.s._) }
finally {
this.$ = false;
computedSignal = prev;
}
}
peek() {
this.refresh()
return this.s.peek()
}
get value() {
this.refresh()
return this.s.value;
}
}
Expand Down Expand Up @@ -153,7 +159,7 @@ export class Effect extends FX {

/**
* Invokes a function when any of its internal signals or computed values change.
*
*
* Returns a dispose callback.
* @template T
* @type {<T>(fn: (v: T) => T, value?: T, options?: { async?: boolean }) => () => void}
Expand Down Expand Up @@ -261,4 +267,4 @@ export const signal = (value, options = defaults) => new Reactive(value, options
* @public
* @template T
* @typedef {Omit<Computed<T>, '$'|'s'|'f'|'r'|'_'>} ComputedSignal<T>
*/
*/
3 changes: 2 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ export default (library, {signal, computed, effect, batch, Signal}) => {

counter.value = 1;
assert(invokes.length === 1, 'computed peek not working as expected');
assert(doubleCounter.peek() === 2, 'computed peek not returning right value');
}

function testComputedUniqueness() {
Expand Down Expand Up @@ -358,7 +359,7 @@ export default (library, {signal, computed, effect, batch, Signal}) => {

assert(invokes.length === 3, 'looped effects not working');
assert(invokes.join(',') === '0,0,1', 'looped values not matching');

invokes.splice(0);
loop = 1;
num.value = 1;
Expand Down