-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
87 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
packages/observable-hooks/src/use-subscription-internal.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { Observable, Subscription } from 'rxjs' | ||
import { useForceUpdate } from './helpers' | ||
import { useEffect, useRef } from 'react' | ||
|
||
/** | ||
* | ||
* @template TInput Input value within Observable. | ||
* | ||
* @param useCustomEffect useEffect or useLayoutEffect | ||
* @param args collected arguments | ||
*/ | ||
export function useSubscriptionInternal<TInput>( | ||
useCustomEffect: typeof useEffect, | ||
args: [ | ||
Observable<TInput>, // inputs$ | ||
((value: TInput) => void) | null | undefined, // next | ||
((error: any) => void) | null | undefined, // error | ||
(() => void) | null | undefined // complete | ||
] | ||
): React.MutableRefObject<Subscription | undefined> { | ||
const argsRef = useRef(args) | ||
argsRef.current = args | ||
|
||
const forceUpdate = useForceUpdate() | ||
|
||
const subscriptionRef = useRef<Subscription>() | ||
const errorRef = useRef<Error | null>() | ||
|
||
useCustomEffect(() => { | ||
errorRef.current = null | ||
|
||
// keep in closure for checking staleness | ||
const input$ = argsRef.current[0] | ||
|
||
const subscription = input$.subscribe({ | ||
next: value => { | ||
if (input$ !== argsRef.current[0]) { | ||
// stale observable | ||
return | ||
} | ||
if (argsRef.current[1]) { | ||
return argsRef.current[1](value) | ||
} | ||
}, | ||
error: error => { | ||
if (input$ !== argsRef.current[0]) { | ||
// stale observable | ||
return | ||
} | ||
if (argsRef.current[2]) { | ||
errorRef.current = null | ||
return argsRef.current[2](error) | ||
} | ||
errorRef.current = error | ||
forceUpdate() | ||
}, | ||
complete: () => { | ||
if (input$ !== argsRef.current[0]) { | ||
// stale observable | ||
return | ||
} | ||
if (argsRef.current[3]) { | ||
return argsRef.current[3]() | ||
} | ||
} | ||
}) | ||
|
||
subscriptionRef.current = subscription | ||
|
||
return () => { | ||
subscription.unsubscribe() | ||
} | ||
}, [args[0]]) | ||
|
||
if (errorRef.current) { | ||
// Let error boundary catch the error | ||
throw errorRef.current | ||
} | ||
|
||
return subscriptionRef | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters