Skip to content

Commit 75ba112

Browse files
committed
feat(utils): startWith -> defaultStart on suspense utils
BREAKING CHANGE: suspend, suspended and switchMapSuspended will only emit SUSPENSE if the source doesn't emit synchronously
1 parent 199a791 commit 75ba112

File tree

5 files changed

+50
-4
lines changed

5 files changed

+50
-4
lines changed

packages/utils/src/internal-utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import {
99
mergeMap,
1010
} from "rxjs/operators"
1111

12-
export const defaultStart = <T>(value: T) => (source$: Observable<T>) =>
13-
new Observable<T>((observer) => {
12+
export const defaultStart = <T, D>(value: D) => (source$: Observable<T>) =>
13+
new Observable<T | D>((observer) => {
1414
let emitted = false
1515
const subscription = source$.subscribe(
1616
(x) => {

packages/utils/src/suspend.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { TestScheduler } from "rxjs/testing"
22
import { SUSPENSE } from "@react-rxjs/core"
3+
import { of } from "rxjs"
34
import { suspend } from "./"
45

56
const scheduler = () =>
@@ -21,4 +22,18 @@ describe("operators/suspend", () => {
2122
})
2223
})
2324
})
25+
26+
it("does not prepend the source stream with SUSPENSE when the source is sync", () => {
27+
scheduler().run(({ expectObservable }) => {
28+
const source = of("a")
29+
const expected = "(a|)"
30+
31+
const suspended = suspend(source)
32+
33+
expectObservable(suspended).toBe(expected, {
34+
s: SUSPENSE,
35+
a: "a",
36+
})
37+
})
38+
})
2439
})

packages/utils/src/suspend.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ObservableInput, from, Observable } from "rxjs"
2-
import { startWith } from "rxjs/operators"
32
import { SUSPENSE } from "@react-rxjs/core"
3+
import { defaultStart } from "./internal-utils"
44

55
/**
66
* A RxJS creation operator that prepends a SUSPENSE on the source observable.
@@ -10,4 +10,4 @@ import { SUSPENSE } from "@react-rxjs/core"
1010
export const suspend: <T>(
1111
source$: ObservableInput<T>,
1212
) => Observable<T | typeof SUSPENSE> = <T>(source$: ObservableInput<T>) =>
13-
startWith(SUSPENSE)(from(source$)) as any
13+
defaultStart(SUSPENSE)(from(source$)) as any

packages/utils/src/suspended.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { TestScheduler } from "rxjs/testing"
22
import { SUSPENSE } from "@react-rxjs/core"
3+
import { of } from "rxjs"
34
import { suspended } from "./"
45

56
const scheduler = () =>
@@ -21,4 +22,18 @@ describe("operators/suspended", () => {
2122
})
2223
})
2324
})
25+
26+
it("does not prepend the source stream with SUSPENSE when the source is sync", () => {
27+
scheduler().run(({ expectObservable }) => {
28+
const source = of("a")
29+
const expected = "(a|)"
30+
31+
const result$ = source.pipe(suspended())
32+
33+
expectObservable(result$).toBe(expected, {
34+
s: SUSPENSE,
35+
a: "a",
36+
})
37+
})
38+
})
2439
})

packages/utils/src/switchMapSuspended.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { TestScheduler } from "rxjs/testing"
22
import { SUSPENSE } from "@react-rxjs/core"
33
import { switchMapSuspended } from "./"
4+
import { of } from "rxjs"
45

56
const scheduler = () =>
67
new TestScheduler((actual, expected) => {
@@ -37,4 +38,19 @@ describe("operators/switchMapSuspended", () => {
3738
})
3839
})
3940
})
41+
42+
it("does not emits another SUSPENSE when the next inner stream is sync", () => {
43+
scheduler().run(({ expectObservable, cold }) => {
44+
const source = cold("-x--x")
45+
const inner = of("a")
46+
const expected = " -a--a"
47+
48+
const result$ = source.pipe(switchMapSuspended(() => inner))
49+
50+
expectObservable(result$).toBe(expected, {
51+
s: SUSPENSE,
52+
a: "a",
53+
})
54+
})
55+
})
4056
})

0 commit comments

Comments
 (0)