mirror of
https://github.com/re-rxjs/react-rxjs.git
synced 2025-12-08 18:01:51 +00:00
v0.0.2 use useReducer on useObservable
This commit is contained in:
parent
47df057067
commit
72055462dc
2
package-lock.json
generated
2
package-lock.json
generated
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "re-rxjs",
|
||||
"version": "0.0.1",
|
||||
"version": "0.0.2",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
{
|
||||
"version": "0.0.1",
|
||||
"version": "0.0.2",
|
||||
"sideEffects": false,
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import { useState, useEffect } from "react"
|
||||
import { useEffect, useReducer } from "react"
|
||||
import delayUnsubscription from "./operators/delay-unsubscription"
|
||||
import {
|
||||
distinctShareReplay,
|
||||
@ -26,20 +26,33 @@ const getEnhancedSource = <T>(
|
||||
return result
|
||||
}
|
||||
|
||||
const reducer = (_: any, action: any) => action
|
||||
const init = (args: any) => {
|
||||
try {
|
||||
return getEnhancedSource(...(args as [any, any])).getValue()
|
||||
} catch (e) {
|
||||
return SUSPENSE
|
||||
}
|
||||
}
|
||||
|
||||
export const useObservable = <O>(
|
||||
source$: Observable<O>,
|
||||
unsubscribeGraceTime = 200,
|
||||
): Exclude<O, typeof SUSPENSE> => {
|
||||
const [state, setState] = useState<O>(SUSPENSE as any)
|
||||
const [state, dispatch] = useReducer(
|
||||
reducer,
|
||||
[source$, unsubscribeGraceTime],
|
||||
init,
|
||||
)
|
||||
|
||||
useEffect(() => {
|
||||
const enhancedSource$ = getEnhancedSource(source$, unsubscribeGraceTime)
|
||||
try {
|
||||
setState(enhancedSource$.getValue())
|
||||
dispatch(enhancedSource$.getValue())
|
||||
} catch (e) {
|
||||
setState(SUSPENSE as any)
|
||||
dispatch(SUSPENSE)
|
||||
}
|
||||
const subscription = enhancedSource$.subscribe(setState)
|
||||
const subscription = enhancedSource$.subscribe(dispatch)
|
||||
return () => subscription.unsubscribe()
|
||||
}, [source$, unsubscribeGraceTime])
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user