Skip to content

Commit

Permalink
fix(core/bind): avoid triggering SUSPENSE when the stream emits synch…
Browse files Browse the repository at this point in the history
…ronously
  • Loading branch information
josepot committed Oct 20, 2020
1 parent fcd6641 commit 14398b3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
19 changes: 18 additions & 1 deletion packages/core/src/bind/connectObservable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
screen,
} from "@testing-library/react"
import { act, renderHook } from "@testing-library/react-hooks"
import React, { Suspense, useEffect, FC } from "react"
import React, { Suspense, useEffect, FC, StrictMode } from "react"
import {
BehaviorSubject,
defer,
Expand All @@ -15,6 +15,7 @@ import {
throwError,
Observable,
merge,
NEVER,
} from "rxjs"
import {
delay,
Expand Down Expand Up @@ -491,4 +492,20 @@ describe("connectObservable", () => {

expect(errorCallback).not.toHaveBeenCalled()
})

it("should not trigger suspense when the stream emits synchronously", () => {
const [useValue] = bind(NEVER.pipe(startWith("Hello")))

const Component: FC = () => <>{useValue()}</>
render(
<StrictMode>
<Suspense fallback={<div>Loading...</div>}>
<Component />
</Suspense>
</StrictMode>,
)

expect(screen.queryByText("Loading...")).toBeNull()
expect(screen.queryByText("Hello")).not.toBeNull()
})
})
5 changes: 4 additions & 1 deletion packages/core/src/internal/react-enhancer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ const reactEnhancer = <T>(source$: BehaviorObservable<T>): (() => T) => {
promise = undefined
})

if (value !== EMPTY_VALUE) return value
if (value !== EMPTY_VALUE) {
promise = undefined
return value
}

throw error !== EMPTY_VALUE ? error : promise
}
Expand Down

0 comments on commit 14398b3

Please sign in to comment.