mirror of
https://github.com/re-rxjs/react-rxjs.git
synced 2025-12-08 18:01:51 +00:00
fix(bind): the default value can be undefined
This commit is contained in:
parent
7dc5824c7c
commit
bfc9b8bcd4
@ -522,6 +522,23 @@ describe("connectFactoryObservable", () => {
|
||||
subscription.unsubscribe()
|
||||
})
|
||||
|
||||
it("the defaultValue can be undefined", () => {
|
||||
const number$ = new Subject<number>()
|
||||
const [useNumber] = bind(() => number$, undefined)
|
||||
|
||||
const { result, unmount } = renderHook(() => useNumber())
|
||||
|
||||
expect(result.current).toBe(undefined)
|
||||
|
||||
actHook(() => {
|
||||
number$.next(5)
|
||||
})
|
||||
|
||||
expect(result.current).toBe(5)
|
||||
|
||||
unmount()
|
||||
})
|
||||
|
||||
it("if the observable hasn't emitted and a defaultValue is provided, it does not start suspense", () => {
|
||||
const number$ = new Subject<number>()
|
||||
const [useNumber] = bind(
|
||||
|
||||
@ -45,8 +45,8 @@ export default function connectFactoryObservable<A extends [], O>(
|
||||
|
||||
const sharedObservable$ = shareLatest(
|
||||
getObservable(...input),
|
||||
false,
|
||||
defaultValue,
|
||||
false,
|
||||
() => {
|
||||
cache.delete(keys)
|
||||
},
|
||||
|
||||
@ -650,6 +650,23 @@ describe("connectObservable", () => {
|
||||
expect(errorCallback).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it("the defaultValue can be undefined", () => {
|
||||
const number$ = new Subject<number>()
|
||||
const [useNumber] = bind(number$, undefined)
|
||||
|
||||
const { result, unmount } = renderHook(() => useNumber())
|
||||
|
||||
expect(result.current).toBe(undefined)
|
||||
|
||||
act(() => {
|
||||
number$.next(5)
|
||||
})
|
||||
|
||||
expect(result.current).toBe(5)
|
||||
|
||||
unmount()
|
||||
})
|
||||
|
||||
it("if the observable hasn't emitted and a defaultValue is provided, it does not start suspense", () => {
|
||||
const number$ = new Subject<number>()
|
||||
const [useNumber] = bind(number$, 0)
|
||||
|
||||
@ -21,7 +21,7 @@ export default function connectObservable<T>(
|
||||
observable: Observable<T>,
|
||||
defaultValue: T,
|
||||
) {
|
||||
const sharedObservable$ = shareLatest<T>(observable, false, defaultValue)
|
||||
const sharedObservable$ = shareLatest<T>(observable, defaultValue, false)
|
||||
const useStaticObservable = () => useObservable(sharedObservable$)
|
||||
return [useStaticObservable, sharedObservable$] as const
|
||||
}
|
||||
|
||||
@ -46,8 +46,11 @@ export function bind<A extends unknown[], O>(
|
||||
defaultValue?: O,
|
||||
): [(...args: A) => Exclude<O, typeof SUSPENSE>, (...args: A) => Observable<O>]
|
||||
|
||||
export function bind(observable: any, defaultValue: any = EMPTY_VALUE) {
|
||||
export function bind(observable: any, defaultValue: any) {
|
||||
return (typeof observable === "function"
|
||||
? (connectFactoryObservable as any)
|
||||
: connectObservable)(observable, defaultValue)
|
||||
: connectObservable)(
|
||||
observable,
|
||||
arguments.length > 1 ? defaultValue : EMPTY_VALUE,
|
||||
)
|
||||
}
|
||||
|
||||
@ -5,8 +5,8 @@ import { SUSPENSE } from "../SUSPENSE"
|
||||
|
||||
const shareLatest = <T>(
|
||||
source$: Observable<T>,
|
||||
defaultValue: T,
|
||||
shouldComplete = true,
|
||||
defaultValue: T = EMPTY_VALUE,
|
||||
teardown = noop,
|
||||
): BehaviorObservable<T> => {
|
||||
let subject: Subject<T> | null
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
import { Observable, MonoTypeOperatorFunction } from "rxjs"
|
||||
import internalShareLatest from "./internal/share-latest"
|
||||
import { EMPTY_VALUE } from "./internal/empty-value"
|
||||
|
||||
/**
|
||||
* A RxJS pipeable operator which shares and replays the latest emitted value.
|
||||
@ -17,4 +18,4 @@ import internalShareLatest from "./internal/share-latest"
|
||||
*/
|
||||
export const shareLatest = <T>(): MonoTypeOperatorFunction<T> => (
|
||||
source$: Observable<T>,
|
||||
) => internalShareLatest(source$)
|
||||
) => internalShareLatest(source$, EMPTY_VALUE)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user