-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
index.ts
31 lines (28 loc) · 1013 Bytes
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { Observable, fromEvent as fromEventRx, from as fromRxjs } from 'rxjs'
import type { ObservableInput } from 'rxjs'
import { filter, mergeMap } from 'rxjs/operators'
import type { Ref, WatchOptions } from 'vue-demi'
import type { MaybeRef } from '@vueuse/shared'
import { isRef, watch } from 'vue-demi'
export function from<T>(value: ObservableInput<T> | Ref<T>, watchOptions?: WatchOptions): Observable<T> {
if (isRef<T>(value)) {
return new Observable((subscriber) => {
const watchStopHandle = watch(value, val => subscriber.next(val), watchOptions)
return () => {
watchStopHandle()
}
})
}
else {
return fromRxjs(value)
}
}
export function fromEvent<T extends HTMLElement>(value: MaybeRef<T>, event: string): Observable<Event> {
if (isRef<T>(value)) {
return from(value, { immediate: true }).pipe(
filter(value => value instanceof HTMLElement),
mergeMap(value => fromEventRx(value, event)),
)
}
return fromEventRx(value, event)
}