コンテンツにスキップ

toObserver

カテゴリ
エクスポートサイズ
88 B (バイト)
パッケージ
@vueuse/rxjs
最終更新日
先週

ref を RxJS の Observer に変換する糖衣構文関数。 @vueuse/rxjs アドオンで利用可能です。

使用方法

ts
import { from, fromEvent, toObserver, useSubscription } from '@vueuse/rxjs'
import { interval } from 'rxjs'
import { map, mapTo, startWith, takeUntil, withLatestFrom } from 'rxjs/operators'
import { ref } from 'vue'

const count = ref(0)
const button = ref<HTMLButtonElement>(null)

useSubscription(
  interval(1000)
    .pipe(
      mapTo(1),
      takeUntil(fromEvent(button, 'click')),
      withLatestFrom(from(count).pipe(startWith(0))),
      map(([curr, total]) => curr + total),
    )
    .subscribe(toObserver(count)), // same as ).subscribe(val => (count.value = val))
)
js
import { from, fromEvent, toObserver, useSubscription } from '@vueuse/rxjs'
import { interval } from 'rxjs'
import {
  map,
  mapTo,
  startWith,
  takeUntil,
  withLatestFrom,
} from 'rxjs/operators'
import { ref } from 'vue'
const count = ref(0)
const button = ref(null)
useSubscription(
  interval(1000)
    .pipe(
      mapTo(1),
      takeUntil(fromEvent(button, 'click')),
      withLatestFrom(from(count).pipe(startWith(0))),
      map(([curr, total]) => curr + total),
    )
    .subscribe(toObserver(count)),
)

型宣言

typescript
export declare function toObserver<T>(value: Ref<T>): NextObserver<T>

ソース

ソースドキュメント

コントリビューター

Anthony Fu
Anthony Fu
Theodros Zelleke
Curt Grimes
yang
Michel Betancourt

変更履歴

v12.0.0-beta.1 2024/11/21
0a9ed - feat!: Vue 2 サポートの削除、バンドルの最適化、クリーンアップ (#4349)

MITライセンスでリリースされています。