Only batch updates for the React subscription

This commit is contained in:
Josep M Sobrepere 2020-05-04 23:09:02 +02:00
parent dd3fc99564
commit 8e4723e991
3 changed files with 10 additions and 9 deletions

View File

@ -1,6 +1,6 @@
import { Observable } from "rxjs"
import { useEffect, useState } from "react"
import reactOperator from "./react-operator"
import reactOperator, { batchUpdates } from "./react-operator"
export function connectFactoryObservable<
I,
@ -48,7 +48,9 @@ export function connectFactoryObservable<
}, suspenseTime)
}
const subscription = getReactObservable$(...input).subscribe(value => {
const subscription = batchUpdates(
getReactObservable$(...input),
).subscribe(value => {
setValue(value)
if (timeoutToken !== null) clearTimeout(timeoutToken)
})

View File

@ -1,6 +1,6 @@
import { Observable } from "rxjs"
import { useEffect, useState } from "react"
import reactOperator from "./react-operator"
import reactOperator, { batchUpdates } from "./react-operator"
export function connectObservable<O, IO>(
observable: Observable<O>,
@ -13,7 +13,7 @@ export function connectObservable<O, IO>(
reactObservable$.getCurrentValue(),
)
useEffect(() => {
const subscription = reactObservable$.subscribe(setValue)
const subscription = batchUpdates(reactObservable$).subscribe(setValue)
return () => subscription.unsubscribe()
}, [])
return value

View File

@ -5,9 +5,9 @@ export interface ReactObservable<O, IO> extends Observable<O> {
getCurrentValue: () => O | IO
}
const batchUpdates: <T>(source: Observable<T>) => Observable<T> = debounceTime(
0,
)
export const batchUpdates: <T>(
source: Observable<T>,
) => Observable<T> = debounceTime(0)
const GRACE_PERIOD = 100
const reactOperator = <T, I>(
@ -15,7 +15,6 @@ const reactOperator = <T, I>(
initialValue: I,
teardown?: () => void,
): ReactObservable<T, I> => {
const batchedSource$ = batchUpdates(source$)
let subject: ReplaySubject<T> | undefined
let subscription: Subscription | undefined
let timeoutToken: NodeJS.Timeout | undefined = undefined
@ -31,7 +30,7 @@ const reactOperator = <T, I>(
if (!subject || hasError) {
hasError = false
subject = new ReplaySubject<T>(1)
subscription = batchedSource$.subscribe({
subscription = source$.subscribe({
next(value) {
currentValue = value
subject!.next(value)