fn

watchPausable

v0.0.14testeddemo

A watch whose execution can be paused and resumed on demand via a pausable event filter.

Example

ts
const count = ref(0);
const { pause, resume, isActive } = watchPausable(count, (value) => {
  console.log('changed to', value);
});

pause();
count.value++; // callback not called
resume();
count.value++; // callback called

Demo

Loading demo…

Signatures

ts
export function watchPausable<T extends Readonly<MultiWatchSources>, Immediate extends Readonly<boolean> = false>(
  sources: [...T],
  cb: WatchCallback<MapSources<T>, MapOldSources<T, Immediate>>,
  options?: UseWatchPausableOptions<Immediate>,
): UseWatchPausableReturn;
ts
export function watchPausable<T, Immediate extends Readonly<boolean> = false>(
  source: WatchSource<T>,
  cb: WatchCallback<T, Immediate extends true ? T | undefined : T>,
  options?: UseWatchPausableOptions<Immediate>,
): UseWatchPausableReturn;
ts
export function watchPausable<T extends object, Immediate extends Readonly<boolean> = false>(
  source: T,
  cb: WatchCallback<T, Immediate extends true ? T | undefined : T>,
  options?: UseWatchPausableOptions<Immediate>,
): UseWatchPausableReturn;

Type Parameters

Immediateextends Readonly<boolean>= false

Parameters

ParameterTypeDescription
sourceWatchSource<unknown> | MultiWatchSources | objectThe watch source (ref, getter, reactive object, or an array of sources)
cbAnyFunctionThe callback invoked when an active source changes
options?UseWatchPausableOptions<Immediate>Watch options plus eventFilter and initialState

Returns

UseWatchPausableReturn{ stop, pause, resume, isActive }
PropertyTypeDescription
isActiveReadonly<Ref<boolean>>Whether the watcher is currently active. While false, source changes are ignored and the callback is not invoked.
pause() => voidPause the watcher. Changes to the source are ignored until resume.
resume() => voidResume the watcher so it reacts to source changes again.
stopWatchStopHandleStop the watcher entirely. It cannot be restarted afterwards.