33 checkDirty ,
44 link ,
55 REACTIVITY_STATE ,
6+ setCurrentSub ,
67 shallowPropagate ,
78 updateComputed ,
89} from '../unReactiveSystem' ;
@@ -12,32 +13,6 @@ export interface UnComputedState<T> extends ReactiveNode {
1213 getter : ( previousValue ?: T ) => T ;
1314}
1415
15- export function computedOper < T > ( this : UnComputedState < T > ) : T {
16- const flags = this . flags ;
17- if (
18- flags & ( 16 satisfies ReactiveFlags . Dirty ) ||
19- ( flags & ( 32 satisfies ReactiveFlags . Pending ) &&
20- checkDirty ( this . deps ! , this ) )
21- ) {
22- if ( updateComputed ( this ) ) {
23- const subs = this . subs ;
24- if ( subs !== undefined ) {
25- shallowPropagate ( subs ) ;
26- }
27- }
28- } else if ( flags & ( 32 satisfies ReactiveFlags . Pending ) ) {
29- this . flags = flags & ~ ( 32 satisfies ReactiveFlags . Pending ) ;
30- }
31-
32- if ( REACTIVITY_STATE . activeSub !== undefined ) {
33- link ( this , REACTIVITY_STATE . activeSub ) ;
34- } else if ( REACTIVITY_STATE . activeScope !== undefined ) {
35- link ( this , REACTIVITY_STATE . activeScope ) ;
36- }
37-
38- return this . value ! ;
39- }
40-
4116/**
4217 * A unique symbol used to identify `UnComputed` objects.
4318 *
@@ -59,6 +34,11 @@ export interface UnComputed<T> {
5934 * Retrieves the current value of the computed.
6035 */
6136 get ( ) : T ;
37+
38+ /**
39+ * Retrieves the current value of the signal without triggering effects.
40+ */
41+ peek ( ) : T ;
6242}
6343
6444/**
@@ -69,19 +49,49 @@ export interface UnComputed<T> {
6949 * @returns An `UnComputed` object that has a `get` method to retrieve the current value.
7050 */
7151export function unComputed < T > ( callback : ( ) => T ) : UnComputed < T > {
72- const get : UnComputed < T > [ 'get' ] = computedOper . bind ( {
52+ const state : UnComputedState < T > = {
7353 value : undefined ,
7454 subs : undefined ,
7555 subsTail : undefined ,
7656 deps : undefined ,
7757 depsTail : undefined ,
7858 flags : 17 as ReactiveFlags . Mutable | ReactiveFlags . Dirty ,
7959 getter : callback ,
80- } satisfies UnComputedState < T > ) as UnComputed < T > [ 'get' ] ;
60+ } ;
8161
8262 return {
8363 [ UN_COMPUTED ] : true ,
84- get,
64+ get ( ) {
65+ const flags = state . flags ;
66+ if (
67+ flags & ( 16 satisfies ReactiveFlags . Dirty ) ||
68+ ( flags & ( 32 satisfies ReactiveFlags . Pending ) &&
69+ checkDirty ( state . deps ! , state ) )
70+ ) {
71+ if ( updateComputed ( state ) ) {
72+ const subs = state . subs ;
73+ if ( subs !== undefined ) {
74+ shallowPropagate ( subs ) ;
75+ }
76+ }
77+ } else if ( flags & ( 32 satisfies ReactiveFlags . Pending ) ) {
78+ state . flags = flags & ~ ( 32 satisfies ReactiveFlags . Pending ) ;
79+ }
80+
81+ if ( REACTIVITY_STATE . activeSub !== undefined ) {
82+ link ( state , REACTIVITY_STATE . activeSub ) ;
83+ } else if ( REACTIVITY_STATE . activeScope !== undefined ) {
84+ link ( state , REACTIVITY_STATE . activeScope ) ;
85+ }
86+
87+ return state . value ! ;
88+ } ,
89+ peek ( ) {
90+ const prev = setCurrentSub ( undefined ) ;
91+ const val = this . get ( ) ;
92+ setCurrentSub ( prev ) ;
93+ return val ;
94+ } ,
8595 } ;
8696}
8797
0 commit comments