@fncts/io 0.0.34 → 0.0.35

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (173) hide show
  1. package/Fiber/FiberRuntime.d.ts +7 -7
  2. package/IO/api/all.d.ts +41 -0
  3. package/IO/api/concurrency.d.ts +11 -0
  4. package/IO/api/interrupt.d.ts +1 -1
  5. package/IO/api/raceWith.d.ts +2 -1
  6. package/IO/api.d.ts +7 -2
  7. package/IO/definition.d.ts +103 -127
  8. package/IO.d.ts +1 -0
  9. package/Push/api.d.ts +25 -4
  10. package/Push/definition.d.ts +17 -0
  11. package/STM/definition.d.ts +2 -2
  12. package/Sink/api.d.ts +19 -19
  13. package/_cjs/Channel/api/runScoped.cjs +1 -1
  14. package/_cjs/Channel/api/runScoped.cjs.map +1 -1
  15. package/_cjs/Channel/api.cjs +2 -2
  16. package/_cjs/Channel/api.cjs.map +1 -1
  17. package/_cjs/Fiber/FiberRuntime.cjs +110 -98
  18. package/_cjs/Fiber/FiberRuntime.cjs.map +1 -1
  19. package/_cjs/Future/api.cjs +1 -1
  20. package/_cjs/Future/api.cjs.map +1 -1
  21. package/_cjs/IO/api/all.cjs +33 -0
  22. package/_cjs/IO/api/all.cjs.map +1 -0
  23. package/_cjs/IO/api/asyncIO.cjs +1 -1
  24. package/_cjs/IO/api/asyncIO.cjs.map +1 -1
  25. package/_cjs/IO/api/bracketExit.cjs +1 -1
  26. package/_cjs/IO/api/bracketExit.cjs.map +1 -1
  27. package/_cjs/IO/api/concurrency.cjs +25 -4
  28. package/_cjs/IO/api/concurrency.cjs.map +1 -1
  29. package/_cjs/IO/api/disconnect.cjs +1 -1
  30. package/_cjs/IO/api/disconnect.cjs.map +1 -1
  31. package/_cjs/IO/api/foreachConcurrent.cjs +1 -1
  32. package/_cjs/IO/api/foreachConcurrent.cjs.map +1 -1
  33. package/_cjs/IO/api/foreachExec.cjs +1 -1
  34. package/_cjs/IO/api/foreachExec.cjs.map +1 -1
  35. package/_cjs/IO/api/forkIn.cjs +1 -1
  36. package/_cjs/IO/api/forkIn.cjs.map +1 -1
  37. package/_cjs/IO/api/forkScoped.cjs +1 -1
  38. package/_cjs/IO/api/forkScoped.cjs.map +1 -1
  39. package/_cjs/IO/api/fulfill.cjs +1 -1
  40. package/_cjs/IO/api/fulfill.cjs.map +1 -1
  41. package/_cjs/IO/api/interrupt.cjs +18 -6
  42. package/_cjs/IO/api/interrupt.cjs.map +1 -1
  43. package/_cjs/IO/api/raceWith.cjs +4 -4
  44. package/_cjs/IO/api/raceWith.cjs.map +1 -1
  45. package/_cjs/IO/api/timeout.cjs +8 -5
  46. package/_cjs/IO/api/timeout.cjs.map +1 -1
  47. package/_cjs/IO/api/zipConcurrent.cjs +1 -1
  48. package/_cjs/IO/api/zipConcurrent.cjs.map +1 -1
  49. package/_cjs/IO/api.cjs +78 -20
  50. package/_cjs/IO/api.cjs.map +1 -1
  51. package/_cjs/IO/definition.cjs +14 -191
  52. package/_cjs/IO/definition.cjs.map +1 -1
  53. package/_cjs/IO.cjs +11 -0
  54. package/_cjs/IO.cjs.map +1 -1
  55. package/_cjs/Layer/MemoMap.cjs +1 -1
  56. package/_cjs/Layer/MemoMap.cjs.map +1 -1
  57. package/_cjs/Push/api.cjs +149 -110
  58. package/_cjs/Push/api.cjs.map +1 -1
  59. package/_cjs/Push/definition.cjs.map +1 -1
  60. package/_cjs/STM/api/atomically.cjs +1 -1
  61. package/_cjs/STM/api/atomically.cjs.map +1 -1
  62. package/_cjs/STM/definition.cjs +1 -1
  63. package/_cjs/STM/definition.cjs.map +1 -1
  64. package/_cjs/ScopedRef/api.cjs +2 -2
  65. package/_cjs/ScopedRef/api.cjs.map +1 -1
  66. package/_cjs/Semaphore.cjs +1 -1
  67. package/_cjs/Semaphore.cjs.map +1 -1
  68. package/_cjs/TReentrantLock/api.cjs +2 -2
  69. package/_cjs/TReentrantLock/api.cjs.map +1 -1
  70. package/_cjs/TSemaphore/api.cjs +1 -1
  71. package/_cjs/TSemaphore/api.cjs.map +1 -1
  72. package/_cjs/collection/immutable/Conc/dropUntilIO.cjs +12 -17
  73. package/_cjs/collection/immutable/Conc/dropUntilIO.cjs.map +1 -1
  74. package/_cjs/collection/immutable/Conc/dropWhileIO.cjs +12 -17
  75. package/_cjs/collection/immutable/Conc/dropWhileIO.cjs.map +1 -1
  76. package/_cjs/collection/immutable/Conc/filterIO.cjs +2 -12
  77. package/_cjs/collection/immutable/Conc/filterIO.cjs.map +1 -1
  78. package/_cjs/collection/immutable/Conc/mapIO.cjs +3 -9
  79. package/_cjs/collection/immutable/Conc/mapIO.cjs.map +1 -1
  80. package/_cjs/collection/immutable/Conc/takeWhileIO.cjs +11 -27
  81. package/_cjs/collection/immutable/Conc/takeWhileIO.cjs.map +1 -1
  82. package/_mjs/Channel/api/runScoped.mjs +1 -1
  83. package/_mjs/Channel/api/runScoped.mjs.map +1 -1
  84. package/_mjs/Channel/api.mjs +2 -2
  85. package/_mjs/Channel/api.mjs.map +1 -1
  86. package/_mjs/Fiber/FiberRuntime.mjs +111 -100
  87. package/_mjs/Fiber/FiberRuntime.mjs.map +1 -1
  88. package/_mjs/Future/api.mjs +1 -1
  89. package/_mjs/Future/api.mjs.map +1 -1
  90. package/_mjs/IO/api/all.mjs +24 -0
  91. package/_mjs/IO/api/all.mjs.map +1 -0
  92. package/_mjs/IO/api/asyncIO.mjs +1 -1
  93. package/_mjs/IO/api/asyncIO.mjs.map +1 -1
  94. package/_mjs/IO/api/bracketExit.mjs +1 -1
  95. package/_mjs/IO/api/bracketExit.mjs.map +1 -1
  96. package/_mjs/IO/api/concurrency.mjs +19 -2
  97. package/_mjs/IO/api/concurrency.mjs.map +1 -1
  98. package/_mjs/IO/api/disconnect.mjs +1 -1
  99. package/_mjs/IO/api/disconnect.mjs.map +1 -1
  100. package/_mjs/IO/api/foreachConcurrent.mjs +1 -1
  101. package/_mjs/IO/api/foreachConcurrent.mjs.map +1 -1
  102. package/_mjs/IO/api/foreachExec.mjs +1 -1
  103. package/_mjs/IO/api/foreachExec.mjs.map +1 -1
  104. package/_mjs/IO/api/forkIn.mjs +1 -1
  105. package/_mjs/IO/api/forkIn.mjs.map +1 -1
  106. package/_mjs/IO/api/forkScoped.mjs +1 -1
  107. package/_mjs/IO/api/forkScoped.mjs.map +1 -1
  108. package/_mjs/IO/api/fulfill.mjs +1 -1
  109. package/_mjs/IO/api/fulfill.mjs.map +1 -1
  110. package/_mjs/IO/api/interrupt.mjs +19 -7
  111. package/_mjs/IO/api/interrupt.mjs.map +1 -1
  112. package/_mjs/IO/api/raceWith.mjs +4 -4
  113. package/_mjs/IO/api/raceWith.mjs.map +1 -1
  114. package/_mjs/IO/api/timeout.mjs +8 -5
  115. package/_mjs/IO/api/timeout.mjs.map +1 -1
  116. package/_mjs/IO/api/zipConcurrent.mjs +1 -1
  117. package/_mjs/IO/api/zipConcurrent.mjs.map +1 -1
  118. package/_mjs/IO/api.mjs +78 -23
  119. package/_mjs/IO/api.mjs.map +1 -1
  120. package/_mjs/IO/definition.mjs +12 -181
  121. package/_mjs/IO/definition.mjs.map +1 -1
  122. package/_mjs/IO.mjs +1 -0
  123. package/_mjs/IO.mjs.map +1 -1
  124. package/_mjs/Layer/MemoMap.mjs +1 -1
  125. package/_mjs/Layer/MemoMap.mjs.map +1 -1
  126. package/_mjs/Push/api.mjs +145 -109
  127. package/_mjs/Push/api.mjs.map +1 -1
  128. package/_mjs/Push/definition.mjs.map +1 -1
  129. package/_mjs/STM/api/atomically.mjs +1 -1
  130. package/_mjs/STM/api/atomically.mjs.map +1 -1
  131. package/_mjs/STM/definition.mjs +1 -1
  132. package/_mjs/STM/definition.mjs.map +1 -1
  133. package/_mjs/ScopedRef/api.mjs +2 -2
  134. package/_mjs/ScopedRef/api.mjs.map +1 -1
  135. package/_mjs/Semaphore.mjs +1 -1
  136. package/_mjs/Semaphore.mjs.map +1 -1
  137. package/_mjs/TReentrantLock/api.mjs +2 -2
  138. package/_mjs/TReentrantLock/api.mjs.map +1 -1
  139. package/_mjs/TSemaphore/api.mjs +1 -1
  140. package/_mjs/TSemaphore/api.mjs.map +1 -1
  141. package/_mjs/collection/immutable/Conc/dropUntilIO.mjs +12 -17
  142. package/_mjs/collection/immutable/Conc/dropUntilIO.mjs.map +1 -1
  143. package/_mjs/collection/immutable/Conc/dropWhileIO.mjs +12 -17
  144. package/_mjs/collection/immutable/Conc/dropWhileIO.mjs.map +1 -1
  145. package/_mjs/collection/immutable/Conc/filterIO.mjs +2 -12
  146. package/_mjs/collection/immutable/Conc/filterIO.mjs.map +1 -1
  147. package/_mjs/collection/immutable/Conc/mapIO.mjs +3 -9
  148. package/_mjs/collection/immutable/Conc/mapIO.mjs.map +1 -1
  149. package/_mjs/collection/immutable/Conc/takeWhileIO.mjs +11 -27
  150. package/_mjs/collection/immutable/Conc/takeWhileIO.mjs.map +1 -1
  151. package/_src/Channel/api.ts +3 -3
  152. package/_src/Fiber/FiberRuntime.ts +76 -75
  153. package/_src/IO/api/all.ts +64 -0
  154. package/_src/IO/api/concurrency.ts +33 -0
  155. package/_src/IO/api/foreachExec.ts +2 -2
  156. package/_src/IO/api/interrupt.ts +20 -7
  157. package/_src/IO/api/raceWith.ts +4 -2
  158. package/_src/IO/api/timeout.ts +21 -1
  159. package/_src/IO/api.ts +79 -27
  160. package/_src/IO/definition.ts +155 -199
  161. package/_src/IO.ts +1 -0
  162. package/_src/Push/api.ts +70 -4
  163. package/_src/Push/definition.ts +6 -0
  164. package/_src/STM/definition.ts +2 -2
  165. package/_src/collection/immutable/Conc/dropUntilIO.ts +18 -15
  166. package/_src/collection/immutable/Conc/dropWhileIO.ts +18 -17
  167. package/_src/collection/immutable/Conc/filterIO.ts +1 -11
  168. package/_src/collection/immutable/Conc/mapIO.ts +2 -9
  169. package/_src/collection/immutable/Conc/takeWhileIO.ts +19 -28
  170. package/collection/immutable/Conc/filterIO.d.ts +1 -1
  171. package/collection/immutable/Conc/mapIO.d.ts +1 -1
  172. package/collection/immutable/Conc/takeWhileIO.d.ts +1 -1
  173. package/package.json +2 -2
@@ -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.readWith(
830
+ Channel.readWithCause(
831
831
  (out) => Channel.fromIO(f(out)).flatMap(Channel.writeNow).zipRight(mapOutIOReader(f)),
832
- Channel.failNow,
833
- Channel.endNow,
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 { WhileLoop } from "../IO/definition.js";
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 = IOOpCode.RevertFlags;
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 = IOOpCode.UpdateTrace;
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 === IOOpCode.YieldNow) {
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 === IOOpCode.Async) {
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 IOOpCode.Sync: {
321
- const value = cur.evaluate();
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 IOOpCode.OnSuccess:
326
- case IOOpCode.OnSuccessAndFailure: {
327
- cur = IO.concrete(cont.successK(value));
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 IOOpCode.WhileLoop: {
331
- cont.process(value);
332
- if (cont.check()) {
328
+ case IOTag.WhileLoop: {
329
+ cont.i2(value);
330
+ if (cont.i0()) {
333
331
  this.stack.push(cont);
334
- cur = IO.concrete(cont.body());
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 IOOpCode.RevertFlags: {
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 IOOpCode.UpdateTrace: {
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 IOOpCode.OnSuccessAndFailure:
361
- case IOOpCode.OnFailure:
362
- case IOOpCode.OnSuccess: {
358
+ case IOTag.OnSuccessAndFailure:
359
+ case IOTag.OnFailure:
360
+ case IOTag.OnSuccess: {
363
361
  this.stack.push(cur);
364
- cur = IO.concrete(cur.first);
362
+ cur = IO.concrete(cur.i0);
365
363
  break;
366
364
  }
367
- case IOOpCode.Async: {
365
+ case IOTag.Async: {
368
366
  this.asyncTrace = lastTrace;
369
- this.asyncBlockinOn = cur.blockingOn();
370
- this.initiateAsync(this._runtimeFlags, cur.registerCallback);
367
+ this.asyncBlockinOn = cur.i1();
368
+ this.initiateAsync(this._runtimeFlags, cur.i0);
371
369
  throw cur;
372
370
  }
373
- case IOOpCode.UpdateRuntimeFlagsWithin: {
374
- const updateFlags = cur.update;
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.scope(oldRuntimeFlags));
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.scope(oldRuntimeFlags));
385
+ cur = IO.concrete(cur.i1(oldRuntimeFlags));
388
386
  }
389
387
  }
390
388
  break;
391
389
  }
392
- case IOOpCode.GenerateStackTrace: {
390
+ case IOTag.GenerateStackTrace: {
393
391
  cur = IO.concrete(IO.succeedNow(this.generateStackTrace()));
394
392
  break;
395
393
  }
396
- case IOOpCode.Stateful: {
397
- cur = IO.concrete(cur.onState(this, new Running(this._runtimeFlags, lastTrace)));
394
+ case IOTag.Stateful: {
395
+ cur = IO.concrete(cur.i0(this, new Running(this._runtimeFlags, lastTrace)));
398
396
  break;
399
397
  }
400
- case IOOpCode.SucceedNow: {
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 IOOpCode.OnSuccess:
406
- case IOOpCode.OnSuccessAndFailure: {
407
- cur = IO.concrete(cont.successK(oldCur.value));
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 IOOpCode.WhileLoop: {
411
- cont.process(oldCur.value);
412
- if (cont.check()) {
411
+ case IOTag.WhileLoop: {
412
+ cont.i2(oldCur.i0);
413
+ if (cont.i0()) {
413
414
  this.stack.push(cont);
414
- cur = IO.concrete(cont.body());
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 IOOpCode.RevertFlags: {
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 IOOpCode.UpdateTrace: {
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.value);
435
+ return Exit.succeed(oldCur.i0);
435
436
  }
436
437
  break;
437
438
  }
438
- case IOOpCode.Fail: {
439
- const cause = cur.cause();
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 IOOpCode.OnFailure:
444
- case IOOpCode.OnSuccessAndFailure: {
444
+ case IOTag.OnFailure:
445
+ case IOTag.OnSuccessAndFailure: {
445
446
  if (!(this._runtimeFlags.interruptible && this.isInterrupted())) {
446
- cur = IO.concrete(cont.failureK(cause));
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 IOOpCode.RevertFlags: {
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 IOOpCode.UpdateTrace: {
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 IOOpCode.UpdateRuntimeFlags: {
476
- this.patchRuntimeFlags(this._runtimeFlags, cur.update);
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 IOOpCode.WhileLoop: {
481
+ case IOTag.WhileLoop: {
481
482
  const iterate = cur;
482
- const check = iterate.check;
483
- const body = iterate.body;
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 IOOpCode.YieldNow: {
493
+ case IOTag.YieldNow: {
493
494
  throw cur;
494
495
  }
495
- case IOOpCode.Commit: {
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 === IOOpCode.Async || IO.concrete(e)._tag === IOOpCode.YieldNow)) {
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
- return new WhileLoop(
626
- check,
627
- body,
628
- () => {
629
- //
630
- },
631
- this.id.location,
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 !== IOOpCode.OnFailure) {
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 !== IOOpCode.OnSuccess && frame._tag !== IOOpCode.WhileLoop) {
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).withConcurrencyUnbounded,
18
- (fiberBound) => IO.foreachConcurrent(as, f).withConcurrency(fiberBound),
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
  }
@@ -1,4 +1,4 @@
1
- import { Dynamic, Interruptible, Uninterruptible } from "@fncts/io/IO/definition";
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
- return new Interruptible(self, __tsplusTrace);
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
- return new Uninterruptible(self, __tsplusTrace);
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>(f: (restore: InterruptibilityRestorer) => IO<R, E, A>): IO<R, E, A> {
69
- return new Dynamic(RuntimeFlags.disable(RuntimeFlag.Interruption), (oldFlags) =>
70
- f(oldFlags.interruption ? RestoreInterruptible : RestoreUninterruptible),
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
  /**
@@ -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, null, __tsplusTrace);
33
- const rightFiber = IO.unsafeMakeChildFiber(right0, parentState, parentRuntimeFlags, null, __tsplusTrace);
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));
@@ -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 self.map(f).raceFirst(IO.sleep(duration).interruptible.as(b));
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