@@ -26,9 +26,33 @@ import * as OpCodes from "./opCodes/effect.js"
26
26
import * as runtimeFlags from "./runtimeFlags.js"
27
27
import * as supervisor_ from "./supervisor.js"
28
28
29
+ const makeDual = < Args extends Array < any > , Return > (
30
+ f : ( runtime : Runtime . Runtime < never > , effect : Effect . Effect < any , any > , ...args : Args ) => Return
31
+ ) : {
32
+ < R > ( runtime : Runtime . Runtime < R > ) : < A , E > ( effect : Effect . Effect < A , E , R > , ...args : Args ) => Return
33
+ < R , A , E > ( runtime : Runtime . Runtime < R > , effect : Effect . Effect < A , E , R > , ...args : Args ) : Return
34
+ } =>
35
+ function ( this : any ) {
36
+ if ( arguments . length === 1 ) {
37
+ const runtime = arguments [ 0 ]
38
+ return ( effect : any , ...args : Args ) => f ( runtime , effect , ...args )
39
+ }
40
+ return f . apply ( this , arguments as any )
41
+ } as any
42
+
29
43
/** @internal */
30
- export const unsafeFork = < R > ( runtime : Runtime . Runtime < R > ) =>
31
- < A , E > (
44
+ export const unsafeFork : {
45
+ < R > ( runtime : Runtime . Runtime < R > ) : < A , E > (
46
+ effect : Effect . Effect < A , E , R > ,
47
+ options ?: Runtime . RunForkOptions | undefined
48
+ ) => Fiber . RuntimeFiber < A , E >
49
+ < R , A , E > (
50
+ runtime : Runtime . Runtime < R > ,
51
+ effect : Effect . Effect < A , E , R > ,
52
+ options ?: Runtime . RunForkOptions | undefined
53
+ ) : Fiber . RuntimeFiber < A , E >
54
+ } = makeDual ( < R , A , E > (
55
+ runtime : Runtime . Runtime < R > ,
32
56
self : Effect . Effect < A , E , R > ,
33
57
options ?: Runtime . RunForkOptions
34
58
) : Fiber . RuntimeFiber < A , E > => {
@@ -93,15 +117,25 @@ export const unsafeFork = <R>(runtime: Runtime.Runtime<R>) =>
93
117
}
94
118
95
119
return fiberRuntime
96
- }
120
+ } )
97
121
98
122
/** @internal */
99
- export const unsafeRunCallback = < R > ( runtime : Runtime . Runtime < R > ) =>
100
- < A , E > (
101
- effect : Effect . Effect < A , E , R > ,
102
- options : Runtime . RunCallbackOptions < A , E > = { }
103
- ) : ( fiberId ?: FiberId . FiberId , options ?: Runtime . RunCallbackOptions < A , E > | undefined ) => void => {
104
- const fiberRuntime = unsafeFork ( runtime ) ( effect , options )
123
+ export const unsafeRunCallback : {
124
+ < R > ( runtime : Runtime . Runtime < R > ) : < A , E > (
125
+ effect : Effect . Effect < A , E , R > ,
126
+ options ?: Runtime . RunCallbackOptions < A , E > | undefined
127
+ ) => ( fiberId ?: FiberId . FiberId , options ?: Runtime . RunCallbackOptions < A , E > | undefined ) => void
128
+ < R , A , E > (
129
+ runtime : Runtime . Runtime < R > ,
130
+ effect : Effect . Effect < A , E , R > ,
131
+ options ?: Runtime . RunCallbackOptions < A , E > | undefined
132
+ ) : ( fiberId ?: FiberId . FiberId , options ?: Runtime . RunCallbackOptions < A , E > | undefined ) => void
133
+ } = makeDual ( (
134
+ runtime ,
135
+ effect ,
136
+ options : Runtime . RunCallbackOptions < any , any > = { }
137
+ ) : ( fiberId ?: FiberId . FiberId , options ?: Runtime . RunCallbackOptions < any , any > | undefined ) => void => {
138
+ const fiberRuntime = unsafeFork ( runtime , effect , options )
105
139
106
140
if ( options . onExit ) {
107
141
fiberRuntime . addObserver ( ( exit ) => {
@@ -119,17 +153,19 @@ export const unsafeRunCallback = <R>(runtime: Runtime.Runtime<R>) =>
119
153
: undefined
120
154
}
121
155
)
122
- }
156
+ } )
123
157
124
158
/** @internal */
125
- export const unsafeRunSync = < R > ( runtime : Runtime . Runtime < R > ) => < A , E > ( effect : Effect . Effect < A , E , R > ) : A => {
159
+ export const unsafeRunSync : {
160
+ < A , E , R > ( runtime : Runtime . Runtime < R > , effect : Effect . Effect < A , E , R > ) : A
161
+ < R > ( runtime : Runtime . Runtime < R > ) : < A , E > ( effect : Effect . Effect < A , E , R > ) => A
162
+ } = makeDual ( ( runtime , effect ) => {
126
163
const result = unsafeRunSyncExit ( runtime ) ( effect )
127
164
if ( result . _tag === "Failure" ) {
128
165
throw fiberFailure ( result . effect_instruction_i0 )
129
- } else {
130
- return result . effect_instruction_i0
131
166
}
132
- }
167
+ return result . effect_instruction_i0
168
+ } )
133
169
134
170
class AsyncFiberExceptionImpl < A , E = never > extends Error implements Runtime . AsyncFiberException < A , E > {
135
171
readonly _tag = "AsyncFiberException"
@@ -229,31 +265,47 @@ const fastPath = <A, E, R>(effect: Effect.Effect<A, E, R>): Exit.Exit<A, E> | un
229
265
}
230
266
231
267
/** @internal */
232
- export const unsafeRunSyncExit =
233
- < R > ( runtime : Runtime . Runtime < R > ) => < A , E > ( effect : Effect . Effect < A , E , R > ) : Exit . Exit < A , E > => {
234
- const op = fastPath ( effect )
235
- if ( op ) {
236
- return op
237
- }
238
- const scheduler = new scheduler_ . SyncScheduler ( )
239
- const fiberRuntime = unsafeFork ( runtime ) ( effect , { scheduler } )
240
- scheduler . flush ( )
241
- const result = fiberRuntime . unsafePoll ( )
242
- if ( result ) {
243
- return result
244
- }
245
- return core . exitDie ( core . capture ( asyncFiberException ( fiberRuntime ) , core . currentSpanFromFiber ( fiberRuntime ) ) )
268
+ export const unsafeRunSyncExit : {
269
+ < A , E , R > ( runtime : Runtime . Runtime < R > , effect : Effect . Effect < A , E , R > ) : Exit . Exit < A , E >
270
+ < R > ( runtime : Runtime . Runtime < R > ) : < A , E > ( effect : Effect . Effect < A , E , R > ) => Exit . Exit < A , E >
271
+ } = makeDual ( ( runtime , effect ) => {
272
+ const op = fastPath ( effect )
273
+ if ( op ) {
274
+ return op
246
275
}
276
+ const scheduler = new scheduler_ . SyncScheduler ( )
277
+ const fiberRuntime = unsafeFork ( runtime ) ( effect , { scheduler } )
278
+ scheduler . flush ( )
279
+ const result = fiberRuntime . unsafePoll ( )
280
+ if ( result ) {
281
+ return result
282
+ }
283
+ return core . exitDie ( core . capture ( asyncFiberException ( fiberRuntime ) , core . currentSpanFromFiber ( fiberRuntime ) ) )
284
+ } )
247
285
248
286
/** @internal */
249
- export const unsafeRunPromise = < R > ( runtime : Runtime . Runtime < R > ) =>
250
- < A , E > (
251
- effect : Effect . Effect < A , E , R > ,
287
+ export const unsafeRunPromise : {
288
+ < R > ( runtime : Runtime . Runtime < R > ) : < A , E > (
289
+ effect : Effect . Effect < A , E , R > ,
290
+ options ?: {
291
+ readonly signal ?: AbortSignal | undefined
292
+ } | undefined
293
+ ) => Promise < A >
294
+ < R , A , E > (
295
+ runtime : Runtime . Runtime < R > ,
296
+ effect : Effect . Effect < A , E , R > ,
297
+ options ?: {
298
+ readonly signal ?: AbortSignal | undefined
299
+ } | undefined
300
+ ) : Promise < A >
301
+ } = makeDual ( (
302
+ runtime ,
303
+ effect ,
252
304
options ?: {
253
305
readonly signal ?: AbortSignal | undefined
254
306
} | undefined
255
- ) : Promise < A > =>
256
- unsafeRunPromiseExit ( runtime ) ( effect , options ) . then ( ( result ) => {
307
+ ) =>
308
+ unsafeRunPromiseExit ( runtime , effect , options ) . then ( ( result ) => {
257
309
switch ( result . _tag ) {
258
310
case OpCodes . OP_SUCCESS : {
259
311
return result . effect_instruction_i0
@@ -263,16 +315,29 @@ export const unsafeRunPromise = <R>(runtime: Runtime.Runtime<R>) =>
263
315
}
264
316
}
265
317
} )
318
+ )
266
319
267
320
/** @internal */
268
- export const unsafeRunPromiseExit = < R > ( runtime : Runtime . Runtime < R > ) =>
269
- < A , E > (
270
- effect : Effect . Effect < A , E , R > ,
321
+ export const unsafeRunPromiseExit : {
322
+ < R > (
323
+ runtime : Runtime . Runtime < R >
324
+ ) : < A , E > (
325
+ effect : Effect . Effect < A , E , R > ,
326
+ options ?: { readonly signal ?: AbortSignal | undefined } | undefined
327
+ ) => Promise < Exit . Exit < A , E > >
328
+ < R , A , E > (
329
+ runtime : Runtime . Runtime < R > ,
330
+ effect : Effect . Effect < A , E , R > ,
331
+ options ?: { readonly signal ?: AbortSignal | undefined } | undefined
332
+ ) : Promise < Exit . Exit < A , E > >
333
+ } = makeDual ( (
334
+ runtime ,
335
+ effect ,
271
336
options ?: {
272
337
readonly signal ?: AbortSignal | undefined
273
338
} | undefined
274
- ) : Promise < Exit . Exit < A , E > > =>
275
- new Promise ( ( resolve ) => {
339
+ ) =>
340
+ new Promise < Exit . Exit < any , any > > ( ( resolve ) => {
276
341
const op = fastPath ( effect )
277
342
if ( op ) {
278
343
resolve ( op )
@@ -291,6 +356,7 @@ export const unsafeRunPromiseExit = <R>(runtime: Runtime.Runtime<R>) =>
291
356
}
292
357
}
293
358
} )
359
+ )
294
360
295
361
/** @internal */
296
362
export class RuntimeImpl < in R > implements Runtime . Runtime < R > {
0 commit comments