@@ -103,8 +103,13 @@ trait Runtime[+R] {
103
103
* This method is effectful and should only be invoked at the edges of your program.
104
104
*/
105
105
final def unsafeRunAsyncCancelable [E , A ](zio : => ZIO [R , E , A ])(k : Exit [E , A ] => Any ): Fiber .Id => Exit [E , A ] = {
106
- lazy val curZIO = if (Platform .isJS) zio else ZIO .yieldNow *> zio
107
- unsafeRunWith(curZIO)(k)
106
+ lazy val curZio = if (Platform .isJS) zio else ZIO .yieldNow *> zio
107
+ val canceler = unsafeRunWith(curZio)(k)
108
+ fiberId => {
109
+ val result = internal.OneShot .make[Exit [E , A ]]
110
+ canceler(fiberId)(result.set)
111
+ result.get()
112
+ }
108
113
}
109
114
110
115
/**
@@ -122,9 +127,15 @@ trait Runtime[+R] {
122
127
*/
123
128
final def unsafeRunToFuture [E <: Throwable , A ](zio : ZIO [R , E , A ]): CancelableFuture [A ] = {
124
129
val p : concurrent.Promise [A ] = scala.concurrent.Promise [A ]()
125
- val canceler = unsafeRunAsyncCancelable(zio)(_.fold(cause => p.failure(cause.squashTraceWith(identity)), p.success))
130
+
131
+ val canceler = unsafeRunWith(zio)(_.fold(cause => p.failure(cause.squashTraceWith(identity)), p.success))
132
+
126
133
new CancelableFuture [A ](p.future) {
127
- def cancel (): Future [Exit [Throwable , A ]] = Future .successful(canceler(Fiber .Id .None ))
134
+ def cancel (): Future [Exit [Throwable , A ]] = {
135
+ val p : concurrent.Promise [Exit [Throwable , A ]] = scala.concurrent.Promise [Exit [Throwable , A ]]()
136
+ canceler(Fiber .Id .None )(p.success)
137
+ p.future
138
+ }
128
139
}
129
140
}
130
141
@@ -163,7 +174,9 @@ trait Runtime[+R] {
163
174
*/
164
175
def withTracingConfig (config : TracingConfig ): Runtime [R ] = mapPlatform(_.withTracingConfig(config))
165
176
166
- private final def unsafeRunWith [E , A ](zio : => ZIO [R , E , A ])(k : Exit [E , A ] => Any ): Fiber .Id => Exit [E , A ] = {
177
+ private final def unsafeRunWith [E , A ](
178
+ zio : => ZIO [R , E , A ]
179
+ )(k : Exit [E , A ] => Any ): Fiber .Id => (Exit [E , A ] => Any ) => Unit = {
167
180
val InitialInterruptStatus = InterruptStatus .Interruptible
168
181
169
182
val fiberId = Fiber .newFiberId()
@@ -194,7 +207,7 @@ trait Runtime[+R] {
194
207
context.evaluateNow(ZIOFn .recordStackTrace(() => zio)(zio.asInstanceOf [IO [E , A ]]))
195
208
context.runAsync(k)
196
209
197
- fiberId => unsafeRun (context.interruptAs(fiberId))
210
+ fiberId => k => unsafeRunAsync (context.interruptAs(fiberId))(( exit : Exit [ Nothing , Exit [ E , A ]]) => k(exit.flatten ))
198
211
}
199
212
}
200
213
0 commit comments