feat: untrack

This commit is contained in:
teleskop150750 2025-05-06 13:31:01 +03:00
parent cc326a72d2
commit d089f23943
2 changed files with 21 additions and 0 deletions

View File

@ -27,6 +27,7 @@ import {
pauseTracking,
resetTracking,
startBatch,
untrack,
} from '../src/effect'
describe('reactivity/effect', () => {
@ -1182,6 +1183,17 @@ describe('reactivity/effect', () => {
expect(spy2).toHaveBeenCalledTimes(2)
})
it('should not track dependencies when using untrack', () => {
const value = ref(1)
let dummy
effect(() => {
dummy = untrack(() => value.value)
})
expect(dummy).toBe(1)
value.value = 2
expect(dummy).toBe(1)
})
describe('dep unsubscribe', () => {
function getSubCount(dep: Dep | undefined) {
let count = 0

View File

@ -537,6 +537,15 @@ export function resetTracking(): void {
shouldTrack = last === undefined ? true : last
}
export function untrack<T>(fn: () => T): T {
try {
pauseTracking()
return fn()
} finally {
resetTracking()
}
}
/**
* Registers a cleanup function for the current active effect.
* The cleanup function is called right before the next effect run, or when the