Skip to content

Commit

Permalink
Revert "feat(runtime-core): add once option to watch (vuejs#9034)"
Browse files Browse the repository at this point in the history
This reverts commit d573e50.
  • Loading branch information
johnsoncodehk committed Oct 14, 2023
1 parent 34a0de4 commit 50418be
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 61 deletions.
38 changes: 0 additions & 38 deletions packages/runtime-core/__tests__/apiWatch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1205,42 +1205,4 @@ describe('api: watch', () => {
expect(countWE).toBe(3)
expect(countW).toBe(2)
})

const options = [
{ name: 'only trigger once watch' },
{
deep: true,
name: 'only trigger once watch with deep'
},
{
flush: 'sync',
name: 'only trigger once watch with flush: sync'
},
{
flush: 'pre',
name: 'only trigger once watch with flush: pre'
},
{
immediate: true,
name: 'only trigger once watch with immediate'
}
] as const
test.each(options)('$name', async option => {
const count = ref(0)
const cb = vi.fn()

watch(count, cb, { once: true, ...option })

count.value++
await nextTick()

expect(count.value).toBe(1)
expect(cb).toHaveBeenCalledTimes(1)

count.value++
await nextTick()

expect(count.value).toBe(2)
expect(cb).toHaveBeenCalledTimes(1)
})
})
31 changes: 8 additions & 23 deletions packages/runtime-core/src/apiWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ export interface WatchOptionsBase extends DebuggerOptions {
export interface WatchOptions<Immediate = boolean> extends WatchOptionsBase {
immediate?: Immediate
deep?: boolean
once?: boolean
}

export type WatchStopHandle = () => void
Expand Down Expand Up @@ -172,16 +171,8 @@ export function watch<T = any, Immediate extends Readonly<boolean> = false>(
function doWatch(
source: WatchSource | WatchSource[] | WatchEffect | object,
cb: WatchCallback | null,
{ immediate, deep, flush, once, onTrack, onTrigger }: WatchOptions = EMPTY_OBJ
{ immediate, deep, flush, onTrack, onTrigger }: WatchOptions = EMPTY_OBJ
): WatchStopHandle {
if (cb && once) {
const _cb = cb
cb = (...args) => {
_cb(...args)
unwatch()
}
}

if (__DEV__ && !cb) {
if (immediate !== undefined) {
warn(
Expand All @@ -195,12 +186,6 @@ function doWatch(
`watch(source, callback, options?) signature.`
)
}
if (once !== undefined) {
warn(
`watch() "once" option is only respected when using the ` +
`watch(source, callback, options?) signature.`
)
}
}

const warnInvalidSource = (s: unknown) => {
Expand Down Expand Up @@ -380,13 +365,6 @@ function doWatch(
onScheduled(schedulerJob as any)
})

const unwatch = () => {
effect.stop()
if (instance && instance.scope) {
remove(instance.scope.effects!, effect)
}
}

if (__DEV__) {
effect.onTrack = onTrack
effect.onTrigger = onTrigger
Expand All @@ -408,6 +386,13 @@ function doWatch(
effect.run()
}

const unwatch = () => {
effect.stop()
if (instance && instance.scope) {
remove(instance.scope.effects!, effect)
}
}

if (__SSR__ && ssrCleanup) ssrCleanup.push(unwatch)
return unwatch
}
Expand Down

0 comments on commit 50418be

Please sign in to comment.