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/useExtractedObservable.md
1---2category: '@RxJS'3---45# useExtractedObservable67Use an RxJS [`Observable`](https://rxjs.dev/guide/observable) as extracted from one or more composables, return a `ref`,8and automatically unsubscribe from it when the component is unmounted.910Automatically unsubscribe on observable change, and automatically unsubscribe from it when the component is unmounted.1112Supports signatures that match all overloads13of [`watch`](https://vuejs.org/guide/essentials/watchers.html#basic-example).1415## Usage1617<!-- TODO: import rxjs error if enable twoslash -->1819```ts no-twoslash20import { useExtractedObservable } from '@vueuse/rxjs'21import ObservableSocket from 'observable-socket'22import { computed } from 'vue'23import { makeSocket, useUser } from '../some/lib/func'2425// setup()26const user = useUser()27const lastMessage = useExtractedObservable(user, u => ObservableSocket.create(makeSocket(u.id)).down)28```2930If you want to add custom error handling to an `Observable` that might error, you can supply an optional `onError`31configuration. Without this, RxJS will treat any error in the supplied `Observable` as an "unhandled error" and it will32be thrown in a new call stack and reported to `window.onerror` (or `process.on('error')` if you happen to be in Node).3334```ts no-twoslash35import { useExtractedObservable } from '@vueuse/rxjs'36import { interval } from 'rxjs'37import { mapTo, scan, startWith, tap } from 'rxjs/operators'38import { shallowRef } from 'vue'3940// setup()41const start = shallowRef(0)4243const count = useExtractedObservable(44start,45(start) => {46return interval(1000).pipe(47mapTo(1),48startWith(start),49scan((total, next) => next + total),50tap((n) => {51if (n === 10)52throw new Error('oops')53})54)55},56{57onError: (err) => {58console.log(err.message) // "oops"59},60}61)62```6364You can also supply an optional `onComplete` configuration if you need to attach special behavior when the watched65observable completes.6667```ts no-twoslash68import { useExtractedObservable } from '@vueuse/rxjs'69import { interval } from 'rxjs'70import { mapTo, scan, startWith, takeWhile } from 'rxjs/operators'71import { shallowRef } from 'vue'7273// setup()74const start = shallowRef(0)7576const count = useExtractedObservable(77start,78(start) => {79return interval(1000).pipe(80mapTo(1),81startWith(start),82scan((total, next) => next + total),83takeWhile(num => num < 10)84)85},86{87onComplete: () => {88console.log('Done!')89},90}91)92```9394If you want, you can also pass `watch` options as the last argument:9596```ts no-twoslash97import { useExtractedObservable } from '@vueuse/rxjs'98import { interval } from 'rxjs'99import { mapTo, scan, startWith, takeWhile } from 'rxjs/operators'100import { shallowRef } from 'vue'101102// setup()103const start = shallowRef<number>()104105const count = useExtractedObservable(106start,107(start) => {108return interval(1000).pipe(109mapTo(1),110startWith(start),111scan((total, next) => next + total),112takeWhile(num => num < 10)113)114},115{},116{117immediate: false118}119)120```121122## Options123124| Option | Type | Description |125| -------------- | -------------------- | ---------------------------------------- |126| `initialValue` | `T` | Value to use before the Observable emits |127| `onError` | `(err: any) => void` | Error handler for Observable errors |128| `onComplete` | `() => void` | Called when the Observable completes |129130## Return Value131132Returns a readonly `ShallowRef` containing the latest value emitted by the extracted Observable.133134## Type Declarations135136```ts137export interface UseExtractedObservableOptions<138E,139> extends UseObservableOptions<E> {140onComplete?: () => void141}142export declare function useExtractedObservable<143T extends MultiWatchSources,144E,145Immediate extends Readonly<boolean> = false,146>(147sources: [...T],148extractor: WatchExtractedObservableCallback<149MapSources<T>,150MapOldSources<T, Immediate>,151E152>,153options?: UseExtractedObservableOptions<E>,154watchOptions?: WatchOptions<Immediate>,155): Readonly<ShallowRef<E>>156export declare function useExtractedObservable<157T extends Readonly<MultiWatchSources>,158E,159Immediate extends Readonly<boolean> = false,160>(161sources: T,162extractor: WatchExtractedObservableCallback<163MapSources<T>,164MapOldSources<T, Immediate>,165E166>,167options?: UseExtractedObservableOptions<E>,168watchOptions?: WatchOptions<Immediate>,169): Readonly<ShallowRef<E>>170export declare function useExtractedObservable<171T,172E,173Immediate extends Readonly<boolean> = false,174>(175sources: WatchSource<T>,176extractor: WatchExtractedObservableCallback<177T,178Immediate extends true ? T | undefined : T,179E180>,181options?: UseExtractedObservableOptions<E>,182watchOptions?: WatchOptions<Immediate>,183): Readonly<ShallowRef<E>>184export declare function useExtractedObservable<185T extends object,186E,187Immediate extends Readonly<boolean> = false,188>(189sources: T,190extractor: WatchExtractedObservableCallback<191T,192Immediate extends true ? T | undefined : T,193E194>,195options?: UseExtractedObservableOptions<E>,196watchOptions?: WatchOptions<Immediate>,197): Readonly<ShallowRef<E>>198```199