mirror of https://github.com/vuejs/core.git
feat: untrack
This commit is contained in:
parent
cc326a72d2
commit
d089f23943
|
@ -27,6 +27,7 @@ import {
|
||||||
pauseTracking,
|
pauseTracking,
|
||||||
resetTracking,
|
resetTracking,
|
||||||
startBatch,
|
startBatch,
|
||||||
|
untrack,
|
||||||
} from '../src/effect'
|
} from '../src/effect'
|
||||||
|
|
||||||
describe('reactivity/effect', () => {
|
describe('reactivity/effect', () => {
|
||||||
|
@ -1182,6 +1183,17 @@ describe('reactivity/effect', () => {
|
||||||
expect(spy2).toHaveBeenCalledTimes(2)
|
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', () => {
|
describe('dep unsubscribe', () => {
|
||||||
function getSubCount(dep: Dep | undefined) {
|
function getSubCount(dep: Dep | undefined) {
|
||||||
let count = 0
|
let count = 0
|
||||||
|
|
|
@ -537,6 +537,15 @@ export function resetTracking(): void {
|
||||||
shouldTrack = last === undefined ? true : last
|
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.
|
* Registers a cleanup function for the current active effect.
|
||||||
* The cleanup function is called right before the next effect run, or when the
|
* The cleanup function is called right before the next effect run, or when the
|
||||||
|
|
Loading…
Reference in New Issue