fix(core): ensure the same observable on useObservable

This commit is contained in:
Josep M Sobrepere 2020-10-14 19:11:30 +02:00
parent c01cd43cb0
commit ce05cf36b4
3 changed files with 6 additions and 3 deletions

View File

@ -80,7 +80,7 @@ export default function connectFactoryObservable<A extends [], O>(
return [
(...input: A) => {
const [source$, getValue] = getSharedObservables$(input)
return useObservable(source$, getValue)
return useObservable(source$, getValue, input)
},
(...input: A) => getSharedObservables$(input)[0],
]

View File

@ -18,9 +18,11 @@ import { useObservable } from "../internal/useObservable"
* subscription, then the hook will leverage React Suspense while it's waiting
* for the first value.
*/
const emptyArr: Array<any> = []
export default function connectObservable<T>(observable: Observable<T>) {
const sharedObservable$ = shareLatest<T>(observable, false)
const getValue = reactEnhancer(sharedObservable$)
const useStaticObservable = () => useObservable(sharedObservable$, getValue)
const useStaticObservable = () =>
useObservable(sharedObservable$, getValue, emptyArr)
return [useStaticObservable, sharedObservable$] as const
}

View File

@ -6,6 +6,7 @@ import { Observable } from "rxjs"
export const useObservable = <O>(
source$: Observable<O>,
getValue: () => O,
keys: Array<any>,
): Exclude<O, typeof SUSPENSE> => {
const [state, setState] = useState(getValue)
const prevStateRef = useRef<O | (() => O)>(state)
@ -37,7 +38,7 @@ export const useObservable = <O>(
t.unsubscribe()
return () => subscription.unsubscribe()
}, [source$])
}, keys)
return state as Exclude<O, typeof SUSPENSE>
}