Skip to content

watchExtractedObservable

Category
Export Size
250 B
Package
@vueuse/rxjs
Last Changed
3 weeks ago

Watch the values of an RxJS Observable as extracted from one or more composables.

Automatically unsubscribe on observable change, and automatically unsubscribe from it when the component is unmounted.

Supports all overloads of watch. Available in the @vueuse/rxjs add-on.

Usage

ts
import { watchExtractedObservable } from '@vueuse/rxjs'
import { computed, reactive, useTemplateRef } from 'vue'
import { AudioPlayer } from '../my/libs/AudioPlayer'

// setup()

const audio = useTemplateRef('audio')
const player = computed(() => (audio.value ? new AudioPlayer(audio.value) : null))
const state = reactive({
  progress: 0,
})

watchExtractedObservable(player, p => p.progress$, (percentage) => {
  state.progress = percentage * 100
})

If 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).

You can also supply an optional onComplete configuration if you need to attach special behavior when the watched observable completes.

ts
import { watchExtractedObservable } from '@vueuse/rxjs'
import { computed, reactive, useTemplateRef } from 'vue'
import { AudioPlayer } from '../my/libs/AudioPlayer'

// setup()

const audio = useTemplateRef('audio')
const player = computed(() => (audio.value ? new AudioPlayer(audio.value) : null))
const state = reactive({
  progress: 0,
})

watchExtractedObservable(player, p => p.progress$, (percentage) => {
  state.progress = percentage * 100
}, {
  onError: (err: unknown) => {
    console.error(err)
  },
  onComplete: () => {
    state.progress = 100 // or 0, or whatever
  },
})
js
import { watchExtractedObservable } from '@vueuse/rxjs'
import { computed, reactive, useTemplateRef } from 'vue'
import { AudioPlayer } from '../my/libs/AudioPlayer'
// setup()
const audio = useTemplateRef('audio')
const player = computed(() =>
  audio.value ? new AudioPlayer(audio.value) : null,
)
const state = reactive({
  progress: 0,
})
watchExtractedObservable(
  player,
  (p) => p.progress$,
  (percentage) => {
    state.progress = percentage * 100
  },
  {
    onError: (err) => {
      console.error(err)
    },
    onComplete: () => {
      state.progress = 100 // or 0, or whatever
    },
  },
)

If you want, you can also pass watch options as the last argument:

ts
import { watchExtractedObservable } from '@vueuse/rxjs'
import { computed, reactive, useTemplateRef } from 'vue'
import { AudioPlayer } from '../my/libs/AudioPlayer'

// setup()

const audio = useTemplateRef('audio')
const player = computed(() => (audio.value ? new AudioPlayer(audio.value) : null))
const state = reactive({
  progress: 0,
})

watchExtractedObservable(player, p => p.progress$, (percentage) => {
  state.progress = percentage * 100
}, {
  onError: (err: unknown) => {
    console.error(err)
  }
}, {
  immediate: true
})
js
import { watchExtractedObservable } from '@vueuse/rxjs'
import { computed, reactive, useTemplateRef } from 'vue'
import { AudioPlayer } from '../my/libs/AudioPlayer'
// setup()
const audio = useTemplateRef('audio')
const player = computed(() =>
  audio.value ? new AudioPlayer(audio.value) : null,
)
const state = reactive({
  progress: 0,
})
watchExtractedObservable(
  player,
  (p) => p.progress$,
  (percentage) => {
    state.progress = percentage * 100
  },
  {
    onError: (err) => {
      console.error(err)
    },
  },
  {
    immediate: true,
  },
)

Subscription Options

OptionTypeDescription
onError(err: unknown) => voidError handler for Observable errors
onComplete() => voidCalled when the Observable completes

Return Value

Returns a WatchHandle that can be used to stop watching:

ts
import { watchExtractedObservable } from '@vueuse/rxjs'
import { ref } from 'vue'

const source = ref({ data$: null })

const stop = watchExtractedObservable(source, s => s.data$, (data) => {
  console.log(data)
})

// Later, stop watching
stop()

Type Declarations

Show Type Declarations
ts
export type 
OnCleanup
= (
cleanupFn
: () => void) => void
export type
WatchExtractedObservableCallback
<
Value
,
OldValue
,
ObservableElement
,
> = (
value
:
NonNullable
<
Value
>,
oldValue
:
OldValue
,
onCleanup
:
OnCleanup
,
) =>
Observable
<
ObservableElement
>
export interface WatchExtractedObservableOptions {
onError
?: (
err
: unknown) => void
onComplete
?: () => void
} export declare function
watchExtractedObservable
<
T
extends
MultiWatchSources
,
E
,
Immediate
extends
Readonly
<boolean> = false,
>(
sources
: [...
T
],
extractor
:
WatchExtractedObservableCallback
<
MapSources
<
T
>,
MapOldSources
<
T
,
Immediate
>,
E
>,
callback
: (
snapshot
:
E
) => void,
subscriptionOptions
?: WatchExtractedObservableOptions,
watchOptions
?:
WatchOptions
<
Immediate
>,
):
WatchHandle
export declare function
watchExtractedObservable
<
T
extends
Readonly
<
MultiWatchSources
>,
E
,
Immediate
extends
Readonly
<boolean> = false,
>(
source
:
T
,
extractor
:
WatchExtractedObservableCallback
<
MapSources
<
T
>,
MapOldSources
<
T
,
Immediate
>,
E
>,
callback
: (
snapshot
:
E
) => void,
subscriptionOptions
?: WatchExtractedObservableOptions,
watchOptions
?:
WatchOptions
<
Immediate
>,
):
WatchHandle
export declare function
watchExtractedObservable
<
T
,
E
,
Immediate
extends
Readonly
<boolean> = false,
>(
source
:
WatchSource
<
T
>,
extractor
:
WatchExtractedObservableCallback
<
T
,
Immediate
extends true ?
T
| undefined :
T
,
E
>,
callback
: (
snapshot
:
E
) => void,
subscriptionOptions
?: WatchExtractedObservableOptions,
watchOptions
?:
WatchOptions
<
Immediate
>,
):
WatchHandle
export declare function
watchExtractedObservable
<
T
extends object,
E
,
Immediate
extends
Readonly
<boolean> = false,
>(
source
:
T
,
extractor
:
WatchExtractedObservableCallback
<
T
,
Immediate
extends true ?
T
| undefined :
T
,
E
>,
callback
: (
snapshot
:
E
) => void,
subscriptionOptions
?: WatchExtractedObservableOptions,
watchOptions
?:
WatchOptions
<
Immediate
>,
):
WatchHandle

Source

SourceDocs

Contributors

Anthony Fu
Anthony Fu
Vida Xie
Arthur Darkstone
Kricsleo
SerKo
Robin
IlyaL
James Garbutt
OrbisK
Voltra

Changelog

v14.0.0-alpha.3 on
b8102 - feat(watch): update watch return typo in watchExtractedObservable, watchDebounced, watchDeep, watchImmediate, watchOnce, watchThrottled and watchWithFilter (#4896)
v14.0.0-alpha.0 on
00a72 - fix(types): update type casting for watch functions to use WatchSource (#4966)
v12.0.0-beta.1 on
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
v10.5.0 on
23b8c - feat(rxjs): add useExtractedObservable and watchExtractedObservable (#3453)

Released under the MIT License.

Ship Faster with AI Dev Workflows
Flagship advanced AI course is coming soon
Join Waitlist