Skip to content

Commit f9b6175

Browse files
authored
feat: RemoveSubscribe component (#261)
1 parent bb716ce commit f9b6175

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

packages/core/src/Subscribe.test.tsx

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { state } from "@rxstate/core"
22
import { render, screen } from "@testing-library/react"
33
import React, { StrictMode, useState } from "react"
4-
import { defer, EMPTY, Observable, of, startWith } from "rxjs"
4+
import { defer, EMPTY, NEVER, Observable, of, startWith } from "rxjs"
5+
import { bind, RemoveSubscribe, Subscribe as OriginalSubscribe } from "./"
6+
import { TestErrorBoundary } from "./test-helpers/TestErrorBoundary"
57
import { useStateObservable } from "./useStateObservable"
6-
import { bind, Subscribe as OriginalSubscribe } from "./"
78

89
const Subscribe = (props: any) => {
910
return (
@@ -267,3 +268,27 @@ describe("Subscribe", () => {
267268
})
268269
})
269270
})
271+
272+
describe("RemoveSubscribe", () => {
273+
it("prevents its children from using the parent Subscribe boundary", () => {
274+
const [useValue] = bind(NEVER)
275+
276+
const ChildrenComponent = () => {
277+
const value = useValue()
278+
return <>{value}</>
279+
}
280+
281+
const errorCallback = jest.fn()
282+
render(
283+
<TestErrorBoundary onError={(e) => errorCallback(e.message)}>
284+
<Subscribe>
285+
<RemoveSubscribe>
286+
<ChildrenComponent />
287+
</RemoveSubscribe>
288+
</Subscribe>
289+
</TestErrorBoundary>,
290+
)
291+
292+
expect(errorCallback).toHaveBeenCalledWith("Missing Subscribe!")
293+
})
294+
})

packages/core/src/Subscribe.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,11 @@ export const Subscribe: React.FC<{
9595
<Suspense fallback={fallback}>{actualChildren}</Suspense>
9696
)
9797
}
98+
99+
/**
100+
* Component that prevents its children from using the parent `Subscribe` boundary
101+
* to manage their subscriptions.
102+
*/
103+
export const RemoveSubscribe: React.FC<{
104+
children?: React.ReactNode | undefined
105+
}> = ({ children }) => <Provider value={null}>{children}</Provider>

packages/core/src/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ export { shareLatest } from "./shareLatest"
33
export { useStateObservable } from "./useStateObservable"
44
export { bind } from "./bind"
55
export { SUSPENSE } from "./SUSPENSE"
6-
export { Subscribe } from "./Subscribe"
6+
export { Subscribe, RemoveSubscribe } from "./Subscribe"

0 commit comments

Comments
 (0)