Skip to content

Commit

Permalink
feat(fromEvent): support HTMLElement (#2048)
Browse files Browse the repository at this point in the history
Co-authored-by: luolei <luolei@kuaishou.com>
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
3 people committed Oct 5, 2022
1 parent 5ce2f6d commit 554a3cf
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions packages/rxjs/from/index.ts
Expand Up @@ -2,6 +2,7 @@ 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> {
Expand All @@ -19,9 +20,12 @@ export function from<T>(value: ObservableInput<T> | Ref<T>, watchOptions?: Watch
}
}

export function fromEvent<T extends HTMLElement>(value: Ref<T>, event: string): Observable<Event> {
return from(value, { immediate: true }).pipe(
filter(value => value instanceof HTMLElement),
mergeMap(value => fromEventRx(value, event)),
)
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)
}

0 comments on commit 554a3cf

Please sign in to comment.