Skip to content

Commit

Permalink
fix(core): ensure the same observable on useObservable
Browse files Browse the repository at this point in the history
  • Loading branch information
josepot committed Oct 14, 2020
1 parent c01cd43 commit ce05cf3
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/bind/connectFactoryObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
]
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/bind/connectObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
3 changes: 2 additions & 1 deletion packages/core/src/internal/useObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -37,7 +38,7 @@ export const useObservable = <O>(
t.unsubscribe()

return () => subscription.unsubscribe()
}, [source$])
}, keys)

return state as Exclude<O, typeof SUSPENSE>
}

0 comments on commit ce05cf3

Please sign in to comment.