File tree Expand file tree Collapse file tree 2 files changed +21
-0
lines changed Expand file tree Collapse file tree 2 files changed +21
-0
lines changed Original file line number Diff line number Diff line change @@ -27,6 +27,7 @@ import {
27
27
pauseTracking ,
28
28
resetTracking ,
29
29
startBatch ,
30
+ untrack ,
30
31
} from '../src/effect'
31
32
32
33
describe ( 'reactivity/effect' , ( ) => {
@@ -1182,6 +1183,17 @@ describe('reactivity/effect', () => {
1182
1183
expect ( spy2 ) . toHaveBeenCalledTimes ( 2 )
1183
1184
} )
1184
1185
1186
+ it ( 'should not track dependencies when using untrack' , ( ) => {
1187
+ const value = ref ( 1 )
1188
+ let dummy
1189
+ effect ( ( ) => {
1190
+ dummy = untrack ( ( ) => value . value )
1191
+ } )
1192
+ expect ( dummy ) . toBe ( 1 )
1193
+ value . value = 2
1194
+ expect ( dummy ) . toBe ( 1 )
1195
+ } )
1196
+
1185
1197
describe ( 'dep unsubscribe' , ( ) => {
1186
1198
function getSubCount ( dep : Dep | undefined ) {
1187
1199
let count = 0
Original file line number Diff line number Diff line change @@ -537,6 +537,15 @@ export function resetTracking(): void {
537
537
shouldTrack = last === undefined ? true : last
538
538
}
539
539
540
+ export function untrack < T > ( fn : ( ) => T ) : T {
541
+ try {
542
+ pauseTracking ( )
543
+ return fn ( )
544
+ } finally {
545
+ resetTracking ( )
546
+ }
547
+ }
548
+
540
549
/**
541
550
* Registers a cleanup function for the current active effect.
542
551
* The cleanup function is called right before the next effect run, or when the
You can’t perform that action at this time.
0 commit comments