@fncts/io 0.0.34 → 0.0.36

Sign up to get free protection for your applications and to get access to all the features.
Files changed (230) hide show
  1. package/Fiber/FiberRuntime.d.ts +7 -7
  2. package/Fiber/constructors.d.ts +5 -0
  3. package/IO/api/all.d.ts +41 -0
  4. package/IO/api/concurrency.d.ts +11 -0
  5. package/IO/api/interrupt.d.ts +1 -1
  6. package/IO/api/raceWith.d.ts +2 -1
  7. package/IO/api.d.ts +7 -2
  8. package/IO/definition.d.ts +103 -127
  9. package/IO.d.ts +1 -0
  10. package/Push/api.d.ts +86 -36
  11. package/Push/definition.d.ts +28 -13
  12. package/Push/internal.d.ts +7 -11
  13. package/RefSubject/Atomic.d.ts +8 -11
  14. package/RefSubject/Synchronized/definition.d.ts +4 -6
  15. package/RefSubject/api.d.ts +0 -1
  16. package/RefSubject/definition.d.ts +6 -8
  17. package/STM/definition.d.ts +19 -2
  18. package/Sink/api.d.ts +24 -24
  19. package/Subject/Atomic.d.ts +4 -6
  20. package/Subject/definition.d.ts +2 -2
  21. package/_cjs/Channel/api/runScoped.cjs +1 -1
  22. package/_cjs/Channel/api/runScoped.cjs.map +1 -1
  23. package/_cjs/Channel/api.cjs +2 -2
  24. package/_cjs/Channel/api.cjs.map +1 -1
  25. package/_cjs/Fiber/FiberRuntime.cjs +110 -98
  26. package/_cjs/Fiber/FiberRuntime.cjs.map +1 -1
  27. package/_cjs/Fiber/constructors.cjs +10 -2
  28. package/_cjs/Fiber/constructors.cjs.map +1 -1
  29. package/_cjs/Future/api.cjs +1 -1
  30. package/_cjs/Future/api.cjs.map +1 -1
  31. package/_cjs/IO/api/all.cjs +33 -0
  32. package/_cjs/IO/api/all.cjs.map +1 -0
  33. package/_cjs/IO/api/asyncIO.cjs +1 -1
  34. package/_cjs/IO/api/asyncIO.cjs.map +1 -1
  35. package/_cjs/IO/api/bracketExit.cjs +1 -1
  36. package/_cjs/IO/api/bracketExit.cjs.map +1 -1
  37. package/_cjs/IO/api/concurrency.cjs +25 -4
  38. package/_cjs/IO/api/concurrency.cjs.map +1 -1
  39. package/_cjs/IO/api/disconnect.cjs +1 -1
  40. package/_cjs/IO/api/disconnect.cjs.map +1 -1
  41. package/_cjs/IO/api/foreachConcurrent.cjs +1 -1
  42. package/_cjs/IO/api/foreachConcurrent.cjs.map +1 -1
  43. package/_cjs/IO/api/foreachExec.cjs +1 -1
  44. package/_cjs/IO/api/foreachExec.cjs.map +1 -1
  45. package/_cjs/IO/api/forkIn.cjs +1 -1
  46. package/_cjs/IO/api/forkIn.cjs.map +1 -1
  47. package/_cjs/IO/api/forkScoped.cjs +1 -1
  48. package/_cjs/IO/api/forkScoped.cjs.map +1 -1
  49. package/_cjs/IO/api/fulfill.cjs +1 -1
  50. package/_cjs/IO/api/fulfill.cjs.map +1 -1
  51. package/_cjs/IO/api/interrupt.cjs +18 -6
  52. package/_cjs/IO/api/interrupt.cjs.map +1 -1
  53. package/_cjs/IO/api/raceWith.cjs +4 -4
  54. package/_cjs/IO/api/raceWith.cjs.map +1 -1
  55. package/_cjs/IO/api/timeout.cjs +8 -5
  56. package/_cjs/IO/api/timeout.cjs.map +1 -1
  57. package/_cjs/IO/api/zipConcurrent.cjs +1 -1
  58. package/_cjs/IO/api/zipConcurrent.cjs.map +1 -1
  59. package/_cjs/IO/api.cjs +78 -20
  60. package/_cjs/IO/api.cjs.map +1 -1
  61. package/_cjs/IO/definition.cjs +14 -191
  62. package/_cjs/IO/definition.cjs.map +1 -1
  63. package/_cjs/IO.cjs +11 -0
  64. package/_cjs/IO.cjs.map +1 -1
  65. package/_cjs/Layer/MemoMap.cjs +1 -1
  66. package/_cjs/Layer/MemoMap.cjs.map +1 -1
  67. package/_cjs/Layer/api.cjs.map +1 -1
  68. package/_cjs/Push/api.cjs +238 -168
  69. package/_cjs/Push/api.cjs.map +1 -1
  70. package/_cjs/Push/definition.cjs +12 -13
  71. package/_cjs/Push/definition.cjs.map +1 -1
  72. package/_cjs/Push/internal.cjs +37 -29
  73. package/_cjs/Push/internal.cjs.map +1 -1
  74. package/_cjs/RefSubject/Atomic.cjs +15 -19
  75. package/_cjs/RefSubject/Atomic.cjs.map +1 -1
  76. package/_cjs/RefSubject/Synchronized/definition.cjs +9 -10
  77. package/_cjs/RefSubject/Synchronized/definition.cjs.map +1 -1
  78. package/_cjs/RefSubject/api.cjs +5 -6
  79. package/_cjs/RefSubject/api.cjs.map +1 -1
  80. package/_cjs/RefSubject/definition.cjs.map +1 -1
  81. package/_cjs/STM/api/atomically.cjs +1 -1
  82. package/_cjs/STM/api/atomically.cjs.map +1 -1
  83. package/_cjs/STM/api.cjs +2 -2
  84. package/_cjs/STM/api.cjs.map +1 -1
  85. package/_cjs/STM/definition.cjs +1 -1
  86. package/_cjs/STM/definition.cjs.map +1 -1
  87. package/_cjs/ScopedRef/api.cjs +2 -2
  88. package/_cjs/ScopedRef/api.cjs.map +1 -1
  89. package/_cjs/Semaphore.cjs +1 -1
  90. package/_cjs/Semaphore.cjs.map +1 -1
  91. package/_cjs/Sink/api.cjs +13 -13
  92. package/_cjs/Sink/api.cjs.map +1 -1
  93. package/_cjs/Subject/Atomic.cjs +4 -5
  94. package/_cjs/Subject/Atomic.cjs.map +1 -1
  95. package/_cjs/TReentrantLock/api.cjs +2 -2
  96. package/_cjs/TReentrantLock/api.cjs.map +1 -1
  97. package/_cjs/TSemaphore/api.cjs +1 -1
  98. package/_cjs/TSemaphore/api.cjs.map +1 -1
  99. package/_cjs/collection/immutable/Conc/dropUntilIO.cjs +12 -17
  100. package/_cjs/collection/immutable/Conc/dropUntilIO.cjs.map +1 -1
  101. package/_cjs/collection/immutable/Conc/dropWhileIO.cjs +12 -17
  102. package/_cjs/collection/immutable/Conc/dropWhileIO.cjs.map +1 -1
  103. package/_cjs/collection/immutable/Conc/filterIO.cjs +2 -12
  104. package/_cjs/collection/immutable/Conc/filterIO.cjs.map +1 -1
  105. package/_cjs/collection/immutable/Conc/mapIO.cjs +3 -9
  106. package/_cjs/collection/immutable/Conc/mapIO.cjs.map +1 -1
  107. package/_cjs/collection/immutable/Conc/takeWhileIO.cjs +11 -27
  108. package/_cjs/collection/immutable/Conc/takeWhileIO.cjs.map +1 -1
  109. package/_mjs/Channel/api/runScoped.mjs +1 -1
  110. package/_mjs/Channel/api/runScoped.mjs.map +1 -1
  111. package/_mjs/Channel/api.mjs +2 -2
  112. package/_mjs/Channel/api.mjs.map +1 -1
  113. package/_mjs/Fiber/FiberRuntime.mjs +111 -100
  114. package/_mjs/Fiber/FiberRuntime.mjs.map +1 -1
  115. package/_mjs/Fiber/constructors.mjs +7 -1
  116. package/_mjs/Fiber/constructors.mjs.map +1 -1
  117. package/_mjs/Future/api.mjs +1 -1
  118. package/_mjs/Future/api.mjs.map +1 -1
  119. package/_mjs/IO/api/all.mjs +24 -0
  120. package/_mjs/IO/api/all.mjs.map +1 -0
  121. package/_mjs/IO/api/asyncIO.mjs +1 -1
  122. package/_mjs/IO/api/asyncIO.mjs.map +1 -1
  123. package/_mjs/IO/api/bracketExit.mjs +1 -1
  124. package/_mjs/IO/api/bracketExit.mjs.map +1 -1
  125. package/_mjs/IO/api/concurrency.mjs +19 -2
  126. package/_mjs/IO/api/concurrency.mjs.map +1 -1
  127. package/_mjs/IO/api/disconnect.mjs +1 -1
  128. package/_mjs/IO/api/disconnect.mjs.map +1 -1
  129. package/_mjs/IO/api/foreachConcurrent.mjs +1 -1
  130. package/_mjs/IO/api/foreachConcurrent.mjs.map +1 -1
  131. package/_mjs/IO/api/foreachExec.mjs +1 -1
  132. package/_mjs/IO/api/foreachExec.mjs.map +1 -1
  133. package/_mjs/IO/api/forkIn.mjs +1 -1
  134. package/_mjs/IO/api/forkIn.mjs.map +1 -1
  135. package/_mjs/IO/api/forkScoped.mjs +1 -1
  136. package/_mjs/IO/api/forkScoped.mjs.map +1 -1
  137. package/_mjs/IO/api/fulfill.mjs +1 -1
  138. package/_mjs/IO/api/fulfill.mjs.map +1 -1
  139. package/_mjs/IO/api/interrupt.mjs +19 -7
  140. package/_mjs/IO/api/interrupt.mjs.map +1 -1
  141. package/_mjs/IO/api/raceWith.mjs +4 -4
  142. package/_mjs/IO/api/raceWith.mjs.map +1 -1
  143. package/_mjs/IO/api/timeout.mjs +8 -5
  144. package/_mjs/IO/api/timeout.mjs.map +1 -1
  145. package/_mjs/IO/api/zipConcurrent.mjs +1 -1
  146. package/_mjs/IO/api/zipConcurrent.mjs.map +1 -1
  147. package/_mjs/IO/api.mjs +78 -23
  148. package/_mjs/IO/api.mjs.map +1 -1
  149. package/_mjs/IO/definition.mjs +12 -181
  150. package/_mjs/IO/definition.mjs.map +1 -1
  151. package/_mjs/IO.mjs +1 -0
  152. package/_mjs/IO.mjs.map +1 -1
  153. package/_mjs/Layer/MemoMap.mjs +1 -1
  154. package/_mjs/Layer/MemoMap.mjs.map +1 -1
  155. package/_mjs/Layer/api.mjs.map +1 -1
  156. package/_mjs/Push/api.mjs +223 -166
  157. package/_mjs/Push/api.mjs.map +1 -1
  158. package/_mjs/Push/definition.mjs +9 -10
  159. package/_mjs/Push/definition.mjs.map +1 -1
  160. package/_mjs/Push/internal.mjs +33 -22
  161. package/_mjs/Push/internal.mjs.map +1 -1
  162. package/_mjs/RefSubject/Atomic.mjs +15 -19
  163. package/_mjs/RefSubject/Atomic.mjs.map +1 -1
  164. package/_mjs/RefSubject/Synchronized/definition.mjs +9 -10
  165. package/_mjs/RefSubject/Synchronized/definition.mjs.map +1 -1
  166. package/_mjs/RefSubject/api.mjs +6 -7
  167. package/_mjs/RefSubject/api.mjs.map +1 -1
  168. package/_mjs/RefSubject/definition.mjs.map +1 -1
  169. package/_mjs/STM/api/atomically.mjs +1 -1
  170. package/_mjs/STM/api/atomically.mjs.map +1 -1
  171. package/_mjs/STM/api.mjs +2 -2
  172. package/_mjs/STM/api.mjs.map +1 -1
  173. package/_mjs/STM/definition.mjs +1 -1
  174. package/_mjs/STM/definition.mjs.map +1 -1
  175. package/_mjs/ScopedRef/api.mjs +2 -2
  176. package/_mjs/ScopedRef/api.mjs.map +1 -1
  177. package/_mjs/Semaphore.mjs +1 -1
  178. package/_mjs/Semaphore.mjs.map +1 -1
  179. package/_mjs/Sink/api.mjs +10 -10
  180. package/_mjs/Sink/api.mjs.map +1 -1
  181. package/_mjs/Subject/Atomic.mjs +4 -5
  182. package/_mjs/Subject/Atomic.mjs.map +1 -1
  183. package/_mjs/TReentrantLock/api.mjs +2 -2
  184. package/_mjs/TReentrantLock/api.mjs.map +1 -1
  185. package/_mjs/TSemaphore/api.mjs +1 -1
  186. package/_mjs/TSemaphore/api.mjs.map +1 -1
  187. package/_mjs/collection/immutable/Conc/dropUntilIO.mjs +12 -17
  188. package/_mjs/collection/immutable/Conc/dropUntilIO.mjs.map +1 -1
  189. package/_mjs/collection/immutable/Conc/dropWhileIO.mjs +12 -17
  190. package/_mjs/collection/immutable/Conc/dropWhileIO.mjs.map +1 -1
  191. package/_mjs/collection/immutable/Conc/filterIO.mjs +2 -12
  192. package/_mjs/collection/immutable/Conc/filterIO.mjs.map +1 -1
  193. package/_mjs/collection/immutable/Conc/mapIO.mjs +3 -9
  194. package/_mjs/collection/immutable/Conc/mapIO.mjs.map +1 -1
  195. package/_mjs/collection/immutable/Conc/takeWhileIO.mjs +11 -27
  196. package/_mjs/collection/immutable/Conc/takeWhileIO.mjs.map +1 -1
  197. package/_src/Channel/api.ts +3 -3
  198. package/_src/Fiber/FiberRuntime.ts +76 -75
  199. package/_src/Fiber/constructors.ts +5 -0
  200. package/_src/IO/api/all.ts +64 -0
  201. package/_src/IO/api/concurrency.ts +33 -0
  202. package/_src/IO/api/foreachExec.ts +2 -2
  203. package/_src/IO/api/interrupt.ts +20 -7
  204. package/_src/IO/api/raceWith.ts +4 -2
  205. package/_src/IO/api/timeout.ts +21 -1
  206. package/_src/IO/api.ts +79 -27
  207. package/_src/IO/definition.ts +155 -200
  208. package/_src/IO.ts +1 -0
  209. package/_src/Layer/api.ts +0 -1
  210. package/_src/Push/api.ts +305 -304
  211. package/_src/Push/definition.ts +19 -17
  212. package/_src/Push/internal.ts +63 -31
  213. package/_src/RefSubject/Atomic.ts +16 -22
  214. package/_src/RefSubject/Synchronized/definition.ts +6 -9
  215. package/_src/RefSubject/api.ts +9 -12
  216. package/_src/RefSubject/definition.ts +6 -8
  217. package/_src/STM/api.ts +0 -5
  218. package/_src/STM/definition.ts +8 -2
  219. package/_src/Sink/api.ts +9 -9
  220. package/_src/Subject/Atomic.ts +6 -8
  221. package/_src/Subject/definition.ts +2 -2
  222. package/_src/collection/immutable/Conc/dropUntilIO.ts +18 -15
  223. package/_src/collection/immutable/Conc/dropWhileIO.ts +18 -17
  224. package/_src/collection/immutable/Conc/filterIO.ts +1 -11
  225. package/_src/collection/immutable/Conc/mapIO.ts +2 -9
  226. package/_src/collection/immutable/Conc/takeWhileIO.ts +19 -28
  227. package/collection/immutable/Conc/filterIO.d.ts +1 -1
  228. package/collection/immutable/Conc/mapIO.d.ts +1 -1
  229. package/collection/immutable/Conc/takeWhileIO.d.ts +1 -1
  230. package/package.json +2 -2
@@ -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
 
package/_src/IO/api.ts CHANGED
@@ -6,17 +6,8 @@ import type { RuntimeFlags } from "@fncts/io/RuntimeFlags";
6
6
 
7
7
  import { IOError } from "@fncts/base/data/exceptions";
8
8
  import { identity, pipe, tuple } from "@fncts/base/data/function";
9
- import {
10
- Async,
11
- GenerateStackTrace,
12
- OnSuccess,
13
- OnSuccessAndFailure,
14
- Sync,
15
- UpdateRuntimeFlags,
16
- YieldNow,
17
- } from "@fncts/io/IO/definition";
18
- import { Stateful } from "@fncts/io/IO/definition";
19
- import { Fail, IO, SucceedNow } from "@fncts/io/IO/definition";
9
+ import { IOPrimitive,IOTag } from "@fncts/io/IO/definition";
10
+ import { IO } from "@fncts/io/IO/definition";
20
11
 
21
12
  /**
22
13
  * Imports an asynchronous side-effect into a `IO`
@@ -28,7 +19,11 @@ export function async<R, E, A>(
28
19
  blockingOn: FiberId = FiberId.none,
29
20
  __tsplusTrace?: string,
30
21
  ): IO<R, E, A> {
31
- return new Async(register, () => blockingOn, __tsplusTrace);
22
+ const io = new IOPrimitive(IOTag.Async) as any;
23
+ io.i0 = register;
24
+ io.i1 = () => blockingOn;
25
+ io.trace = __tsplusTrace;
26
+ return io;
32
27
  }
33
28
 
34
29
  /**
@@ -318,7 +313,11 @@ export function checkInterruptible<R, E, A>(
318
313
  */
319
314
  export function flatMap<A, R1, E1, B>(f: (a: A) => IO<R1, E1, B>, __tsplusTrace?: string) {
320
315
  return <R, E>(ma: IO<R, E, A>): IO<R | R1, E | E1, B> => {
321
- return new OnSuccess(ma, f, __tsplusTrace);
316
+ const io = new IOPrimitive(IOTag.OnSuccess) as any;
317
+ io.i0 = ma;
318
+ io.i1 = f;
319
+ io.trace = __tsplusTrace;
320
+ return io;
322
321
  };
323
322
  }
324
323
 
@@ -369,7 +368,7 @@ export function condIO<R, R1, E, A>(
369
368
  *
370
369
  * @tsplus static fncts.io.IOOps defer
371
370
  */
372
- export function defer<R, E, A>(io: Lazy<IO<R, E, A>>, __tsplusTrace?: string): IO<R, E, A> {
371
+ export function defer<R = never, E = never, A = never>(io: Lazy<IO<R, E, A>>, __tsplusTrace?: string): IO<R, E, A> {
373
372
  return IO.succeed(io).flatMap(identity);
374
373
  }
375
374
 
@@ -379,7 +378,10 @@ export function defer<R, E, A>(io: Lazy<IO<R, E, A>>, __tsplusTrace?: string): I
379
378
  *
380
379
  * @tsplus static fncts.io.IOOps deferTry
381
380
  */
382
- export function deferTry<R, E, A>(io: () => IO<R, E, A>, __tsplusTrace?: string): IO<R, unknown, A> {
381
+ export function deferTry<R = never, E = never, A = never>(
382
+ io: () => IO<R, E, A>,
383
+ __tsplusTrace?: string,
384
+ ): IO<R, unknown, A> {
383
385
  return IO.defer(() => {
384
386
  try {
385
387
  return io();
@@ -463,7 +465,10 @@ export function failNow<E>(e: E, __tsplusTrace?: string): FIO<E, never> {
463
465
  * @tsplus static fncts.io.IOOps refailCause
464
466
  */
465
467
  export function refailCause<E>(cause: Cause<E>, __tsplusTrace?: string): FIO<E, never> {
466
- return new Fail(() => cause, __tsplusTrace);
468
+ const io = new IOPrimitive(IOTag.Fail) as any;
469
+ io.i0 = () => cause;
470
+ io.trace = __tsplusTrace;
471
+ return io;
467
472
  }
468
473
 
469
474
  /**
@@ -472,7 +477,10 @@ export function refailCause<E>(cause: Cause<E>, __tsplusTrace?: string): FIO<E,
472
477
  * @tsplus static fncts.io.IOOps failCauseNow
473
478
  */
474
479
  export function failCauseNow<E>(cause: Cause<E>, __tsplusTrace?: string): FIO<E, never> {
475
- return new Fail(() => cause, __tsplusTrace);
480
+ const io = new IOPrimitive(IOTag.Fail) as any;
481
+ io.i0 = () => cause;
482
+ io.trace = __tsplusTrace;
483
+ return io;
476
484
  }
477
485
 
478
486
  /**
@@ -1223,7 +1231,12 @@ export function matchCauseIO<E, A, R1, E1, A1, R2, E2, A2>(
1223
1231
  __tsplusTrace?: string,
1224
1232
  ) {
1225
1233
  return <R>(self: IO<R, E, A>): IO<R | R1 | R2, E1 | E2, A1 | A2> => {
1226
- return new OnSuccessAndFailure(self, onFailure, onSuccess, __tsplusTrace);
1234
+ const io = new IOPrimitive(IOTag.OnSuccessAndFailure) as any;
1235
+ io.i0 = self;
1236
+ io.i1 = onFailure;
1237
+ io.i2 = onSuccess;
1238
+ io.trace = __tsplusTrace;
1239
+ return io;
1227
1240
  };
1228
1241
  }
1229
1242
 
@@ -1664,7 +1677,10 @@ export function sequenceIterableDiscard<R, E, A>(as: Iterable<IO<R, E, A>>, __ts
1664
1677
  * @tsplus static fncts.io.IOOps succeedNow
1665
1678
  */
1666
1679
  export function succeedNow<A>(value: A, __tsplusTrace?: string): IO<never, never, A> {
1667
- return new SucceedNow(value, __tsplusTrace);
1680
+ const io = new IOPrimitive(IOTag.SucceedNow) as any;
1681
+ io.i0 = value;
1682
+ io.trace = __tsplusTrace;
1683
+ return io;
1668
1684
  }
1669
1685
 
1670
1686
  /**
@@ -1676,7 +1692,10 @@ export function succeedNow<A>(value: A, __tsplusTrace?: string): IO<never, never
1676
1692
  * @tsplus static fncts.io.IOOps __call
1677
1693
  */
1678
1694
  export function succeed<A>(effect: Lazy<A>, __tsplusTrace?: string): UIO<A> {
1679
- return new Sync(effect, __tsplusTrace);
1695
+ const io = new IOPrimitive(IOTag.Sync) as any;
1696
+ io.i0 = effect;
1697
+ io.trace = __tsplusTrace;
1698
+ return io;
1680
1699
  }
1681
1700
 
1682
1701
  /**
@@ -1721,17 +1740,42 @@ export function timedWith<R1, E1>(msTime: IO<R1, E1, number>, __tsplusTrace?: st
1721
1740
  ma.summarized(msTime, (start, end) => end - start);
1722
1741
  }
1723
1742
 
1743
+ /**
1744
+ * @tsplus static fncts.io.IOOps attempt
1745
+ */
1746
+ export function attempt<A>(effect: Lazy<A>, __tsplusTrace?: string): FIO<unknown, A> {
1747
+ return IO.defer(() => {
1748
+ try {
1749
+ return Exit.succeed(effect());
1750
+ } catch (u) {
1751
+ return IO.withFiberRuntime((fiberState) => {
1752
+ if (!fiberState.isFatal(u)) {
1753
+ throw new IOError(Cause.fail(u));
1754
+ } else {
1755
+ throw u;
1756
+ }
1757
+ });
1758
+ }
1759
+ });
1760
+ }
1761
+
1724
1762
  /**
1725
1763
  * Creates a `IO` that has succeeded with a pure value
1726
1764
  *
1727
1765
  * @tsplus static fncts.io.IOOps tryCatch
1728
1766
  */
1729
1767
  export function tryCatch<E, A>(effect: Lazy<A>, onThrow: (error: unknown) => E, __tsplusTrace?: string): FIO<E, A> {
1730
- return IO.succeed(() => {
1768
+ return IO.defer(() => {
1731
1769
  try {
1732
- return effect();
1770
+ return Exit.succeed(effect());
1733
1771
  } catch (u) {
1734
- throw new IOError(Cause.fail(onThrow(u)));
1772
+ return IO.withFiberRuntime((fiberState) => {
1773
+ if (!fiberState.isFatal(u)) {
1774
+ throw new IOError(Cause.fail(u));
1775
+ } else {
1776
+ throw u;
1777
+ }
1778
+ });
1735
1779
  }
1736
1780
  });
1737
1781
  }
@@ -1902,21 +1946,29 @@ export function withFiberRuntime<R, E, A>(
1902
1946
  onState: (fiber: FiberRuntime<E, A>, status: Running) => IO<R, E, A>,
1903
1947
  __tsplusTrace?: string,
1904
1948
  ): IO<R, E, A> {
1905
- return new Stateful(onState, __tsplusTrace);
1949
+ const io = new IOPrimitive(IOTag.Stateful) as any;
1950
+ io.i0 = onState;
1951
+ io.trace = __tsplusTrace;
1952
+ return io;
1906
1953
  }
1907
1954
 
1908
1955
  /**
1909
1956
  * @tsplus static fncts.io.IOOps updateRuntimeFlags
1910
1957
  */
1911
1958
  export function updateRuntimeFlags(patch: RuntimeFlags.Patch, __tsplusTrace?: string): IO<never, never, void> {
1912
- return new UpdateRuntimeFlags(patch, __tsplusTrace);
1959
+ const io = new IOPrimitive(IOTag.UpdateRuntimeFlags) as any;
1960
+ io.i0 = patch;
1961
+ io.trace = __tsplusTrace;
1962
+ return io;
1913
1963
  }
1914
1964
 
1915
1965
  /**
1916
1966
  * @tsplus static fncts.io.IOOps stackTrace
1917
1967
  */
1918
1968
  export function stackTrace(__tsplusTrace?: string): UIO<Trace> {
1919
- return new GenerateStackTrace(__tsplusTrace);
1969
+ const io = new IOPrimitive(IOTag.GenerateStackTrace) as any;
1970
+ io.trace = __tsplusTrace;
1971
+ return io;
1920
1972
  }
1921
1973
 
1922
1974
  /**
@@ -1926,7 +1978,7 @@ export function stackTrace(__tsplusTrace?: string): UIO<Trace> {
1926
1978
  *
1927
1979
  * @tsplus static fncts.io.IOOps yieldNow
1928
1980
  */
1929
- export const yieldNow: UIO<void> = new YieldNow();
1981
+ export const yieldNow: UIO<void> = new IOPrimitive(IOTag.YieldNow) as any;
1930
1982
 
1931
1983
  /**
1932
1984
  * @tsplus pipeable fncts.io.IO zip