@fncts/io 0.0.34 → 0.0.35
Sign up to get free protection for your applications and to get access to all the features.
- package/Fiber/FiberRuntime.d.ts +7 -7
- package/IO/api/all.d.ts +41 -0
- package/IO/api/concurrency.d.ts +11 -0
- package/IO/api/interrupt.d.ts +1 -1
- package/IO/api/raceWith.d.ts +2 -1
- package/IO/api.d.ts +7 -2
- package/IO/definition.d.ts +103 -127
- package/IO.d.ts +1 -0
- package/Push/api.d.ts +25 -4
- package/Push/definition.d.ts +17 -0
- package/STM/definition.d.ts +2 -2
- package/Sink/api.d.ts +19 -19
- package/_cjs/Channel/api/runScoped.cjs +1 -1
- package/_cjs/Channel/api/runScoped.cjs.map +1 -1
- package/_cjs/Channel/api.cjs +2 -2
- package/_cjs/Channel/api.cjs.map +1 -1
- package/_cjs/Fiber/FiberRuntime.cjs +110 -98
- package/_cjs/Fiber/FiberRuntime.cjs.map +1 -1
- package/_cjs/Future/api.cjs +1 -1
- package/_cjs/Future/api.cjs.map +1 -1
- package/_cjs/IO/api/all.cjs +33 -0
- package/_cjs/IO/api/all.cjs.map +1 -0
- package/_cjs/IO/api/asyncIO.cjs +1 -1
- package/_cjs/IO/api/asyncIO.cjs.map +1 -1
- package/_cjs/IO/api/bracketExit.cjs +1 -1
- package/_cjs/IO/api/bracketExit.cjs.map +1 -1
- package/_cjs/IO/api/concurrency.cjs +25 -4
- package/_cjs/IO/api/concurrency.cjs.map +1 -1
- package/_cjs/IO/api/disconnect.cjs +1 -1
- package/_cjs/IO/api/disconnect.cjs.map +1 -1
- package/_cjs/IO/api/foreachConcurrent.cjs +1 -1
- package/_cjs/IO/api/foreachConcurrent.cjs.map +1 -1
- package/_cjs/IO/api/foreachExec.cjs +1 -1
- package/_cjs/IO/api/foreachExec.cjs.map +1 -1
- package/_cjs/IO/api/forkIn.cjs +1 -1
- package/_cjs/IO/api/forkIn.cjs.map +1 -1
- package/_cjs/IO/api/forkScoped.cjs +1 -1
- package/_cjs/IO/api/forkScoped.cjs.map +1 -1
- package/_cjs/IO/api/fulfill.cjs +1 -1
- package/_cjs/IO/api/fulfill.cjs.map +1 -1
- package/_cjs/IO/api/interrupt.cjs +18 -6
- package/_cjs/IO/api/interrupt.cjs.map +1 -1
- package/_cjs/IO/api/raceWith.cjs +4 -4
- package/_cjs/IO/api/raceWith.cjs.map +1 -1
- package/_cjs/IO/api/timeout.cjs +8 -5
- package/_cjs/IO/api/timeout.cjs.map +1 -1
- package/_cjs/IO/api/zipConcurrent.cjs +1 -1
- package/_cjs/IO/api/zipConcurrent.cjs.map +1 -1
- package/_cjs/IO/api.cjs +78 -20
- package/_cjs/IO/api.cjs.map +1 -1
- package/_cjs/IO/definition.cjs +14 -191
- package/_cjs/IO/definition.cjs.map +1 -1
- package/_cjs/IO.cjs +11 -0
- package/_cjs/IO.cjs.map +1 -1
- package/_cjs/Layer/MemoMap.cjs +1 -1
- package/_cjs/Layer/MemoMap.cjs.map +1 -1
- package/_cjs/Push/api.cjs +149 -110
- package/_cjs/Push/api.cjs.map +1 -1
- package/_cjs/Push/definition.cjs.map +1 -1
- package/_cjs/STM/api/atomically.cjs +1 -1
- package/_cjs/STM/api/atomically.cjs.map +1 -1
- package/_cjs/STM/definition.cjs +1 -1
- package/_cjs/STM/definition.cjs.map +1 -1
- package/_cjs/ScopedRef/api.cjs +2 -2
- package/_cjs/ScopedRef/api.cjs.map +1 -1
- package/_cjs/Semaphore.cjs +1 -1
- package/_cjs/Semaphore.cjs.map +1 -1
- package/_cjs/TReentrantLock/api.cjs +2 -2
- package/_cjs/TReentrantLock/api.cjs.map +1 -1
- package/_cjs/TSemaphore/api.cjs +1 -1
- package/_cjs/TSemaphore/api.cjs.map +1 -1
- package/_cjs/collection/immutable/Conc/dropUntilIO.cjs +12 -17
- package/_cjs/collection/immutable/Conc/dropUntilIO.cjs.map +1 -1
- package/_cjs/collection/immutable/Conc/dropWhileIO.cjs +12 -17
- package/_cjs/collection/immutable/Conc/dropWhileIO.cjs.map +1 -1
- package/_cjs/collection/immutable/Conc/filterIO.cjs +2 -12
- package/_cjs/collection/immutable/Conc/filterIO.cjs.map +1 -1
- package/_cjs/collection/immutable/Conc/mapIO.cjs +3 -9
- package/_cjs/collection/immutable/Conc/mapIO.cjs.map +1 -1
- package/_cjs/collection/immutable/Conc/takeWhileIO.cjs +11 -27
- package/_cjs/collection/immutable/Conc/takeWhileIO.cjs.map +1 -1
- package/_mjs/Channel/api/runScoped.mjs +1 -1
- package/_mjs/Channel/api/runScoped.mjs.map +1 -1
- package/_mjs/Channel/api.mjs +2 -2
- package/_mjs/Channel/api.mjs.map +1 -1
- package/_mjs/Fiber/FiberRuntime.mjs +111 -100
- package/_mjs/Fiber/FiberRuntime.mjs.map +1 -1
- package/_mjs/Future/api.mjs +1 -1
- package/_mjs/Future/api.mjs.map +1 -1
- package/_mjs/IO/api/all.mjs +24 -0
- package/_mjs/IO/api/all.mjs.map +1 -0
- package/_mjs/IO/api/asyncIO.mjs +1 -1
- package/_mjs/IO/api/asyncIO.mjs.map +1 -1
- package/_mjs/IO/api/bracketExit.mjs +1 -1
- package/_mjs/IO/api/bracketExit.mjs.map +1 -1
- package/_mjs/IO/api/concurrency.mjs +19 -2
- package/_mjs/IO/api/concurrency.mjs.map +1 -1
- package/_mjs/IO/api/disconnect.mjs +1 -1
- package/_mjs/IO/api/disconnect.mjs.map +1 -1
- package/_mjs/IO/api/foreachConcurrent.mjs +1 -1
- package/_mjs/IO/api/foreachConcurrent.mjs.map +1 -1
- package/_mjs/IO/api/foreachExec.mjs +1 -1
- package/_mjs/IO/api/foreachExec.mjs.map +1 -1
- package/_mjs/IO/api/forkIn.mjs +1 -1
- package/_mjs/IO/api/forkIn.mjs.map +1 -1
- package/_mjs/IO/api/forkScoped.mjs +1 -1
- package/_mjs/IO/api/forkScoped.mjs.map +1 -1
- package/_mjs/IO/api/fulfill.mjs +1 -1
- package/_mjs/IO/api/fulfill.mjs.map +1 -1
- package/_mjs/IO/api/interrupt.mjs +19 -7
- package/_mjs/IO/api/interrupt.mjs.map +1 -1
- package/_mjs/IO/api/raceWith.mjs +4 -4
- package/_mjs/IO/api/raceWith.mjs.map +1 -1
- package/_mjs/IO/api/timeout.mjs +8 -5
- package/_mjs/IO/api/timeout.mjs.map +1 -1
- package/_mjs/IO/api/zipConcurrent.mjs +1 -1
- package/_mjs/IO/api/zipConcurrent.mjs.map +1 -1
- package/_mjs/IO/api.mjs +78 -23
- package/_mjs/IO/api.mjs.map +1 -1
- package/_mjs/IO/definition.mjs +12 -181
- package/_mjs/IO/definition.mjs.map +1 -1
- package/_mjs/IO.mjs +1 -0
- package/_mjs/IO.mjs.map +1 -1
- package/_mjs/Layer/MemoMap.mjs +1 -1
- package/_mjs/Layer/MemoMap.mjs.map +1 -1
- package/_mjs/Push/api.mjs +145 -109
- package/_mjs/Push/api.mjs.map +1 -1
- package/_mjs/Push/definition.mjs.map +1 -1
- package/_mjs/STM/api/atomically.mjs +1 -1
- package/_mjs/STM/api/atomically.mjs.map +1 -1
- package/_mjs/STM/definition.mjs +1 -1
- package/_mjs/STM/definition.mjs.map +1 -1
- package/_mjs/ScopedRef/api.mjs +2 -2
- package/_mjs/ScopedRef/api.mjs.map +1 -1
- package/_mjs/Semaphore.mjs +1 -1
- package/_mjs/Semaphore.mjs.map +1 -1
- package/_mjs/TReentrantLock/api.mjs +2 -2
- package/_mjs/TReentrantLock/api.mjs.map +1 -1
- package/_mjs/TSemaphore/api.mjs +1 -1
- package/_mjs/TSemaphore/api.mjs.map +1 -1
- package/_mjs/collection/immutable/Conc/dropUntilIO.mjs +12 -17
- package/_mjs/collection/immutable/Conc/dropUntilIO.mjs.map +1 -1
- package/_mjs/collection/immutable/Conc/dropWhileIO.mjs +12 -17
- package/_mjs/collection/immutable/Conc/dropWhileIO.mjs.map +1 -1
- package/_mjs/collection/immutable/Conc/filterIO.mjs +2 -12
- package/_mjs/collection/immutable/Conc/filterIO.mjs.map +1 -1
- package/_mjs/collection/immutable/Conc/mapIO.mjs +3 -9
- package/_mjs/collection/immutable/Conc/mapIO.mjs.map +1 -1
- package/_mjs/collection/immutable/Conc/takeWhileIO.mjs +11 -27
- package/_mjs/collection/immutable/Conc/takeWhileIO.mjs.map +1 -1
- package/_src/Channel/api.ts +3 -3
- package/_src/Fiber/FiberRuntime.ts +76 -75
- package/_src/IO/api/all.ts +64 -0
- package/_src/IO/api/concurrency.ts +33 -0
- package/_src/IO/api/foreachExec.ts +2 -2
- package/_src/IO/api/interrupt.ts +20 -7
- package/_src/IO/api/raceWith.ts +4 -2
- package/_src/IO/api/timeout.ts +21 -1
- package/_src/IO/api.ts +79 -27
- package/_src/IO/definition.ts +155 -199
- package/_src/IO.ts +1 -0
- package/_src/Push/api.ts +70 -4
- package/_src/Push/definition.ts +6 -0
- package/_src/STM/definition.ts +2 -2
- package/_src/collection/immutable/Conc/dropUntilIO.ts +18 -15
- package/_src/collection/immutable/Conc/dropWhileIO.ts +18 -17
- package/_src/collection/immutable/Conc/filterIO.ts +1 -11
- package/_src/collection/immutable/Conc/mapIO.ts +2 -9
- package/_src/collection/immutable/Conc/takeWhileIO.ts +19 -28
- package/collection/immutable/Conc/filterIO.d.ts +1 -1
- package/collection/immutable/Conc/mapIO.d.ts +1 -1
- package/collection/immutable/Conc/takeWhileIO.d.ts +1 -1
- package/package.json +2 -2
package/_src/Channel/api.ts
CHANGED
@@ -827,10 +827,10 @@ export function mapOut<OutElem, OutElem2>(f: (o: OutElem) => OutElem2) {
|
|
827
827
|
const mapOutIOReader = <Env, Env1, OutErr, OutErr1, OutElem, OutElem1, OutDone>(
|
828
828
|
f: (o: OutElem) => IO<Env1, OutErr1, OutElem1>,
|
829
829
|
): Channel<Env | Env1, OutErr, OutElem, OutDone, OutErr | OutErr1, OutElem1, OutDone> =>
|
830
|
-
Channel.
|
830
|
+
Channel.readWithCause(
|
831
831
|
(out) => Channel.fromIO(f(out)).flatMap(Channel.writeNow).zipRight(mapOutIOReader(f)),
|
832
|
-
Channel.
|
833
|
-
Channel.
|
832
|
+
Channel.failCauseNow,
|
833
|
+
Channel.succeedNow,
|
834
834
|
);
|
835
835
|
|
836
836
|
/**
|
@@ -1,5 +1,6 @@
|
|
1
1
|
import type { FiberStatus } from "../FiberStatus.js";
|
2
2
|
import type { OnFailure, OnSuccess, OnSuccessAndFailure, UIO } from "../IO/definition.js";
|
3
|
+
import type { WhileLoop } from "../IO/definition.js";
|
3
4
|
import type { RuntimeFlags } from "../RuntimeFlags.js";
|
4
5
|
|
5
6
|
import { isIOError } from "@fncts/base/data/exceptions";
|
@@ -12,29 +13,22 @@ import { Done, Suspended } from "../FiberStatus.js";
|
|
12
13
|
import { Running } from "../FiberStatus.js";
|
13
14
|
import { FiberStatusTag } from "../FiberStatus.js";
|
14
15
|
import { LinkedQueue } from "../internal/MutableQueue.js";
|
15
|
-
import { isIO } from "../IO/definition.js";
|
16
|
-
import {
|
17
|
-
import { IOOpCode } from "../IO/definition.js";
|
16
|
+
import { IOPrimitive, isIO } from "../IO/definition.js";
|
17
|
+
import { IOTag } from "../IO/definition.js";
|
18
18
|
import { RuntimeFlag } from "../RuntimeFlag.js";
|
19
19
|
import { FiberMessage, FiberMessageTag } from "./FiberMessage.js";
|
20
20
|
|
21
21
|
export class RevertFlags {
|
22
|
-
readonly _tag =
|
22
|
+
readonly _tag = IOTag.RevertFlags;
|
23
23
|
constructor(readonly patch: RuntimeFlags.Patch, readonly trace?: string) {}
|
24
24
|
}
|
25
25
|
|
26
26
|
export class UpdateTrace {
|
27
|
-
readonly _tag =
|
27
|
+
readonly _tag = IOTag.UpdateTrace;
|
28
28
|
constructor(readonly trace?: string) {}
|
29
29
|
}
|
30
30
|
|
31
|
-
export type Continuation =
|
32
|
-
| OnSuccess<any, any, any, any, any, any>
|
33
|
-
| OnSuccessAndFailure<any, any, any, any, any, any, any, any, any>
|
34
|
-
| OnFailure<any, any, any, any, any, any>
|
35
|
-
| WhileLoop<any, any, any>
|
36
|
-
| UpdateTrace
|
37
|
-
| RevertFlags;
|
31
|
+
export type Continuation = OnSuccess | OnSuccessAndFailure | OnFailure | WhileLoop | UpdateTrace | RevertFlags;
|
38
32
|
|
39
33
|
/**
|
40
34
|
* @tsplus type fncts.io.Fiber
|
@@ -268,7 +262,7 @@ export class FiberRuntime<E, A> implements Fiber.Runtime<E, A> {
|
|
268
262
|
}
|
269
263
|
} catch (e) {
|
270
264
|
if (isIO(e)) {
|
271
|
-
if (IO.concrete(e)._tag ===
|
265
|
+
if (IO.concrete(e)._tag === IOTag.YieldNow) {
|
272
266
|
if (this._runtimeFlags.cooperativeYielding) {
|
273
267
|
this.tell(FiberMessage.YieldNow);
|
274
268
|
this.tell(FiberMessage.Resume(IO.unit));
|
@@ -276,7 +270,7 @@ export class FiberRuntime<E, A> implements Fiber.Runtime<E, A> {
|
|
276
270
|
} else {
|
277
271
|
effect = IO.unit;
|
278
272
|
}
|
279
|
-
} else if (IO.concrete(e)._tag ===
|
273
|
+
} else if (IO.concrete(e)._tag === IOTag.Async) {
|
280
274
|
effect = null!;
|
281
275
|
} else {
|
282
276
|
throw new Error(`Unhandled op ${IO.concrete(e)._tag}`);
|
@@ -302,6 +296,7 @@ export class FiberRuntime<E, A> implements Fiber.Runtime<E, A> {
|
|
302
296
|
if (this._runtimeFlags.opSupervision) {
|
303
297
|
this.getSupervisor().unsafeOnEffect(this, cur);
|
304
298
|
}
|
299
|
+
|
305
300
|
const nextTrace = IO.concrete(cur).trace;
|
306
301
|
if (nextTrace !== undefined) lastTrace = nextTrace;
|
307
302
|
|
@@ -317,27 +312,30 @@ export class FiberRuntime<E, A> implements Fiber.Runtime<E, A> {
|
|
317
312
|
}
|
318
313
|
try {
|
319
314
|
switch (cur._tag) {
|
320
|
-
case
|
321
|
-
const value = cur.
|
315
|
+
case IOTag.Sync: {
|
316
|
+
const value = cur.i0();
|
322
317
|
const cont = this.getNextSuccessCont();
|
323
318
|
if (cont) {
|
324
319
|
switch (cont._tag) {
|
325
|
-
case
|
326
|
-
|
327
|
-
|
320
|
+
case IOTag.OnSuccess: {
|
321
|
+
cur = IO.concrete(cont.i1(value));
|
322
|
+
break;
|
323
|
+
}
|
324
|
+
case IOTag.OnSuccessAndFailure: {
|
325
|
+
cur = IO.concrete(cont.i2(value));
|
328
326
|
break;
|
329
327
|
}
|
330
|
-
case
|
331
|
-
cont.
|
332
|
-
if (cont.
|
328
|
+
case IOTag.WhileLoop: {
|
329
|
+
cont.i2(value);
|
330
|
+
if (cont.i0()) {
|
333
331
|
this.stack.push(cont);
|
334
|
-
cur = IO.concrete(cont.
|
332
|
+
cur = IO.concrete(cont.i1());
|
335
333
|
} else {
|
336
334
|
cur = IO.concrete(IO.unit);
|
337
335
|
}
|
338
336
|
break;
|
339
337
|
}
|
340
|
-
case
|
338
|
+
case IOTag.RevertFlags: {
|
341
339
|
this.patchRuntimeFlags(this._runtimeFlags, cont.patch);
|
342
340
|
if (this._runtimeFlags.interruptible && this.isInterrupted()) {
|
343
341
|
cur = IO.concrete(IO.failCauseNow(this.getInterruptedCause()));
|
@@ -346,7 +344,7 @@ export class FiberRuntime<E, A> implements Fiber.Runtime<E, A> {
|
|
346
344
|
}
|
347
345
|
break;
|
348
346
|
}
|
349
|
-
case
|
347
|
+
case IOTag.UpdateTrace: {
|
350
348
|
if (cont.trace !== undefined) lastTrace = cont.trace;
|
351
349
|
cur = IO.concrete(IO.unit);
|
352
350
|
break;
|
@@ -357,26 +355,26 @@ export class FiberRuntime<E, A> implements Fiber.Runtime<E, A> {
|
|
357
355
|
}
|
358
356
|
break;
|
359
357
|
}
|
360
|
-
case
|
361
|
-
case
|
362
|
-
case
|
358
|
+
case IOTag.OnSuccessAndFailure:
|
359
|
+
case IOTag.OnFailure:
|
360
|
+
case IOTag.OnSuccess: {
|
363
361
|
this.stack.push(cur);
|
364
|
-
cur = IO.concrete(cur.
|
362
|
+
cur = IO.concrete(cur.i0);
|
365
363
|
break;
|
366
364
|
}
|
367
|
-
case
|
365
|
+
case IOTag.Async: {
|
368
366
|
this.asyncTrace = lastTrace;
|
369
|
-
this.asyncBlockinOn = cur.
|
370
|
-
this.initiateAsync(this._runtimeFlags, cur.
|
367
|
+
this.asyncBlockinOn = cur.i1();
|
368
|
+
this.initiateAsync(this._runtimeFlags, cur.i0);
|
371
369
|
throw cur;
|
372
370
|
}
|
373
|
-
case
|
374
|
-
const updateFlags = cur.
|
371
|
+
case IOTag.UpdateRuntimeFlagsWithin: {
|
372
|
+
const updateFlags = cur.i0;
|
375
373
|
const oldRuntimeFlags = this._runtimeFlags;
|
376
374
|
const newRuntimeFlags = updateFlags.patch(oldRuntimeFlags);
|
377
375
|
|
378
376
|
if (newRuntimeFlags === oldRuntimeFlags) {
|
379
|
-
cur = IO.concrete(cur.
|
377
|
+
cur = IO.concrete(cur.i1(oldRuntimeFlags));
|
380
378
|
} else {
|
381
379
|
if (newRuntimeFlags.interruptible && this.isInterrupted()) {
|
382
380
|
cur = IO.concrete(IO.failCauseNow(this.getInterruptedCause()));
|
@@ -384,72 +382,75 @@ export class FiberRuntime<E, A> implements Fiber.Runtime<E, A> {
|
|
384
382
|
this.patchRuntimeFlags(this._runtimeFlags, updateFlags);
|
385
383
|
const revertFlags = newRuntimeFlags.diff(oldRuntimeFlags);
|
386
384
|
this.stack.push(new RevertFlags(revertFlags));
|
387
|
-
cur = IO.concrete(cur.
|
385
|
+
cur = IO.concrete(cur.i1(oldRuntimeFlags));
|
388
386
|
}
|
389
387
|
}
|
390
388
|
break;
|
391
389
|
}
|
392
|
-
case
|
390
|
+
case IOTag.GenerateStackTrace: {
|
393
391
|
cur = IO.concrete(IO.succeedNow(this.generateStackTrace()));
|
394
392
|
break;
|
395
393
|
}
|
396
|
-
case
|
397
|
-
cur = IO.concrete(cur.
|
394
|
+
case IOTag.Stateful: {
|
395
|
+
cur = IO.concrete(cur.i0(this, new Running(this._runtimeFlags, lastTrace)));
|
398
396
|
break;
|
399
397
|
}
|
400
|
-
case
|
398
|
+
case IOTag.SucceedNow: {
|
401
399
|
const oldCur = cur;
|
402
400
|
const cont = this.getNextSuccessCont();
|
403
401
|
if (cont) {
|
404
402
|
switch (cont._tag) {
|
405
|
-
case
|
406
|
-
|
407
|
-
|
403
|
+
case IOTag.OnSuccess: {
|
404
|
+
cur = IO.concrete(cont.i1(oldCur.i0));
|
405
|
+
break;
|
406
|
+
}
|
407
|
+
case IOTag.OnSuccessAndFailure: {
|
408
|
+
cur = IO.concrete(cont.i2(oldCur.i0));
|
408
409
|
break;
|
409
410
|
}
|
410
|
-
case
|
411
|
-
cont.
|
412
|
-
if (cont.
|
411
|
+
case IOTag.WhileLoop: {
|
412
|
+
cont.i2(oldCur.i0);
|
413
|
+
if (cont.i0()) {
|
413
414
|
this.stack.push(cont);
|
414
|
-
cur = IO.concrete(cont.
|
415
|
+
cur = IO.concrete(cont.i1());
|
415
416
|
} else {
|
416
417
|
cur = IO.concrete(IO.unit);
|
417
418
|
}
|
418
419
|
break;
|
419
420
|
}
|
420
|
-
case
|
421
|
+
case IOTag.RevertFlags: {
|
421
422
|
this.patchRuntimeFlags(this._runtimeFlags, cont.patch);
|
422
423
|
if (this._runtimeFlags.interruptible && this.isInterrupted()) {
|
423
424
|
cur = IO.concrete(IO.failCauseNow(this.getInterruptedCause()));
|
424
425
|
}
|
425
426
|
break;
|
426
427
|
}
|
427
|
-
case
|
428
|
+
case IOTag.UpdateTrace: {
|
428
429
|
if (cont.trace !== undefined) lastTrace = cont.trace;
|
429
430
|
cur = IO.concrete(IO.unit);
|
430
431
|
break;
|
431
432
|
}
|
432
433
|
}
|
433
434
|
} else {
|
434
|
-
return Exit.succeed(oldCur.
|
435
|
+
return Exit.succeed(oldCur.i0);
|
435
436
|
}
|
436
437
|
break;
|
437
438
|
}
|
438
|
-
case
|
439
|
-
const cause = cur.
|
439
|
+
case IOTag.Fail: {
|
440
|
+
const cause = cur.i0();
|
440
441
|
const cont = this.getNextFailCont();
|
441
442
|
if (cont) {
|
442
443
|
switch (cont._tag) {
|
443
|
-
case
|
444
|
-
case
|
444
|
+
case IOTag.OnFailure:
|
445
|
+
case IOTag.OnSuccessAndFailure: {
|
445
446
|
if (!(this._runtimeFlags.interruptible && this.isInterrupted())) {
|
446
|
-
cur = IO.concrete(cont.
|
447
|
+
cur = IO.concrete(cont.i1(cause));
|
447
448
|
} else {
|
448
449
|
cur = IO.concrete(IO.failCauseNow(cause.stripFailures));
|
449
450
|
}
|
450
451
|
break;
|
451
452
|
}
|
452
|
-
case
|
453
|
+
case IOTag.RevertFlags: {
|
453
454
|
this.patchRuntimeFlags(this._runtimeFlags, cont.patch);
|
454
455
|
if (this._runtimeFlags.interruptible && this.isInterrupted()) {
|
455
456
|
const interruptedCause = this.getInterruptedCause();
|
@@ -461,7 +462,7 @@ export class FiberRuntime<E, A> implements Fiber.Runtime<E, A> {
|
|
461
462
|
}
|
462
463
|
break;
|
463
464
|
}
|
464
|
-
case
|
465
|
+
case IOTag.UpdateTrace: {
|
465
466
|
if (cont.trace !== undefined) lastTrace = cont.trace;
|
466
467
|
cur = IO.concrete(IO.unit);
|
467
468
|
break;
|
@@ -472,15 +473,15 @@ export class FiberRuntime<E, A> implements Fiber.Runtime<E, A> {
|
|
472
473
|
}
|
473
474
|
break;
|
474
475
|
}
|
475
|
-
case
|
476
|
-
this.patchRuntimeFlags(this._runtimeFlags, cur.
|
476
|
+
case IOTag.UpdateRuntimeFlags: {
|
477
|
+
this.patchRuntimeFlags(this._runtimeFlags, cur.i0);
|
477
478
|
cur = IO.concrete(IO.unit);
|
478
479
|
break;
|
479
480
|
}
|
480
|
-
case
|
481
|
+
case IOTag.WhileLoop: {
|
481
482
|
const iterate = cur;
|
482
|
-
const check = iterate.
|
483
|
-
const body = iterate.
|
483
|
+
const check = iterate.i0;
|
484
|
+
const body = iterate.i1;
|
484
485
|
if (check()) {
|
485
486
|
cur = IO.concrete(body());
|
486
487
|
this.stack.push(iterate);
|
@@ -489,10 +490,10 @@ export class FiberRuntime<E, A> implements Fiber.Runtime<E, A> {
|
|
489
490
|
}
|
490
491
|
break;
|
491
492
|
}
|
492
|
-
case
|
493
|
+
case IOTag.YieldNow: {
|
493
494
|
throw cur;
|
494
495
|
}
|
495
|
-
case
|
496
|
+
case IOTag.Commit: {
|
496
497
|
cur = IO.concrete(cur.commit);
|
497
498
|
break;
|
498
499
|
}
|
@@ -524,7 +525,7 @@ export class FiberRuntime<E, A> implements Fiber.Runtime<E, A> {
|
|
524
525
|
} catch (e) {
|
525
526
|
if (isIOError(e)) {
|
526
527
|
cur = IO.concrete(IO.failCauseNow(e.cause));
|
527
|
-
} else if (isIO(e) && (IO.concrete(e)._tag ===
|
528
|
+
} else if (isIO(e) && (IO.concrete(e)._tag === IOTag.Async || IO.concrete(e)._tag === IOTag.YieldNow)) {
|
528
529
|
throw e;
|
529
530
|
} else if (isInterruptedException(e)) {
|
530
531
|
cur = IO.concrete(IO.failCauseNow(Cause.parallel(Cause.halt(e), Cause.interrupt(FiberId.none))));
|
@@ -622,14 +623,14 @@ export class FiberRuntime<E, A> implements Fiber.Runtime<E, A> {
|
|
622
623
|
}
|
623
624
|
};
|
624
625
|
|
625
|
-
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
|
632
|
-
|
626
|
+
const io = new IOPrimitive(IOTag.WhileLoop) as any;
|
627
|
+
io.i0 = check;
|
628
|
+
io.i1 = body;
|
629
|
+
io.i2 = () => {
|
630
|
+
//
|
631
|
+
};
|
632
|
+
|
633
|
+
return io;
|
633
634
|
} else {
|
634
635
|
return null!;
|
635
636
|
}
|
@@ -822,7 +823,7 @@ export class FiberRuntime<E, A> implements Fiber.Runtime<E, A> {
|
|
822
823
|
getNextSuccessCont() {
|
823
824
|
while (this.stack.hasNext) {
|
824
825
|
const frame = this.stack.pop()!;
|
825
|
-
if (frame._tag !==
|
826
|
+
if (frame._tag !== IOTag.OnFailure) {
|
826
827
|
return frame;
|
827
828
|
}
|
828
829
|
}
|
@@ -831,7 +832,7 @@ export class FiberRuntime<E, A> implements Fiber.Runtime<E, A> {
|
|
831
832
|
getNextFailCont() {
|
832
833
|
while (this.stack.hasNext) {
|
833
834
|
const frame = this.stack.pop()!;
|
834
|
-
if (frame._tag !==
|
835
|
+
if (frame._tag !== IOTag.OnSuccess && frame._tag !== IOTag.WhileLoop) {
|
835
836
|
return frame;
|
836
837
|
}
|
837
838
|
}
|
@@ -0,0 +1,64 @@
|
|
1
|
+
import type { _A, _E, _R } from "@fncts/base/types";
|
2
|
+
import type { EnvironmentOf, ErrorOf, IOVariance, ValueOf } from "@fncts/io/IO";
|
3
|
+
|
4
|
+
/**
|
5
|
+
* @tsplus static fncts.io.IOOps all
|
6
|
+
*/
|
7
|
+
export function all<T extends ReadonlyArray<IO<any, any, any>>>(
|
8
|
+
ios: [...T],
|
9
|
+
): IO<
|
10
|
+
{ [K in number]: EnvironmentOf<T[K]> }[number],
|
11
|
+
{ [K in number]: ErrorOf<T[K]> }[number],
|
12
|
+
{ [K in keyof T]: ValueOf<T[K]> }
|
13
|
+
>;
|
14
|
+
export function all<T extends Iterable<IO<any, any, any>>>(
|
15
|
+
ios: T,
|
16
|
+
): [T] extends [Iterable<infer A>] ? IO<EnvironmentOf<A>, ErrorOf<A>, Conc<ValueOf<A>>> : never;
|
17
|
+
export function all<T extends Record<string, IO<any, any, any>>>(
|
18
|
+
ios: T,
|
19
|
+
): IO<
|
20
|
+
{ [K in keyof T]: EnvironmentOf<T[K]> }[keyof T],
|
21
|
+
{ [K in keyof T]: ErrorOf<T[K]> }[keyof T],
|
22
|
+
{ [K in keyof T]: ValueOf<T[K]> }
|
23
|
+
>;
|
24
|
+
export function all(ios: Record<string, IO<any, any, any>> | Iterable<IO<any, any, any>>): IO<any, any, any> {
|
25
|
+
if (Symbol.iterator in ios) {
|
26
|
+
return IO.foreach(ios, Function.identity);
|
27
|
+
} else {
|
28
|
+
return IO.foreach(
|
29
|
+
Object.entries(ios).map(([k, io]) => io.map((value) => [k, value] as const)),
|
30
|
+
Function.identity,
|
31
|
+
).map((result) => result.foldLeft({} as Record<string, any>, (b, a) => Object.assign(b, { [a[0]]: a[1] })));
|
32
|
+
}
|
33
|
+
}
|
34
|
+
|
35
|
+
/**
|
36
|
+
* @tsplus static fncts.io.IOOps allConcurrent
|
37
|
+
*/
|
38
|
+
export function allConcurrent<T extends ReadonlyArray<IO<any, any, any>>>(
|
39
|
+
ios: [...T],
|
40
|
+
): IO<
|
41
|
+
{ [K in number]: EnvironmentOf<T[K]> }[number],
|
42
|
+
{ [K in number]: ErrorOf<T[K]> }[number],
|
43
|
+
{ [K in keyof T]: ValueOf<T[K]> }
|
44
|
+
>;
|
45
|
+
export function allConcurrent<T extends Iterable<IO<any, any, any>>>(
|
46
|
+
ios: T,
|
47
|
+
): [T] extends [Iterable<infer A>] ? IO<EnvironmentOf<A>, ErrorOf<A>, Conc<ValueOf<A>>> : never;
|
48
|
+
export function allConcurrent<T extends Record<string, IO<any, any, any>>>(
|
49
|
+
ios: T,
|
50
|
+
): IO<
|
51
|
+
{ [K in keyof T]: EnvironmentOf<T[K]> }[keyof T],
|
52
|
+
{ [K in keyof T]: ErrorOf<T[K]> }[keyof T],
|
53
|
+
{ [K in keyof T]: ValueOf<T[K]> }
|
54
|
+
>;
|
55
|
+
export function allConcurrent(ios: Record<string, IO<any, any, any>> | Iterable<IO<any, any, any>>): IO<any, any, any> {
|
56
|
+
if (Symbol.iterator in ios) {
|
57
|
+
return IO.foreachConcurrent(ios, Function.identity);
|
58
|
+
} else {
|
59
|
+
return IO.foreachConcurrent(
|
60
|
+
Object.entries(ios).map(([k, io]) => io.map((value) => [k, value] as const)),
|
61
|
+
Function.identity,
|
62
|
+
).map((result) => result.foldLeft({} as Record<string, any>, (b, a) => Object.assign(b, { [a[0]]: a[1] })));
|
63
|
+
}
|
64
|
+
}
|
@@ -41,3 +41,36 @@ export function withConcurrency(n: number, __tsplusTrace?: string) {
|
|
41
41
|
export function withConcurrencyUnbounded<R, E, A>(ma: IO<R, E, A>, __tsplusTrace?: string): IO<R, E, A> {
|
42
42
|
return IO.defer(Concurrency.locally(Nothing())(ma));
|
43
43
|
}
|
44
|
+
|
45
|
+
export type ConcurrencyRestorer = <R, E, A>(io: IO<R, E, A>) => IO<R, E, A>;
|
46
|
+
|
47
|
+
const MakeConcurrent =
|
48
|
+
(n: number): ConcurrencyRestorer =>
|
49
|
+
(io) =>
|
50
|
+
io.withConcurrency(n);
|
51
|
+
|
52
|
+
const MakeConcurrentUnbounded: ConcurrencyRestorer = (io) => io.withConcurrencyUnbounded;
|
53
|
+
|
54
|
+
/**
|
55
|
+
* @tsplus static fncts.io.IOOps withConcurrencyMask
|
56
|
+
*/
|
57
|
+
export function withConcurrencyMask<R, E, A>(n: number, f: (restore: ConcurrencyRestorer) => IO<R, E, A>): IO<R, E, A> {
|
58
|
+
return Concurrency.getWith((concurrency) =>
|
59
|
+
concurrency.match(
|
60
|
+
() => Concurrency.locally(Just(n))(f(MakeConcurrentUnbounded)),
|
61
|
+
(n0) => Concurrency.locally(Just(n))(f(MakeConcurrent(n0))),
|
62
|
+
),
|
63
|
+
);
|
64
|
+
}
|
65
|
+
|
66
|
+
/**
|
67
|
+
* @tsplus static fncts.io.IOOps withConcurrencyUnboundedMask
|
68
|
+
*/
|
69
|
+
export function withConcurrencyUnboundedMask<R, E, A>(f: (restore: ConcurrencyRestorer) => IO<R, E, A>): IO<R, E, A> {
|
70
|
+
return Concurrency.getWith((concurrency) =>
|
71
|
+
concurrency.match(
|
72
|
+
() => Concurrency.locally(Nothing())(f(MakeConcurrentUnbounded)),
|
73
|
+
(n0) => Concurrency.locally(Nothing())(f(MakeConcurrent(n0))),
|
74
|
+
),
|
75
|
+
);
|
76
|
+
}
|
@@ -14,7 +14,7 @@ export function foreachExec<R, E, A, B>(
|
|
14
14
|
): IO<R, E, Conc<B>> {
|
15
15
|
return es.match(
|
16
16
|
() => IO.foreach(as, f),
|
17
|
-
() => IO.foreachConcurrent(as, f)
|
18
|
-
(fiberBound) => IO.foreachConcurrent(as,
|
17
|
+
() => IO.withConcurrencyUnboundedMask((restore) => IO.foreachConcurrent(as, (a) => restore(f(a)))),
|
18
|
+
(fiberBound) => IO.withConcurrencyMask(fiberBound, (restore) => IO.foreachConcurrent(as, (a) => restore(f(a)))),
|
19
19
|
);
|
20
20
|
}
|
package/_src/IO/api/interrupt.ts
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
import {
|
1
|
+
import { IOPrimitive,IOTag } from "@fncts/io/IO/definition";
|
2
2
|
import { RuntimeFlag } from "@fncts/io/RuntimeFlag";
|
3
3
|
import { RuntimeFlags } from "@fncts/io/RuntimeFlags";
|
4
4
|
|
@@ -40,7 +40,11 @@ export const interrupt: IO<never, never, never> = IO.fiberId.flatMap(IO.interrup
|
|
40
40
|
* @tsplus static fncts.io.IOOps interruptible
|
41
41
|
*/
|
42
42
|
export function interruptible<R, E, A>(self: IO<R, E, A>, __tsplusTrace?: string): IO<R, E, A> {
|
43
|
-
|
43
|
+
const io = new IOPrimitive(IOTag.UpdateRuntimeFlagsWithin) as any;
|
44
|
+
io.i0 = RuntimeFlags.enable(RuntimeFlag.Interruption);
|
45
|
+
io.i1 = () => self;
|
46
|
+
io.trace = __tsplusTrace;
|
47
|
+
return io;
|
44
48
|
}
|
45
49
|
|
46
50
|
/**
|
@@ -55,7 +59,11 @@ export function interruptible<R, E, A>(self: IO<R, E, A>, __tsplusTrace?: string
|
|
55
59
|
* @tsplus static fncts.io.IOOps uninterruptible
|
56
60
|
*/
|
57
61
|
export function uninterruptible<R, E, A>(self: IO<R, E, A>, __tsplusTrace?: string): IO<R, E, A> {
|
58
|
-
|
62
|
+
const io = new IOPrimitive(IOTag.UpdateRuntimeFlagsWithin) as any;
|
63
|
+
io.i0 = RuntimeFlags.disable(RuntimeFlag.Interruption);
|
64
|
+
io.i1 = () => self;
|
65
|
+
io.trace = __tsplusTrace;
|
66
|
+
return io;
|
59
67
|
}
|
60
68
|
|
61
69
|
/**
|
@@ -65,10 +73,15 @@ export function uninterruptible<R, E, A>(self: IO<R, E, A>, __tsplusTrace?: stri
|
|
65
73
|
*
|
66
74
|
* @tsplus static fncts.io.IOOps uninterruptibleMask
|
67
75
|
*/
|
68
|
-
export function uninterruptibleMask<R, E, A>(
|
69
|
-
|
70
|
-
|
71
|
-
|
76
|
+
export function uninterruptibleMask<R, E, A>(
|
77
|
+
f: (restore: InterruptibilityRestorer) => IO<R, E, A>,
|
78
|
+
__tsplusTrace?: string,
|
79
|
+
): IO<R, E, A> {
|
80
|
+
const io = new IOPrimitive(IOTag.UpdateRuntimeFlagsWithin) as any;
|
81
|
+
io.i0 = RuntimeFlags.disable(RuntimeFlag.Interruption);
|
82
|
+
io.i1 = (oldFlags: RuntimeFlags) => f(oldFlags.interruption ? RestoreInterruptible : RestoreUninterruptible);
|
83
|
+
io.trace = __tsplusTrace;
|
84
|
+
return io;
|
72
85
|
}
|
73
86
|
|
74
87
|
/**
|
package/_src/IO/api/raceWith.ts
CHANGED
@@ -10,6 +10,8 @@ export function raceFibersWith<R, E, A, R1, E1, B, R2, E2, C, R3, E3, D>(
|
|
10
10
|
right: Lazy<IO<R1, E1, B>>,
|
11
11
|
leftWins: (winner: FiberRuntime<E, A>, loser: FiberRuntime<E1, B>) => IO<R2, E2, C>,
|
12
12
|
rightWins: (winner: FiberRuntime<E1, B>, loser: FiberRuntime<E, A>) => IO<R3, E3, D>,
|
13
|
+
leftScope: FiberScope | null = null,
|
14
|
+
rightScope: FiberScope | null = null,
|
13
15
|
__tsplusTrace?: string,
|
14
16
|
) {
|
15
17
|
return (left: IO<R, E, A>): IO<R | R1 | R2 | R3, E2 | E3, C | D> => {
|
@@ -29,8 +31,8 @@ export function raceFibersWith<R, E, A, R1, E1, B, R2, E2, C, R3, E3, D>(
|
|
29
31
|
}
|
30
32
|
|
31
33
|
const raceIndicator = new AtomicBoolean(true);
|
32
|
-
const leftFiber = IO.unsafeMakeChildFiber(left, parentState, parentRuntimeFlags,
|
33
|
-
const rightFiber = IO.unsafeMakeChildFiber(right0, parentState, parentRuntimeFlags,
|
34
|
+
const leftFiber = IO.unsafeMakeChildFiber(left, parentState, parentRuntimeFlags, leftScope, __tsplusTrace);
|
35
|
+
const rightFiber = IO.unsafeMakeChildFiber(right0, parentState, parentRuntimeFlags, rightScope, __tsplusTrace);
|
34
36
|
|
35
37
|
return IO.async((cb) => {
|
36
38
|
leftFiber.addObserver(() => complete(leftFiber, rightFiber, leftWins, raceIndicator, cb));
|
package/_src/IO/api/timeout.ts
CHANGED
@@ -3,7 +3,27 @@
|
|
3
3
|
*/
|
4
4
|
export function timeoutTo<A, B, B1>(duration: Lazy<Duration>, b: Lazy<B>, f: (a: A) => B1, __tsplusTrace?: string) {
|
5
5
|
return <R, E>(self: IO<R, E, A>): IO<R, E, B | B1> => {
|
6
|
-
return
|
6
|
+
return IO.fiberIdWith((parentFiberId) =>
|
7
|
+
self.raceFibersWith(
|
8
|
+
IO.sleep(duration).interruptible,
|
9
|
+
(winner, loser) =>
|
10
|
+
winner.await.flatMap((exit) =>
|
11
|
+
exit.match(
|
12
|
+
(cause) => loser.interruptAs(parentFiberId) > IO.refailCause(cause),
|
13
|
+
(a) => winner.inheritAll > loser.interruptAs(parentFiberId).as(f(a)),
|
14
|
+
),
|
15
|
+
),
|
16
|
+
(winner, loser) =>
|
17
|
+
winner.await.flatMap((exit) =>
|
18
|
+
exit.match(
|
19
|
+
(cause) => loser.interruptAs(parentFiberId) > IO.refailCause(cause),
|
20
|
+
() => winner.inheritAll > loser.interruptAs(parentFiberId).as(b),
|
21
|
+
),
|
22
|
+
),
|
23
|
+
null,
|
24
|
+
FiberScope.global,
|
25
|
+
),
|
26
|
+
);
|
7
27
|
};
|
8
28
|
}
|
9
29
|
|