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/toObserver.md
1---2category: '@RxJS'3---45# toObserver67Sugar function to convert a `ref` into an RxJS [Observer](https://rxjs.dev/guide/observer).89## Usage1011<!-- TODO: import rxjs error if enable twoslash -->1213```ts no-twoslash14import { from, fromEvent, toObserver, useSubscription } from '@vueuse/rxjs'15import { interval } from 'rxjs'16import { map, mapTo, startWith, takeUntil, withLatestFrom } from 'rxjs/operators'17import { shallowRef, useTemplateRef } from 'vue'1819const count = shallowRef(0)20const button = useTemplateRef('buttonRef')2122useSubscription(23interval(1000)24.pipe(25mapTo(1),26takeUntil(fromEvent(button, 'click')),27withLatestFrom(from(count).pipe(startWith(0))),28map(([curr, total]) => curr + total),29)30.subscribe(toObserver(count)), // same as ).subscribe(val => (count.value = val))31)32```3334## Type Declarations3536```ts37export declare function toObserver<T>(value: Ref<T>): NextObserver<T>38```39