Skip to content

Commit

Permalink
feat(rxjs): improve rxjs from (vitest-dev#987)
Browse files Browse the repository at this point in the history
Co-authored-by: Jan Morawietz <jan.morawietz@paessler.com>
  • Loading branch information
DesselBane and Jan Morawietz committed Dec 8, 2021
1 parent fa3cab3 commit fbcbef6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
8 changes: 5 additions & 3 deletions packages/rxjs/from/_demo.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<script setup lang="ts">
import { defineComponent, onMounted, Ref, ref } from 'vue-demi'
import { onMounted, Ref, ref } from 'vue-demi'
import { interval } from 'rxjs'
import {
mapTo,
takeUntil,
withLatestFrom,
startWith,
map,
} from 'rxjs/operators'
import { useSubscription } from '../useSubscription'
Expand All @@ -21,7 +20,10 @@ onMounted(() => {
.pipe(
mapTo(1),
takeUntil(fromEvent(button as Ref<HTMLButtonElement>, 'click')),
withLatestFrom(from(count).pipe(startWith(0))),
withLatestFrom(from(count, {
immediate: true,
deep: false,
})),
map(([total, curr]) => curr + total),
)
.subscribe(toObserver(count)),
Expand Down
5 changes: 4 additions & 1 deletion packages/rxjs/from/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ useSubscription(
.pipe(
mapTo(1),
takeUntil(fromEvent(button, 'click')),
withLatestFrom(from(count).pipe(startWith(0))),
withLatestFrom(from(count, {
immediate: true,
deep: false,
})),
map(([total, curr]) => curr + total),
)
.subscribe(toObserver(count)) // same as ).subscribe(val => (count.value = val))
Expand Down
10 changes: 7 additions & 3 deletions packages/rxjs/from/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { from as fromRxjs, fromEvent as fromEventRx, Observable } from 'rxjs'
import type { ObservableInput } from 'rxjs'
import { filter, mergeMap } from 'rxjs/operators'
import { Ref, isRef, watch } from 'vue-demi'
import { Ref, isRef, watch, WatchOptions } from 'vue-demi'

export function from<T>(value: ObservableInput<T> | Ref<T>): Observable<T> {
export function from<T>(value: ObservableInput<T> | Ref<T>, watchOptions?: WatchOptions): Observable<T> {
if (isRef<T>(value)) {
return new Observable((subscriber) => {
watch(value, val => subscriber.next(val))
const watchStopHandle = watch(value, val => subscriber.next(val), watchOptions)

return () => {
watchStopHandle()
}
})
}
else {
Expand Down

0 comments on commit fbcbef6

Please sign in to comment.