watchExtractedObservable
監看從一個或多個 composable 提取的 RxJS Observable
的值。
在 observable 變更時自動取消訂閱,並在組件卸載時自動從中取消訂閱。
支援 watch
的所有重載。在 @vueuse/rxjs 附加元件中可用。
用法
ts
import { watchExtractedObservable } from '@vueuse/rxjs'
import { computed, reactive, shallowRef } from 'vue'
import { AudioPlayer } from '../my/libs/AudioPlayer'
// setup()
const audio = shallowRef<HTMLAudioElement>()
const player = computed(() => (audio.value ? new AudioPlayer(audio) : null))
const state = reactive({
progress: 0,
})
watchExtractedObservable(player, p => p.progress$, (percentage) => {
state.progress = percentage * 100
})
js
import { watchExtractedObservable } from '@vueuse/rxjs'
import { computed, reactive, shallowRef } from 'vue'
import { AudioPlayer } from '../my/libs/AudioPlayer'
// setup()
const audio = shallowRef()
const player = computed(() => (audio.value ? new AudioPlayer(audio) : null))
const state = reactive({
progress: 0,
})
watchExtractedObservable(
player,
(p) => p.progress$,
(percentage) => {
state.progress = percentage * 100
},
)
如果您想為可能發生錯誤的 Observable
添加自定義錯誤處理,您可以提供可選的 onError
配置。 如果沒有這個,RxJS 會將提供的 Observable
中的任何錯誤視為「未處理的錯誤」,它將在新的調用堆疊中拋出並報告給 window.onerror
(如果您碰巧在 Node 中,則報告給 process.on('error')
)。
如果您需要在監看的 observable 完成時附加特殊行為,您還可以提供可選的 onComplete
配置。
ts
import { watchExtractedObservable } from '@vueuse/rxjs'
import { computed, reactive, shallowRef } from 'vue'
import { AudioPlayer } from '../my/libs/AudioPlayer'
// setup()
const audio = shallowRef<HTMLAudioElement>()
const player = computed(() => (audio.value ? new AudioPlayer(audio) : 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, shallowRef } from 'vue'
import { AudioPlayer } from '../my/libs/AudioPlayer'
// setup()
const audio = shallowRef()
const player = computed(() => (audio.value ? new AudioPlayer(audio) : 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
},
},
)
如果需要,您也可以將 watch
選項作為最後一個參數傳遞
ts
import { watchExtractedObservable } from '@vueuse/rxjs'
import { computed, reactive, shallowRef } from 'vue'
import { AudioPlayer } from '../my/libs/AudioPlayer'
// setup()
const audio = shallowRef<HTMLAudioElement>()
const player = computed(() => (audio.value ? new AudioPlayer(audio) : 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, shallowRef } from 'vue'
import { AudioPlayer } from '../my/libs/AudioPlayer'
// setup()
const audio = shallowRef()
const player = computed(() => (audio.value ? new AudioPlayer(audio) : null))
const state = reactive({
progress: 0,
})
watchExtractedObservable(
player,
(p) => p.progress$,
(percentage) => {
state.progress = percentage * 100
},
{
onError: (err) => {
console.error(err)
},
},
{
immediate: true,
},
)
類型宣告
顯示類型宣告
typescript
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>,
): WatchStopHandle
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>,
): WatchStopHandle
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>,
): WatchStopHandle
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>,
): WatchStopHandle