Skip to content

Commit 14398b3

Browse files
committed
fix(core/bind): avoid triggering SUSPENSE when the stream emits synchronously
1 parent fcd6641 commit 14398b3

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

packages/core/src/bind/connectObservable.test.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
screen,
66
} from "@testing-library/react"
77
import { act, renderHook } from "@testing-library/react-hooks"
8-
import React, { Suspense, useEffect, FC } from "react"
8+
import React, { Suspense, useEffect, FC, StrictMode } from "react"
99
import {
1010
BehaviorSubject,
1111
defer,
@@ -15,6 +15,7 @@ import {
1515
throwError,
1616
Observable,
1717
merge,
18+
NEVER,
1819
} from "rxjs"
1920
import {
2021
delay,
@@ -491,4 +492,20 @@ describe("connectObservable", () => {
491492

492493
expect(errorCallback).not.toHaveBeenCalled()
493494
})
495+
496+
it("should not trigger suspense when the stream emits synchronously", () => {
497+
const [useValue] = bind(NEVER.pipe(startWith("Hello")))
498+
499+
const Component: FC = () => <>{useValue()}</>
500+
render(
501+
<StrictMode>
502+
<Suspense fallback={<div>Loading...</div>}>
503+
<Component />
504+
</Suspense>
505+
</StrictMode>,
506+
)
507+
508+
expect(screen.queryByText("Loading...")).toBeNull()
509+
expect(screen.queryByText("Hello")).not.toBeNull()
510+
})
494511
})

packages/core/src/internal/react-enhancer.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ const reactEnhancer = <T>(source$: BehaviorObservable<T>): (() => T) => {
4949
promise = undefined
5050
})
5151

52-
if (value !== EMPTY_VALUE) return value
52+
if (value !== EMPTY_VALUE) {
53+
promise = undefined
54+
return value
55+
}
5356

5457
throw error !== EMPTY_VALUE ? error : promise
5558
}

0 commit comments

Comments
 (0)