8000 feat(reactivity): untrack by teleskop150750 · Pull Request #13286 · vuejs/core · GitHub
[go: up one dir, main page]

Skip to content

feat(reactivity): untrack #13286

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

Open
wants to merge 10 commits into
base: minor
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat: untrack
  • Loading branch information
teleskop150750 committed May 6, 2025
commit d089f23943f3db1868ff6b6ed480157dd8f8222e
12 changes: 12 additions & 0 deletions packages/reactivity/__tests__/effect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
pauseTracking,
resetTracking,
startBatch,
untrack,
} from '../src/effect'

describe('reactivity/effect', () => {
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions packages/reactivity/src/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,15 @@ export function resetTracking(): void {
shouldTrack = last === undefined ? true : last
}

export function untrack<T>(fn: () => T): T {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we consider using MaybeRefOrGetter<T> instead of () => T here?

Copy link
Author
@teleskop150750 teleskop150750 Jul 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course you can. But the utility won't be too overloaded from this. ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can just call toValue(source). In other words this will make it an untrack wrapper around toValue.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand you perfectly. In my mindset, passing Ref directly should be almost completely avoided. Therefore, the use of toValue is minimized. If necessary, the user can call it himself inside untrack, rather than doing it automatically each time. If you insist, I will add your edit.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is just my opinion. I think I would prefer to hear what others think.

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
Expand Down
0