Skip to content

Commit 50418be

Browse files
committed
Revert "feat(runtime-core): add once option to watch (vuejs#9034)"
This reverts commit d573e50.
1 parent 34a0de4 commit 50418be

File tree

2 files changed

+8
-61
lines changed

2 files changed

+8
-61
lines changed

packages/runtime-core/__tests__/apiWatch.spec.ts

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,42 +1205,4 @@ describe('api: watch', () => {
12051205
expect(countWE).toBe(3)
12061206
expect(countW).toBe(2)
12071207
})
1208-
1209-
const options = [
1210-
{ name: 'only trigger once watch' },
1211-
{
1212-
deep: true,
1213-
name: 'only trigger once watch with deep'
1214-
},
1215-
{
1216-
flush: 'sync',
1217-
name: 'only trigger once watch with flush: sync'
1218-
},
1219-
{
1220-
flush: 'pre',
1221-
name: 'only trigger once watch with flush: pre'
1222-
},
1223-
{
1224-
immediate: true,
1225-
name: 'only trigger once watch with immediate'
1226-
}
1227-
] as const
1228-
test.each(options)('$name', async option => {
1229-
const count = ref(0)
1230-
const cb = vi.fn()
1231-
1232-
watch(count, cb, { once: true, ...option })
1233-
1234-
count.value++
1235-
await nextTick()
1236-
1237-
expect(count.value).toBe(1)
1238-
expect(cb).toHaveBeenCalledTimes(1)
1239-
1240-
count.value++
1241-
await nextTick()
1242-
1243-
expect(count.value).toBe(2)
1244-
expect(cb).toHaveBeenCalledTimes(1)
1245-
})
12461208
})

packages/runtime-core/src/apiWatch.ts

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ export interface WatchOptionsBase extends DebuggerOptions {
7474
export interface WatchOptions<Immediate = boolean> extends WatchOptionsBase {
7575
immediate?: Immediate
7676
deep?: boolean
77-
once?: boolean
7877
}
7978

8079
export type WatchStopHandle = () => void
@@ -172,16 +171,8 @@ export function watch<T = any, Immediate extends Readonly<boolean> = false>(
172171
function doWatch(
173172
source: WatchSource | WatchSource[] | WatchEffect | object,
174173
cb: WatchCallback | null,
175-
{ immediate, deep, flush, once, onTrack, onTrigger }: WatchOptions = EMPTY_OBJ
174+
{ immediate, deep, flush, onTrack, onTrigger }: WatchOptions = EMPTY_OBJ
176175
): WatchStopHandle {
177-
if (cb && once) {
178-
const _cb = cb
179-
cb = (...args) => {
180-
_cb(...args)
181-
unwatch()
182-
}
183-
}
184-
185176
if (__DEV__ && !cb) {
186177
if (immediate !== undefined) {
187178
warn(
@@ -195,12 +186,6 @@ function doWatch(
195186
`watch(source, callback, options?) signature.`
196187
)
197188
}
198-
if (once !== undefined) {
199-
warn(
200-
`watch() "once" option is only respected when using the ` +
201-
`watch(source, callback, options?) signature.`
202-
)
203-
}
204189
}
205190

206191
const warnInvalidSource = (s: unknown) => {
@@ -380,13 +365,6 @@ function doWatch(
380365
onScheduled(schedulerJob as any)
381366
})
382367

383-
const unwatch = () => {
384-
effect.stop()
385-
if (instance && instance.scope) {
386-
remove(instance.scope.effects!, effect)
387-
}
388-
}
389-
390368
if (__DEV__) {
391369
effect.onTrack = onTrack
392370
effect.onTrigger = onTrigger
@@ -408,6 +386,13 @@ function doWatch(
408386
effect.run()
409387
}
410388

389+
const unwatch = () => {
390+
effect.stop()
391+
if (instance && instance.scope) {
392+
remove(instance.scope.effects!, effect)
393+
}
394+
}
395+
411396
if (__SSR__ && ssrCleanup) ssrCleanup.push(unwatch)
412397
return unwatch
413398
}

0 commit comments

Comments
 (0)