pausableWatch
Pausable watch
Demo
Type something below to trigger the watch
Log
Usage
Use as normal the watch
, but return extra pause()
and resume()
functions to control.
import { pausableWatch } from '@vueuse/core'
import { ref, nextTick } from 'vue'
const source = ref('foo')
const { stop, pause, resume } = pausableWatch(
source,
(v) => console.log(`Changed to ${v}!`),
)
source.value = 'bar'
await nextTick() // Changed to bar!
pause()
source.value = 'foobar'
await nextTick() // (nothing happend)
resume()
source.value = 'hello'
await nextTick() // Changed to hello!
import { pausableWatch } from '@vueuse/core'
import { ref, nextTick } from 'vue'
const source = ref('foo')
const { stop, pause, resume } = pausableWatch(
source,
(v) => console.log(`Changed to ${v}!`),
)
source.value = 'bar'
await nextTick() // Changed to bar!
pause()
source.value = 'foobar'
await nextTick() // (nothing happend)
resume()
source.value = 'hello'
await nextTick() // Changed to hello!
Type Declarations
export interface PausableWatchReturn extends Pausable {
stop: WatchStopHandle
}
export declare function pausableWatch<
T extends Readonly<WatchSource<unknown>[]>,
Immediate extends Readonly<boolean> = false
>(
sources: T,
cb: WatchCallback<MapSources<T>, MapOldSources<T, Immediate>>,
options?: WatchWithFilterOptions<Immediate>
): PausableWatchReturn
export declare function pausableWatch<
T,
Immediate extends Readonly<boolean> = false
>(
source: WatchSource<T>,
cb: WatchCallback<T, Immediate extends true ? T | undefined : T>,
options?: WatchWithFilterOptions<Immediate>
): PausableWatchReturn
export declare function pausableWatch<
T extends object,
Immediate extends Readonly<boolean> = false
>(
source: T,
cb: WatchCallback<T, Immediate extends true ? T | undefined : T>,
options?: WatchWithFilterOptions<Immediate>
): PausableWatchReturn
export interface PausableWatchReturn extends Pausable {
stop: WatchStopHandle
}
export declare function pausableWatch<
T extends Readonly<WatchSource<unknown>[]>,
Immediate extends Readonly<boolean> = false
>(
sources: T,
cb: WatchCallback<MapSources<T>, MapOldSources<T, Immediate>>,
options?: WatchWithFilterOptions<Immediate>
): PausableWatchReturn
export declare function pausableWatch<
T,
Immediate extends Readonly<boolean> = false
>(
source: WatchSource<T>,
cb: WatchCallback<T, Immediate extends true ? T | undefined : T>,
options?: WatchWithFilterOptions<Immediate>
): PausableWatchReturn
export declare function pausableWatch<
T extends object,
Immediate extends Readonly<boolean> = false
>(
source: T,
cb: WatchCallback<T, Immediate extends true ? T | undefined : T>,
options?: WatchWithFilterOptions<Immediate>
): PausableWatchReturn
Source
Contributors
Anthony Fu
wheat