1+ import { pipe } from "@effect/data/Function"
12import * as Effect from "@effect/io/Effect"
23import type * as Fiber from "@effect/io/Fiber"
34import * as Ref from "@effect/io/Ref"
@@ -37,7 +38,7 @@ class FiberStore<R, E, A> {
3738 setResult ( result : Result . Result < E , A > ) {
3839 this . result = result
3940 updateTrackedProps ( result , this . trackedProps )
40- this . resultBag = makeResultBag ( this . result , this . trackedProps )
41+ this . resultBag = makeResultBag ( result , this . trackedProps )
4142
4243 for ( let i = 0 ; i < this . listeners . length ; i ++ ) {
4344 this . listeners [ i ] ( )
@@ -49,17 +50,20 @@ class FiberStore<R, E, A> {
4950
5051 fiberState :
5152 | {
52- readonly fiber : Fiber . RuntimeFiber < E , void >
53+ readonly fiber : Fiber . RuntimeFiber < never , void >
5354 readonly interruptedRef : Ref . Ref < boolean >
5455 }
5556 | undefined = undefined
5657
5758 interruptIfRunning ( ) {
5859 if ( this . fiberState ) {
59- Effect . runSync ( Ref . set ( this . fiberState . interruptedRef , true ) )
6060 Effect . runFork (
61- this . fiberState . fiber . interruptAsFork ( this . fiberState . fiber . id ( ) )
61+ Effect . zipRight (
62+ Ref . set ( this . fiberState . interruptedRef , true ) ,
63+ this . fiberState . fiber . interruptAsFork ( this . fiberState . fiber . id ( ) )
64+ )
6265 )
66+ this . fiberState = undefined
6367 }
6468 }
6569
@@ -73,18 +77,18 @@ class FiberStore<R, E, A> {
7377 ( interrupted ) =>
7478 interrupted
7579 ? Effect . unit
76- : Effect . sync ( ( ) => {
77- this . setResult ( result )
78- } )
80+ : Effect . sync ( ( ) => this . setResult ( result ) )
7981 )
8082
81- const fiber = Stream . suspend ( ( ) => {
82- this . setResult ( Result . waiting ( this . result ) )
83- return stream
84- } ) . pipe (
85- Stream . tap ( ( value ) => maybeSetResult ( Result . success ( value ) ) ) ,
86- Stream . tapErrorCause ( ( cause ) => maybeSetResult ( Result . failCause ( cause ) ) ) ,
87- Stream . runDrain ,
83+ const fiber = pipe (
84+ Stream . runForEach (
85+ Stream . suspend ( ( ) => {
86+ this . setResult ( Result . waiting ( this . result ) )
87+ return stream
88+ } ) ,
89+ ( _ ) => maybeSetResult ( Result . success ( _ ) )
90+ ) ,
91+ Effect . catchAllCause ( ( cause ) => maybeSetResult ( Result . failCause ( cause ) ) ) ,
8892 Runtime . runFork ( this . runtime )
8993 )
9094
@@ -108,16 +112,16 @@ export const makeUseResultCallback: <R>(
108112 f : ( ...args : Args ) => Stream . Stream < R0 , E , A >
109113 ) => {
110114 const runtime = useContext ( runtimeContext )
111- const storeRef = useRef < FiberStore < R0 , E , A > | undefined > ( undefined )
115+ const storeRef = useRef < FiberStore < R0 , E , A > > ( undefined as any )
112116 if ( storeRef . current === undefined ) {
113117 storeRef . current = new FiberStore ( runtime )
114118 }
115119 const resultBag = useSyncExternalStore (
116- storeRef . current ! . subscribe ,
117- storeRef . current ! . snapshot
120+ storeRef . current . subscribe ,
121+ storeRef . current . snapshot
118122 )
119123 const run = useCallback ( ( ...args : Args ) => {
120- storeRef . current ! . run ( f ( ...args ) )
124+ storeRef . current . run ( f ( ...args ) )
121125 } , [ f ] )
122126 return [ resultBag , run ] as const
123127 }
0 commit comments