1
- import { useEffect , useState , useRef } from "react"
1
+ import { useEffect , useState } from "react"
2
+ import { Observable } from "rxjs"
2
3
import { SUSPENSE } from "../SUSPENSE"
3
4
import { EMPTY_VALUE } from "./empty-value"
4
- import { Observable } from "rxjs"
5
5
6
6
export const useObservable = < O > (
7
7
source$ : Observable < O > ,
8
8
getValue : ( ) => O ,
9
9
keys : Array < any > ,
10
10
) : Exclude < O , typeof SUSPENSE > => {
11
11
const [ state , setState ] = useState ( getValue )
12
- const prevStateRef = useRef < O | ( ( ) => O ) > ( state )
13
12
14
13
useEffect ( ( ) => {
15
14
let err : any = EMPTY_VALUE
@@ -27,19 +26,14 @@ export const useObservable = <O>(
27
26
} , onError )
28
27
if ( err !== EMPTY_VALUE ) return
29
28
30
- const set = ( value : O | ( ( ) => O ) ) => {
31
- if ( ! Object . is ( prevStateRef . current , value ) )
32
- setState ( ( prevStateRef . current = value ) )
33
- }
34
-
35
29
const defaultValue = ( getValue as any ) . d
36
30
if ( syncVal === EMPTY_VALUE ) {
37
- set ( defaultValue === EMPTY_VALUE ? getValue : defaultValue )
31
+ setState ( defaultValue === EMPTY_VALUE ? getValue : ( ) => defaultValue )
38
32
}
39
33
40
34
const t = subscription
41
35
subscription = source$ . subscribe ( ( value : O | typeof SUSPENSE ) => {
42
- set ( value === SUSPENSE ? getValue : value )
36
+ setState ( value === SUSPENSE ? getValue : ( ) => value )
43
37
} , onError )
44
38
t . unsubscribe ( )
45
39
0 commit comments