@@ -17,16 +17,11 @@ export const makeUseResult: <R>(
17
17
) => UseResult < R > = < R > ( runtimeContext : RuntimeContext < R > ) =>
18
18
< R0 extends R , E , A > ( stream : Stream . Stream < R0 , E , A > ) => {
19
19
const runtime = useContext ( runtimeContext )
20
- const prevRef = useRef < Stream . Stream < R0 , E , A > > ( )
21
20
const fiberRef = useRef < Fiber . RuntimeFiber < E , void > > ( )
21
+ const firstTimeRef = useRef ( true )
22
22
const [ result , setResult ] = useState < Result . Result < E , A > > ( Result . waiting ( Result . initial ( ) ) )
23
23
const [ trackRef , resultBag ] = useResultBag ( result )
24
-
25
- if ( prevRef . current !== stream ) {
26
- prevRef . current = stream
27
- if ( fiberRef . current ) {
28
- Effect . runSync ( Fiber . interruptFork ( fiberRef . current ) )
29
- }
24
+ if ( ! fiberRef . current ) {
30
25
fiberRef . current = stream . pipe (
31
26
Stream . tap ( ( value ) =>
32
27
Effect . sync ( ( ) => {
@@ -43,14 +38,31 @@ export const makeUseResult: <R>(
43
38
)
44
39
}
45
40
46
- trackRef . current . currentStatus = result . _tag
47
-
48
- useEffect ( ( ) =>
49
- ( ) => {
41
+ useEffect ( ( ) => {
42
+ if ( ! firstTimeRef . current ) {
43
+ fiberRef . current = stream . pipe (
44
+ Stream . tap ( ( value ) =>
45
+ Effect . sync ( ( ) => {
46
+ setResult ( updateNext ( Result . success ( value ) , trackRef ) )
47
+ } )
48
+ ) ,
49
+ Stream . tapErrorCause ( ( cause ) =>
50
+ Effect . sync ( ( ) => {
51
+ setResult ( updateNext ( Result . failCause ( cause ) , trackRef ) )
52
+ } )
53
+ ) ,
54
+ Stream . runDrain ,
55
+ Runtime . runFork ( runtime )
56
+ )
57
+ }
58
+ firstTimeRef . current = false
59
+ return ( ) => {
50
60
if ( fiberRef . current ) {
51
61
Effect . runSync ( Fiber . interruptFork ( fiberRef . current ) )
52
62
}
53
- } , [ ] )
63
+ }
64
+ } , [ runtime , stream ] )
54
65
66
+ trackRef . current . currentStatus = result . _tag
55
67
return resultBag
56
68
}
0 commit comments