@@ -17,16 +17,11 @@ export const makeUseResult: <R>(
1717) => UseResult < R > = < R > ( runtimeContext : RuntimeContext < R > ) =>
1818 < R0 extends R , E , A > ( stream : Stream . Stream < R0 , E , A > ) => {
1919 const runtime = useContext ( runtimeContext )
20- const prevRef = useRef < Stream . Stream < R0 , E , A > > ( )
2120 const fiberRef = useRef < Fiber . RuntimeFiber < E , void > > ( )
21+ const firstTimeRef = useRef ( true )
2222 const [ result , setResult ] = useState < Result . Result < E , A > > ( Result . waiting ( Result . initial ( ) ) )
2323 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 ) {
3025 fiberRef . current = stream . pipe (
3126 Stream . tap ( ( value ) =>
3227 Effect . sync ( ( ) => {
@@ -43,14 +38,31 @@ export const makeUseResult: <R>(
4338 )
4439 }
4540
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 ( ) => {
5060 if ( fiberRef . current ) {
5161 Effect . runSync ( Fiber . interruptFork ( fiberRef . current ) )
5262 }
53- } , [ ] )
63+ }
64+ } , [ runtime , stream ] )
5465
66+ trackRef . current . currentStatus = result . _tag
5567 return resultBag
5668 }
0 commit comments