8000 feat: add computed peek by teleskop150750 · Pull Request #66 · un-ts/unuse · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: add computed peek #66

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

Merged
merged 2 commits into from
Jul 4, 2025
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
10 changes: 10 additions & 0 deletions .changeset/neat-dots-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'unuse-reactivity': minor
'unuse': minor
'unuse-angular': minor
'unuse-react': minor
'unuse-solid': minor
'unuse-vue': minor
---

feat: add computed peek
19 changes: 19 additions & 0 deletions packages/unuse-reactivity/src/unComputed/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, expect, it, vi } from 'vitest';
import { isUnComputed, UN_COMPUTED, unComputed } from '.';
import { unEffect } from '../unEffect';
import { unSignal } from '../unSignal';

describe('unComputed', () => {
Expand All @@ -13,6 +14,7 @@ describe('unComputed', () => {

expect(myComputed).toBeTypeOf('object');
expect(myComputed.get).toBeTypeOf('function');
expect(myComputed.peek).toBeTypeOf('function');
expect(isUnComputed(myComputed)).toBe(true);
});

Expand Down Expand Up @@ -64,6 +66,23 @@ describe('unComputed', () => {
expect(fnSpy).toHaveBeenCalledTimes(2);
});

it('should peek the current value without triggering effects', () => {
const fn1Spy = vi.fn();

const mySignal = unSignal(42);
const myComputed = unComputed(mySignal.get);

unEffect(() => {
myComputed.peek();
fn1Spy();
});

mySignal.set(100);
expect(myComputed.peek()).toBe(100);

expect(fn1Spy).toHaveBeenCalledTimes(1);
});

describe('isUnComputed', () => {
it('should return true for an UnComputed', () => {
const myComputed = unComputed(() => 42);
Expand Down
68 changes: 39 additions & 29 deletions packages/unuse-reactivity/src/unComputed/index.ts
< D405 td id="diff-ce67a7587969b22cba3cb369f4459f1da1adf3565d15b8cf351f6ce938a917f2R64" data-line-number="64" class="blob-num blob-num-addition js-linkable-line-number js-blob-rnum">
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
checkDirty,
link,
REACTIVITY_STATE,
setCurrentSub,
shallowPropagate,
updateComputed,
} from '../unReactiveSystem';
Expand All @@ -12,32 +13,6 @@
getter: (previousValue?: T) => T;
}

export function computedOper<T>(this: UnComputedState<T>): T {
const flags = this.flags;
if (
flags & (16 satisfies ReactiveFlags.Dirty) ||
(flags & (32 satisfies ReactiveFlags.Pending) &&
checkDirty(this.deps!, this))
) {
if (updateComputed(this)) {
const subs = this.subs;
if (subs !== undefined) {
shallowPropagate(subs);
}
}
} else if (flags & (32 satisfies ReactiveFlags.Pending)) {
this.flags = flags & ~(32 satisfies ReactiveFlags.Pending);
}

if (REACTIVITY_STATE.activeSub !== undefined) {
link(this, REACTIVITY_STATE.activeSub);
} else if (REACTIVITY_STATE.activeScope !== undefined) {
link(this, REACTIVITY_STATE.activeScope);
}

return this.value!;
}

/**
* A unique symbol used to identify `UnComputed` objects.
*
Expand All @@ -59,6 +34,11 @@
* Retrieves the current value of the computed.
*/
get(): T;

/**
* Retrieves the current value of the signal without triggering effects.
*/
peek(): T;
}

/**
Expand All @@ -69,19 +49,49 @@
* @returns An `UnComputed` object that has a `get` method to retrieve the current value.
*/
export function unComputed<T>(callback: () => T): UnComputed<T> {
const get: UnComputed<T>['get'] = computedOper.bind({
const state: UnComputedState<T> = {
value: undefined,
subs: undefined,
subsTail: undefined,
deps: undefined,
depsTail: undefined,
flags: 17 as ReactiveFlags.Mutable | ReactiveFlags.Dirty,
getter: callback,
} satisfies UnComputedState<T>) as UnComputed<T>['get'];
};

return {
[UN_COMPUTED]: true,
get,
get() {
const flags = state.flags;
if (
flags & (16 satisfies ReactiveFlags.Dirty) ||
(flags & (32 satisfies ReactiveFlags.Pending) &&
checkDirty(state.deps!, state))

Check warning on line 69 in packages/unuse-reactivity/src/unComputed/index.ts

View workflow job for this annotation

GitHub Actions / Lint: node-24, ubuntu-latest

Forbidden non-null assertion

Check warning on line 69 in packages/unuse-reactivity/src/unComputed/index.ts

View workflow job for this annotation

GitHub Actions / Lint: node-24, ubuntu-latest

Forbidden non-null assertion
) {
if (updateComputed(state)) {
const subs = state.subs;
if (subs !== undefined) {
shallowPropagate(subs);
}
}
} else if (flags & (32 satisfies ReactiveFlags.Pending)) {
state.flags = flags & ~(32 satisfies ReactiveFlags.Pending);
}

if (REACTIVITY_STATE.activeSub !== undefined) {
link(state, REACTIVITY_STATE.activeSub);
} else if (REACTIVITY_STATE.activeScope !== undefined) {
link(state, REACTIVITY_STATE.activeScope);
}

return state.value!;

Check warning on line 87 in packages/unuse-reactivity/src/unComputed/index.ts

View workflow job for this annotation

GitHub Actions / Lint: node-24, ubuntu-latest

Forbidden non-null assertion

Check warning on line 87 in packages/unuse-reactivity/src/unComputed/index.ts

View workflow job for this annotation

GitHub Actions / Lint: node-24, ubuntu-latest

Forbidden non-null assertion
},
peek() {
const prev = setCurrentSub(undefined);
const val = this.get();
setCurrentSub(prev);
return val;
},
};
}

Expand Down
Loading
0