Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/watchExtractedObservable.md
1---2category: '@RxJS'3---45# watchExtractedObservable67Watch the values of an RxJS [`Observable`](https://rxjs.dev/guide/observable) as extracted from one or more composables.89Automatically unsubscribe on observable change, and automatically unsubscribe from it when the component is unmounted.1011Supports all overloads of [`watch`](https://vuejs.org/guide/essentials/watchers.html#basic-example).1213## Usage1415<!-- TODO: import rxjs error if enable twoslash -->1617```ts no-twoslash18import { watchExtractedObservable } from '@vueuse/rxjs'19import { computed, reactive, useTemplateRef } from 'vue'20import { AudioPlayer } from '../my/libs/AudioPlayer'2122// setup()2324const audio = useTemplateRef('audio')25const player = computed(() => (audio.value ? new AudioPlayer(audio.value) : null))26const state = reactive({27progress: 0,28})2930watchExtractedObservable(player, p => p.progress$, (percentage) => {31state.progress = percentage * 10032})33```3435If you want to add custom error handling to an `Observable` that might error, you can supply an optional `onError` configuration. Without this, RxJS will treat any error in the supplied `Observable` as an "unhandled error" and it will be thrown in a new call stack and reported to `window.onerror` (or `process.on('error')` if you happen to be in Node).3637You can also supply an optional `onComplete` configuration if you need to attach special behavior when the watched observable completes.3839```ts no-twoslash40import { watchExtractedObservable } from '@vueuse/rxjs'41import { computed, reactive, useTemplateRef } from 'vue'42import { AudioPlayer } from '../my/libs/AudioPlayer'4344// setup()4546const audio = useTemplateRef('audio')47const player = computed(() => (audio.value ? new AudioPlayer(audio.value) : null))48const state = reactive({49progress: 0,50})5152watchExtractedObservable(player, p => p.progress$, (percentage) => {53state.progress = percentage * 10054}, {55onError: (err: unknown) => {56console.error(err)57},58onComplete: () => {59state.progress = 100 // or 0, or whatever60},61})62```6364If you want, you can also pass `watch` options as the last argument:6566```ts no-twoslash67import { watchExtractedObservable } from '@vueuse/rxjs'68import { computed, reactive, useTemplateRef } from 'vue'69import { AudioPlayer } from '../my/libs/AudioPlayer'7071// setup()7273const audio = useTemplateRef('audio')74const player = computed(() => (audio.value ? new AudioPlayer(audio.value) : null))75const state = reactive({76progress: 0,77})7879watchExtractedObservable(player, p => p.progress$, (percentage) => {80state.progress = percentage * 10081}, {82onError: (err: unknown) => {83console.error(err)84}85}, {86immediate: true87})88```8990## Subscription Options9192| Option | Type | Description |93| ------------ | ------------------------ | ------------------------------------ |94| `onError` | `(err: unknown) => void` | Error handler for Observable errors |95| `onComplete` | `() => void` | Called when the Observable completes |9697## Return Value9899Returns a `WatchHandle` that can be used to stop watching:100101```ts no-twoslash102import { watchExtractedObservable } from '@vueuse/rxjs'103import { ref } from 'vue'104105const source = ref({ data$: null })106107const stop = watchExtractedObservable(source, s => s.data$, (data) => {108console.log(data)109})110111// Later, stop watching112stop()113```114115## Type Declarations116117```ts118export type OnCleanup = (cleanupFn: () => void) => void119export type WatchExtractedObservableCallback<120Value,121OldValue,122ObservableElement,123> = (124value: NonNullable<Value>,125oldValue: OldValue,126onCleanup: OnCleanup,127) => Observable<ObservableElement>128export interface WatchExtractedObservableOptions {129onError?: (err: unknown) => void130onComplete?: () => void131}132export declare function watchExtractedObservable<133T extends MultiWatchSources,134E,135Immediate extends Readonly<boolean> = false,136>(137sources: [...T],138extractor: WatchExtractedObservableCallback<139MapSources<T>,140MapOldSources<T, Immediate>,141E142>,143callback: (snapshot: E) => void,144subscriptionOptions?: WatchExtractedObservableOptions,145watchOptions?: WatchOptions<Immediate>,146): WatchHandle147export declare function watchExtractedObservable<148T extends Readonly<MultiWatchSources>,149E,150Immediate extends Readonly<boolean> = false,151>(152source: T,153extractor: WatchExtractedObservableCallback<154MapSources<T>,155MapOldSources<T, Immediate>,156E157>,158callback: (snapshot: E) => void,159subscriptionOptions?: WatchExtractedObservableOptions,160watchOptions?: WatchOptions<Immediate>,161): WatchHandle162export declare function watchExtractedObservable<163T,164E,165Immediate extends Readonly<boolean> = false,166>(167source: WatchSource<T>,168extractor: WatchExtractedObservableCallback<169T,170Immediate extends true ? T | undefined : T,171E172>,173callback: (snapshot: E) => void,174subscriptionOptions?: WatchExtractedObservableOptions,175watchOptions?: WatchOptions<Immediate>,176): WatchHandle177export declare function watchExtractedObservable<178T extends object,179E,180Immediate extends Readonly<boolean> = false,181>(182source: T,183extractor: WatchExtractedObservableCallback<184T,185Immediate extends true ? T | undefined : T,186E187>,188callback: (snapshot: E) => void,189subscriptionOptions?: WatchExtractedObservableOptions,190watchOptions?: WatchOptions<Immediate>,191): WatchHandle192```193