Skip to content

Commit 420acd4

Browse files
volivajosepot
andcommitted
fix: Subscribe with async errors
Co-authored-by: Josep M Sobrepere <jm.sobrepere@gmail.com>
1 parent 19840fd commit 420acd4

File tree

3 files changed

+11
-22
lines changed

3 files changed

+11
-22
lines changed

packages/core/src/Subscribe.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { state } from "@rx-state/core"
2-
import { render, screen, act } from "@testing-library/react"
3-
import React, { StrictMode, useState, useEffect } from "react"
2+
import { act, render, screen } from "@testing-library/react"
3+
import React, { StrictMode, useEffect, useState } from "react"
44
import { defer, EMPTY, NEVER, Observable, of, startWith } from "rxjs"
55
import { bind, RemoveSubscribe, Subscribe as OriginalSubscribe } from "./"
66
import { TestErrorBoundary } from "./test-helpers/TestErrorBoundary"

packages/core/src/Subscribe.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import React, {
88
useContext,
99
} from "react"
1010
import { Observable, Subscription } from "rxjs"
11-
import type { StateObservable } from "@rxstate/core"
11+
import type { StateObservable } from "@rx-state/core"
1212

1313
const SubscriptionContext = createContext<
1414
((src: StateObservable<any>) => void) | null

packages/core/src/useStateObservable.ts

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import { useRef, useState } from "react"
21
import {
3-
SUSPENSE,
42
DefaultedStateObservable,
5-
StateObservable,
63
liftEffects,
4+
StateObservable,
5+
SUSPENSE,
76
} from "@rx-state/core"
8-
import { EMPTY_VALUE } from "./internal/empty-value"
7+
import { useRef, useState } from "react"
98
import useSyncExternalStore from "./internal/useSyncExternalStore"
109
import { useSubscription } from "./Subscribe"
1110

@@ -31,21 +30,11 @@ export const useStateObservable = <O, E>(
3130
}
3231

3332
const gv: <T>() => Exclude<T, typeof SUSPENSE> = () => {
34-
const src = callbackRef.current!.source$ as DefaultedStateObservable<O, E>
35-
36-
if (src.getRefCount() > 0 || src.getDefaultValue) return getValue(src)
37-
38-
if (!subscription) throw new Error("Missing Subscribe!")
39-
40-
let error = EMPTY_VALUE
41-
subscription.add(
42-
liftEffects()(src).subscribe({
43-
error: (e) => {
44-
error = e
45-
},
46-
}),
47-
)
48-
if (error !== EMPTY_VALUE) throw error
33+
const src = callbackRef.current!.source$ as DefaultedStateObservable<O>
34+
if (!src.getRefCount() && !src.getDefaultValue) {
35+
if (!subscription) throw new Error("Missing Subscribe!")
36+
subscription(src)
37+
}
4938
return getValue(src)
5039
}
5140

0 commit comments

Comments
 (0)