@cran/lib.vue.ref
Version:
Vue Reactivity Extensions
19 lines (15 loc) • 460 B
text/typescript
import { type WatchCallback, type WatchOptions, type WatchSource, watch } from "vue";
/**
* @since 0.0.1
* @category Watch
*/
export function watchOnce<T, Immediate extends Readonly<boolean> = false> (
source :WatchSource<T>,
cb: WatchCallback<T, Immediate extends true ? T | undefined : T>,
options?: WatchOptions<Immediate>
) {
const stop = watch(source, function onChange ( ...args ) {
stop(); cb(...args);
}, options);
return stop;
}