mirror of
https://github.com/re-rxjs/react-rxjs.git
synced 2025-12-08 18:01:51 +00:00
Only batch updates for the React subscription
This commit is contained in:
parent
dd3fc99564
commit
8e4723e991
@ -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)
|
||||
})
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user