mirror of
https://github.com/re-rxjs/react-rxjs.git
synced 2025-12-08 18:01:51 +00:00
Fix observable of promises triggering suspense
This commit is contained in:
parent
50cb566a76
commit
80d7708227
@ -315,6 +315,43 @@ describe("connectObservable", () => {
|
||||
expect(screen.queryByText("Waiting")).toBeNull()
|
||||
})
|
||||
|
||||
it("doesn't enter suspense if the observable emits a promise", async () => {
|
||||
const subject$ = new Subject<Promise<any>>()
|
||||
const [usePromise, promise$] = bind(subject$, null)
|
||||
const Result: React.FC = () => {
|
||||
const value = usePromise()
|
||||
return (
|
||||
<div>
|
||||
{value === null
|
||||
? "default"
|
||||
: value instanceof Promise
|
||||
? "promise"
|
||||
: "wtf?"}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
const TestSuspense: React.FC = () => {
|
||||
return (
|
||||
<div>
|
||||
<Subscribe source$={promise$} fallback={<span>Waiting</span>}>
|
||||
<Result />
|
||||
</Subscribe>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
render(<TestSuspense />)
|
||||
|
||||
expect(screen.queryByText("Waiting")).toBeNull()
|
||||
expect(screen.queryByText("default")).not.toBeNull()
|
||||
|
||||
act(() => subject$.next(new Promise(() => {})))
|
||||
|
||||
expect(screen.queryByText("Waiting")).toBeNull()
|
||||
expect(screen.queryByText("promise")).not.toBeNull()
|
||||
})
|
||||
|
||||
it("correctly unsubscribes when the Subscribe component gets unmounted", async () => {
|
||||
const subject$ = new Subject<void>()
|
||||
const [useNumber, number$] = bind(subject$.pipe(scan((a) => a + 1, 0)))
|
||||
|
||||
@ -2,6 +2,7 @@ import {
|
||||
DefaultedStateObservable,
|
||||
liftSuspense,
|
||||
StateObservable,
|
||||
StatePromise,
|
||||
SUSPENSE,
|
||||
} from "@rx-state/core"
|
||||
import { useRef, useState } from "react"
|
||||
@ -25,7 +26,7 @@ export const useStateObservable = <O>(
|
||||
if (!callbackRef.current) {
|
||||
const getValue = (src: StateObservable<O>) => {
|
||||
const result = src.getValue()
|
||||
if (result instanceof Promise) throw result
|
||||
if (result instanceof StatePromise) throw result
|
||||
return result as any
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user