@fncts/io 0.0.17 → 0.0.19

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 (219) hide show
  1. package/Cached/internal.d.ts +1 -1
  2. package/CancellerState.d.ts +1 -1
  3. package/Channel/ChildExecutorDecision/definition.d.ts +1 -1
  4. package/Channel/UpstreamPullRequest/definition.d.ts +1 -1
  5. package/Channel/UpstreamPullStrategy/definition.d.ts +1 -1
  6. package/Channel/api/mergeAllWith.d.ts +1 -1
  7. package/Channel/internal/ChannelExecutor.d.ts +1 -1
  8. package/Channel/internal/ChannelState.d.ts +2 -2
  9. package/Channel/internal/MergeDecision.d.ts +1 -1
  10. package/Channel/internal/MergeState.d.ts +1 -1
  11. package/Channel/internal/SingleProducerAsyncInput.d.ts +1 -1
  12. package/Clock/definition.d.ts +1 -0
  13. package/Fiber/FiberContext.d.ts +22 -19
  14. package/Fiber/api/interruptAll.d.ts +1 -1
  15. package/Fiber/api/interruptAsFork.d.ts +8 -0
  16. package/Fiber/definition.d.ts +6 -8
  17. package/FiberRef/api.d.ts +7 -2
  18. package/FiberRef/definition.d.ts +4 -3
  19. package/FiberRef/unsafe.d.ts +18 -1
  20. package/FiberRefs/api.d.ts +5 -0
  21. package/FiberRefs/definition.d.ts +1 -1
  22. package/FiberState/definition.d.ts +2 -2
  23. package/FiberStatus/definition.d.ts +1 -1
  24. package/Future/definition.d.ts +1 -1
  25. package/Hub/definition.d.ts +1 -1
  26. package/IO/api/core-scope.d.ts +4 -3
  27. package/IO/api/interrupt.d.ts +2 -2
  28. package/IO/api/supervised.d.ts +10 -0
  29. package/IO/api.d.ts +7 -10
  30. package/IO/definition.d.ts +16 -139
  31. package/IO.d.ts +1 -0
  32. package/IOEnv/definition.d.ts +1 -1
  33. package/Layer/definition.d.ts +2 -2
  34. package/Ref/definition.d.ts +3 -3
  35. package/Reloadable/api.d.ts +21 -0
  36. package/Reloadable/constructors.d.ts +23 -0
  37. package/Reloadable/definition.d.ts +20 -0
  38. package/Reloadable.d.ts +3 -0
  39. package/RuntimeConfig.d.ts +1 -1
  40. package/STM/definition.d.ts +5 -5
  41. package/STM/internal/CommitState.d.ts +1 -1
  42. package/STM/internal/Entry.d.ts +2 -2
  43. package/STM/internal/Journal.d.ts +6 -6
  44. package/STM/internal/TryCommit.d.ts +2 -2
  45. package/STM/internal/Versioned.d.ts +1 -1
  46. package/Schedule/Decision.d.ts +1 -1
  47. package/Scope/ReleaseMap/definition.d.ts +1 -1
  48. package/Scope/definition.d.ts +2 -2
  49. package/ScopedRef/definition.d.ts +1 -1
  50. package/State/internal.d.ts +1 -1
  51. package/Stream/api.d.ts +1 -1
  52. package/Stream/definition.d.ts +1 -1
  53. package/Stream/internal/DebounceState.d.ts +4 -4
  54. package/Stream/internal/Handoff.d.ts +5 -5
  55. package/Stream/internal/Pull.d.ts +1 -1
  56. package/Stream/internal/SinkEndReason.d.ts +1 -1
  57. package/TExit/definition.d.ts +3 -3
  58. package/TReentrantLock/definition.d.ts +1 -1
  59. package/TRef/definition.d.ts +1 -1
  60. package/TxnId.d.ts +1 -1
  61. package/_cjs/Clock/definition.cjs +2 -1
  62. package/_cjs/Clock/definition.cjs.map +1 -1
  63. package/_cjs/Console/definition.cjs +1 -1
  64. package/_cjs/Console/definition.cjs.map +1 -1
  65. package/_cjs/Fiber/FiberContext.cjs +180 -310
  66. package/_cjs/Fiber/FiberContext.cjs.map +1 -1
  67. package/_cjs/Fiber/api/collectAll.cjs +2 -2
  68. package/_cjs/Fiber/api/collectAll.cjs.map +1 -1
  69. package/_cjs/Fiber/api/interrupt.cjs +4 -2
  70. package/_cjs/Fiber/api/interrupt.cjs.map +1 -1
  71. package/_cjs/Fiber/api/interruptAll.cjs +8 -8
  72. package/_cjs/Fiber/api/interruptAll.cjs.map +1 -1
  73. package/_cjs/Fiber/api/interruptAs.cjs +7 -1
  74. package/_cjs/Fiber/api/interruptAs.cjs.map +1 -1
  75. package/_cjs/Fiber/api/interruptAsFork.cjs +15 -0
  76. package/_cjs/Fiber/api/interruptAsFork.cjs.map +1 -0
  77. package/_cjs/Fiber/api/interruptFork.cjs +5 -5
  78. package/_cjs/Fiber/api/interruptFork.cjs.map +1 -1
  79. package/_cjs/Fiber/definition.cjs +2 -2
  80. package/_cjs/Fiber/definition.cjs.map +1 -1
  81. package/_cjs/FiberRef/api.cjs +29 -10
  82. package/_cjs/FiberRef/api.cjs.map +1 -1
  83. package/_cjs/FiberRef/definition.cjs +2 -1
  84. package/_cjs/FiberRef/definition.cjs.map +1 -1
  85. package/_cjs/FiberRef/unsafe.cjs +29 -5
  86. package/_cjs/FiberRef/unsafe.cjs.map +1 -1
  87. package/_cjs/FiberRefs/api.cjs +28 -2
  88. package/_cjs/FiberRefs/api.cjs.map +1 -1
  89. package/_cjs/FiberRefs/join.cjs +1 -1
  90. package/_cjs/FiberRefs/join.cjs.map +1 -1
  91. package/_cjs/IO/api/core-scope.cjs +23 -15
  92. package/_cjs/IO/api/core-scope.cjs.map +1 -1
  93. package/_cjs/IO/api/ensuringChildren.cjs +4 -2
  94. package/_cjs/IO/api/ensuringChildren.cjs.map +1 -1
  95. package/_cjs/IO/api/interrupt.cjs +21 -11
  96. package/_cjs/IO/api/interrupt.cjs.map +1 -1
  97. package/_cjs/IO/api/supervised.cjs +26 -0
  98. package/_cjs/IO/api/supervised.cjs.map +1 -0
  99. package/_cjs/IO/api/withChildren.cjs +4 -2
  100. package/_cjs/IO/api/withChildren.cjs.map +1 -1
  101. package/_cjs/IO/api/zipC.cjs +10 -8
  102. package/_cjs/IO/api/zipC.cjs.map +1 -1
  103. package/_cjs/IO/api.cjs +67 -63
  104. package/_cjs/IO/api.cjs.map +1 -1
  105. package/_cjs/IO/definition.cjs +8 -226
  106. package/_cjs/IO/definition.cjs.map +1 -1
  107. package/_cjs/IO/runtime.cjs +7 -9
  108. package/_cjs/IO/runtime.cjs.map +1 -1
  109. package/_cjs/IO.cjs +13 -0
  110. package/_cjs/IO.cjs.map +1 -1
  111. package/_cjs/Layer/api.cjs +1 -1
  112. package/_cjs/Layer/api.cjs.map +1 -1
  113. package/_cjs/Random/definition.cjs +1 -1
  114. package/_cjs/Random/definition.cjs.map +1 -1
  115. package/_cjs/Reloadable/api.cjs +46 -0
  116. package/_cjs/Reloadable/api.cjs.map +1 -0
  117. package/_cjs/Reloadable/constructors.cjs +82 -0
  118. package/_cjs/Reloadable/constructors.cjs.map +1 -0
  119. package/_cjs/Reloadable/definition.cjs +58 -0
  120. package/_cjs/Reloadable/definition.cjs.map +1 -0
  121. package/_cjs/Reloadable.cjs +45 -0
  122. package/_cjs/Reloadable.cjs.map +1 -0
  123. package/_cjs/Scope/definition.cjs +1 -1
  124. package/_cjs/Scope/definition.cjs.map +1 -1
  125. package/_cjs/internal/IsFatal.cjs +129 -0
  126. package/_cjs/internal/IsFatal.cjs.map +1 -0
  127. package/_mjs/Clock/definition.mjs +2 -1
  128. package/_mjs/Clock/definition.mjs.map +1 -1
  129. package/_mjs/Console/definition.mjs +1 -1
  130. package/_mjs/Console/definition.mjs.map +1 -1
  131. package/_mjs/Fiber/FiberContext.mjs +180 -309
  132. package/_mjs/Fiber/FiberContext.mjs.map +1 -1
  133. package/_mjs/Fiber/api/collectAll.mjs +2 -2
  134. package/_mjs/Fiber/api/collectAll.mjs.map +1 -1
  135. package/_mjs/Fiber/api/interrupt.mjs +3 -2
  136. package/_mjs/Fiber/api/interrupt.mjs.map +1 -1
  137. package/_mjs/Fiber/api/interruptAll.mjs +6 -6
  138. package/_mjs/Fiber/api/interruptAll.mjs.map +1 -1
  139. package/_mjs/Fiber/api/interruptAs.mjs +3 -1
  140. package/_mjs/Fiber/api/interruptAs.mjs.map +1 -1
  141. package/_mjs/Fiber/api/interruptAsFork.mjs +8 -0
  142. package/_mjs/Fiber/api/interruptAsFork.mjs.map +1 -0
  143. package/_mjs/Fiber/api/interruptFork.mjs +5 -4
  144. package/_mjs/Fiber/api/interruptFork.mjs.map +1 -1
  145. package/_mjs/Fiber/definition.mjs +2 -2
  146. package/_mjs/Fiber/definition.mjs.map +1 -1
  147. package/_mjs/FiberRef/api.mjs +26 -9
  148. package/_mjs/FiberRef/api.mjs.map +1 -1
  149. package/_mjs/FiberRef/definition.mjs +2 -1
  150. package/_mjs/FiberRef/definition.mjs.map +1 -1
  151. package/_mjs/FiberRef/unsafe.mjs +23 -4
  152. package/_mjs/FiberRef/unsafe.mjs.map +1 -1
  153. package/_mjs/FiberRefs/api.mjs +22 -1
  154. package/_mjs/FiberRefs/api.mjs.map +1 -1
  155. package/_mjs/FiberRefs/join.mjs +1 -1
  156. package/_mjs/FiberRefs/join.mjs.map +1 -1
  157. package/_mjs/IO/api/core-scope.mjs +21 -16
  158. package/_mjs/IO/api/core-scope.mjs.map +1 -1
  159. package/_mjs/IO/api/ensuringChildren.mjs +3 -2
  160. package/_mjs/IO/api/ensuringChildren.mjs.map +1 -1
  161. package/_mjs/IO/api/interrupt.mjs +18 -9
  162. package/_mjs/IO/api/interrupt.mjs.map +1 -1
  163. package/_mjs/IO/api/supervised.mjs +14 -0
  164. package/_mjs/IO/api/supervised.mjs.map +1 -0
  165. package/_mjs/IO/api/withChildren.mjs +3 -2
  166. package/_mjs/IO/api/withChildren.mjs.map +1 -1
  167. package/_mjs/IO/api/zipC.mjs +9 -8
  168. package/_mjs/IO/api/zipC.mjs.map +1 -1
  169. package/_mjs/IO/api.mjs +66 -60
  170. package/_mjs/IO/api.mjs.map +1 -1
  171. package/_mjs/IO/definition.mjs +5 -184
  172. package/_mjs/IO/definition.mjs.map +1 -1
  173. package/_mjs/IO/runtime.mjs +7 -8
  174. package/_mjs/IO/runtime.mjs.map +1 -1
  175. package/_mjs/IO.mjs +1 -0
  176. package/_mjs/IO.mjs.map +1 -1
  177. package/_mjs/Layer/api.mjs +1 -1
  178. package/_mjs/Layer/api.mjs.map +1 -1
  179. package/_mjs/Random/definition.mjs +1 -1
  180. package/_mjs/Random/definition.mjs.map +1 -1
  181. package/_mjs/Reloadable/api.mjs +30 -0
  182. package/_mjs/Reloadable/api.mjs.map +1 -0
  183. package/_mjs/Reloadable/constructors.mjs +55 -0
  184. package/_mjs/Reloadable/constructors.mjs.map +1 -0
  185. package/_mjs/Reloadable/definition.mjs +40 -0
  186. package/_mjs/Reloadable/definition.mjs.map +1 -0
  187. package/_mjs/Reloadable.mjs +5 -0
  188. package/_mjs/Reloadable.mjs.map +1 -0
  189. package/_mjs/Scope/definition.mjs +1 -1
  190. package/_mjs/Scope/definition.mjs.map +1 -1
  191. package/_mjs/internal/IsFatal.mjs +104 -0
  192. package/_mjs/internal/IsFatal.mjs.map +1 -0
  193. package/_src/Clock/definition.ts +1 -0
  194. package/_src/Fiber/FiberContext.ts +50 -135
  195. package/_src/Fiber/api/collectAll.ts +1 -4
  196. package/_src/Fiber/api/interruptAll.ts +2 -2
  197. package/_src/Fiber/api/interruptAs.ts +1 -1
  198. package/_src/Fiber/api/interruptAsFork.ts +7 -0
  199. package/_src/Fiber/api/interruptFork.ts +1 -1
  200. package/_src/Fiber/definition.ts +3 -5
  201. package/_src/FiberRef/api.ts +30 -7
  202. package/_src/FiberRef/definition.ts +1 -0
  203. package/_src/FiberRef/unsafe.ts +27 -3
  204. package/_src/FiberRefs/api.ts +16 -0
  205. package/_src/FiberRefs/join.ts +1 -1
  206. package/_src/IO/api/core-scope.ts +12 -10
  207. package/_src/IO/api/interrupt.ts +8 -3
  208. package/_src/IO/api/supervised.ts +14 -0
  209. package/_src/IO/api.ts +25 -26
  210. package/_src/IO/definition.ts +8 -158
  211. package/_src/IO/runtime.ts +7 -11
  212. package/_src/IO.ts +1 -0
  213. package/_src/Reloadable/api.ts +28 -0
  214. package/_src/Reloadable/constructors.ts +60 -0
  215. package/_src/Reloadable/definition.ts +34 -0
  216. package/_src/Reloadable.ts +5 -0
  217. package/_src/internal/IsFatal.ts +79 -0
  218. package/internal/IsFatal.d.ts +47 -0
  219. package/package.json +4 -4
@@ -5,6 +5,7 @@ import { identity } from "@fncts/base/data/function";
5
5
  import { Patch } from "@fncts/base/data/Patch";
6
6
  import { defaultScheduler } from "@fncts/io/internal/Scheduler";
7
7
 
8
+ import { IsFatal } from "../internal/IsFatal.js";
8
9
  import { FiberRefInternal } from "./definition.js";
9
10
 
10
11
  /**
@@ -16,8 +17,9 @@ export function unsafeMakePatch<Value, Patch>(
16
17
  combine: (first: Patch, second: Patch) => Patch,
17
18
  patch: (patch: Patch) => (oldValue: Value) => Value,
18
19
  fork: Patch,
20
+ join: (oldValue: Value, newValue: Value) => Value = (_, newValue) => newValue,
19
21
  ): FiberRef.WithPatch<Value, Patch> {
20
- return new FiberRefInternal(initial, diff, combine, patch, fork);
22
+ return new FiberRefInternal(initial, diff, combine, patch, fork, join);
21
23
  }
22
24
 
23
25
  /**
@@ -45,15 +47,20 @@ export function unsafeMake<A>(
45
47
  initial,
46
48
  (_, newValue) => () => newValue,
47
49
  (first, second) => (value) => second(first(value)),
48
- (patch) => (value) => join(value, patch(value)),
50
+ (patch) => (value) => patch(value),
49
51
  fork,
52
+ join,
50
53
  );
51
54
  }
52
55
 
53
56
  /**
54
57
  * @tsplus static fncts.io.FiberRefOps forkScopeOverride
55
58
  */
56
- export const forkScopeOverride = FiberRef.unsafeMake<Maybe<FiberScope>>(Nothing(), () => Nothing());
59
+ export const forkScopeOverride = FiberRef.unsafeMake<Maybe<FiberScope>>(
60
+ Nothing(),
61
+ () => Nothing(),
62
+ (parent, _) => parent,
63
+ );
57
64
 
58
65
  /**
59
66
  * @tsplus static fncts.io.FiberRefOps currentEnvironment
@@ -84,3 +91,20 @@ export const currentLogAnnotations = FiberRef.unsafeMake<HashMap<string, string>
84
91
  * @tsplus static fncts.io.FiberRefOps currentScheduler
85
92
  */
86
93
  export const currentScheduler = FiberRef.unsafeMake<Scheduler>(defaultScheduler);
94
+
95
+ /**
96
+ * @tsplus static fncts.io.FiberRefOps currentSupervisor
97
+ */
98
+ export const currentSupervisor = FiberRef.unsafeMake<Supervisor<any>>(Supervisor.none);
99
+
100
+ /**
101
+ * @tsplus static fncts.io.FiberRefOps currentIsFatal
102
+ */
103
+ export const currentIsFatal = FiberRef.unsafeMake<IsFatal>(IsFatal.empty);
104
+
105
+ /**
106
+ * @tsplus static fncts.io.FiberRefOps currentReportFatal
107
+ */
108
+ export const currentReportFatal = FiberRef.unsafeMake<(t: unknown) => never>((t) => {
109
+ throw t;
110
+ });
@@ -13,3 +13,19 @@ export function fiberRefs(self: FiberRefs): HashSet<FiberRef<unknown>> {
13
13
  export function make(fiberRefLocals: HashMap<FiberRef<unknown>, Cons<readonly [FiberId.Runtime, unknown]>>): FiberRefs {
14
14
  return new FiberRefs(fiberRefLocals);
15
15
  }
16
+
17
+ /**
18
+ * @tsplus fluent fncts.io.FiberRefs forkAs
19
+ */
20
+ export function forkAs(self: FiberRefs, childId: FiberId.Runtime): FiberRefs {
21
+ const childFiberRefLocals = self.fiberRefLocals.mapWithIndex((fiberRef, stack) => {
22
+ const oldValue = stack.head[1];
23
+ const newValue = fiberRef.patch(fiberRef.fork)(oldValue);
24
+ if (oldValue === newValue) {
25
+ return stack;
26
+ } else {
27
+ return Cons([childId, newValue], stack);
28
+ }
29
+ });
30
+ return FiberRefs(childFiberRefLocals);
31
+ }
@@ -25,7 +25,7 @@ export function join(self: FiberRefs, fiberId: FiberId.Runtime, that: FiberRefs)
25
25
  const patch = ref.diff(ancestor, childValue);
26
26
 
27
27
  const oldValue = parentStack.head[1];
28
- const newValue = ref.patch(patch)(oldValue);
28
+ const newValue = ref.join(oldValue, ref.patch(patch)(oldValue));
29
29
 
30
30
  if (oldValue === newValue) return parentFiberRefs;
31
31
 
@@ -1,13 +1,13 @@
1
1
  import { ExitTag } from "@fncts/base/data/Exit";
2
2
 
3
3
  import { FiberScope } from "../../FiberScope.js";
4
- import { Fork, GetForkScope, IO, OverrideForkScope, Race } from "../definition.js";
4
+ import { Fork, IO, Race } from "../definition.js";
5
5
 
6
6
  /**
7
7
  * @tsplus getter fncts.io.IO daemonChildren
8
8
  */
9
9
  export function daemonChildren<R, E, A>(self: IO<R, E, A>, __tsplusTrace?: string): IO<R, E, A> {
10
- return IO.defer(new OverrideForkScope(self, Just(FiberScope.global), __tsplusTrace));
10
+ return IO.defer(FiberRef.forkScopeOverride.locally(Just(FiberScope.global))(self));
11
11
  }
12
12
 
13
13
  /**
@@ -15,7 +15,9 @@ export function daemonChildren<R, E, A>(self: IO<R, E, A>, __tsplusTrace?: strin
15
15
  *
16
16
  * @tsplus static fncts.io.IOOps forkScope
17
17
  */
18
- export const forkScope: UIO<FiberScope> = new GetForkScope(IO.succeedNow);
18
+ export const forkScope: UIO<FiberScope> = IO.withFiberContext((fiber) =>
19
+ IO(fiber.unsafeGetRef(FiberRef.forkScopeOverride).getOrElse(fiber.scope)),
20
+ );
19
21
 
20
22
  /**
21
23
  * Retrieves the scope that will be used to supervise forked effects.
@@ -23,14 +25,14 @@ export const forkScope: UIO<FiberScope> = new GetForkScope(IO.succeedNow);
23
25
  * @tsplus static fncts.io.IOOps forkScopeWith
24
26
  */
25
27
  export function forkScopeWith<R, E, A>(f: (_: FiberScope) => IO<R, E, A>, __tsplusTrace?: string) {
26
- return new GetForkScope(f, __tsplusTrace);
28
+ return forkScope.flatMap(f);
27
29
  }
28
30
 
29
31
  export class ForkScopeRestore {
30
32
  constructor(private scope: FiberScope) {}
31
33
 
32
34
  readonly restore = <R, E, A>(ma: IO<R, E, A>, __tsplusTrace?: string): IO<R, E, A> =>
33
- new OverrideForkScope(ma, Just(this.scope), __tsplusTrace);
35
+ FiberRef.forkScopeOverride.locally(Just(this.scope))(ma);
34
36
  }
35
37
 
36
38
  /**
@@ -45,8 +47,8 @@ export function forkScopeMask_<R, E, A>(
45
47
  f: (restore: ForkScopeRestore) => IO<R, E, A>,
46
48
  __tsplusTrace?: string,
47
49
  ): IO<R, E, A> {
48
- return IO.forkScopeWith(
49
- (scope) => new OverrideForkScope(f(new ForkScopeRestore(scope)), Just(newScope), __tsplusTrace),
50
+ return IO.forkScopeWith((scope) =>
51
+ FiberRef.forkScopeOverride.locally(Just(newScope))(f(new ForkScopeRestore(scope))),
50
52
  );
51
53
  }
52
54
 
@@ -103,7 +105,7 @@ export type Grafter = <R, E, A>(effect: IO<R, E, A>) => IO<R, E, A>;
103
105
  * @tsplus static fncts.io.IOOps transplant
104
106
  */
105
107
  export function transplant<R, E, A>(f: (_: Grafter) => IO<R, E, A>, __tsplusTrace?: string): IO<R, E, A> {
106
- return forkScopeWith((scope) => f((e) => new OverrideForkScope(e, Just(scope))));
108
+ return forkScopeWith((scope) => f((e) => FiberRef.forkScopeOverride.locally(Just(scope))(e)));
107
109
  }
108
110
 
109
111
  /**
@@ -124,7 +126,7 @@ export function forkDaemon<R, E, A>(ma: IO<R, E, A>, __tsplusTrace?: string): UR
124
126
  * @tsplus fluent fncts.io.IO overrideForkScope
125
127
  */
126
128
  export function overrideForkScope_<R, E, A>(ma: IO<R, E, A>, scope: FiberScope, __tsplusTrace?: string): IO<R, E, A> {
127
- return new OverrideForkScope(ma, Just(scope), __tsplusTrace);
129
+ return FiberRef.forkScopeOverride.locally(Just(scope))(ma);
128
130
  }
129
131
 
130
132
  /**
@@ -134,5 +136,5 @@ export function overrideForkScope_<R, E, A>(ma: IO<R, E, A>, scope: FiberScope,
134
136
  * @tsplus getter fncts.io.IO defaultForkScope
135
137
  */
136
138
  export function defaultForkScope<R, E, A>(ma: IO<R, E, A>, __tsplusTrace?: string): IO<R, E, A> {
137
- return new OverrideForkScope(ma, Nothing(), __tsplusTrace);
139
+ return FiberRef.forkScopeOverride.locally(Nothing())(ma);
138
140
  }
@@ -1,5 +1,3 @@
1
- import { SetInterrupt } from "../definition.js";
2
-
3
1
  /**
4
2
  * Returns an effect that is interrupted as if by the specified fiber.
5
3
  *
@@ -30,7 +28,14 @@ export function setInterruptStatus_<R, E, A>(
30
28
  flag: InterruptStatus,
31
29
  __tsplusTrace?: string,
32
30
  ): IO<R, E, A> {
33
- return new SetInterrupt(self, flag, __tsplusTrace);
31
+ return IO.withFiberContext((fiber) => {
32
+ const b = flag.toBoolean;
33
+ if (fiber.unsafeIsInterruptible !== b) {
34
+ fiber.interruptStatus.push(b);
35
+ fiber.unsafeRestoreInterruptStatus();
36
+ }
37
+ return self;
38
+ });
34
39
  }
35
40
 
36
41
  /**
@@ -0,0 +1,14 @@
1
+ /**
2
+ *
3
+ * Returns an IO with the behavior of this one, but where all child
4
+ * fibers forked in the effect are reported to the specified supervisor.
5
+ *
6
+ * @tsplus fluent fncts.io.IO supervised
7
+ */
8
+ export function supervised_<R, E, A>(
9
+ fa: IO<R, E, A>,
10
+ supervisor: Supervisor<any>,
11
+ __tsplusTrace?: string,
12
+ ): IO<R, E, A> {
13
+ return FiberRef.currentSupervisor.locally(supervisor)(fa);
14
+ }
package/_src/IO/api.ts CHANGED
@@ -1,28 +1,24 @@
1
+ import type { FiberStatus } from "../FiberStatus.js";
1
2
  import type * as P from "@fncts/base/typeclass";
2
3
  import type { _E, _R } from "@fncts/base/types";
3
4
  import type { FiberContext } from "@fncts/io/Fiber/FiberContext";
4
5
  import type { Canceler } from "@fncts/io/IO/definition";
5
6
 
6
7
  import { identity, pipe, tuple } from "@fncts/base/data/function";
8
+ import { Stateful } from "@fncts/io/IO/definition";
7
9
  import {
8
10
  Async,
9
11
  Chain,
10
12
  Defer,
11
13
  DeferWith,
12
- Ensuring,
13
14
  Fail,
14
- FiberRefModifyAll,
15
15
  Fork,
16
- GetDescriptor,
17
- GetInterrupt,
18
16
  IO,
19
17
  IOError,
20
18
  Logged,
21
19
  Match,
22
- SetRuntimeConfig,
23
20
  Succeed,
24
21
  SucceedNow,
25
- Supervise,
26
22
  Yield,
27
23
  } from "@fncts/io/IO/definition";
28
24
 
@@ -358,7 +354,7 @@ export function checkInterruptible<R, E, A>(
358
354
  f: (i: InterruptStatus) => IO<R, E, A>,
359
355
  __tsplusTrace?: string,
360
356
  ): IO<R, E, A> {
361
- return new GetInterrupt(f, __tsplusTrace);
357
+ return IO.withFiberContext((fiber) => f(InterruptStatus.fromBoolean(fiber.unsafeIsInterruptible)));
362
358
  }
363
359
 
364
360
  /**
@@ -515,7 +511,7 @@ export const descriptor = descriptorWith(IO.succeedNow);
515
511
  * @tsplus static fncts.io.IOOps descriptorWith
516
512
  */
517
513
  export function descriptorWith<R, E, A>(f: (d: FiberDescriptor) => IO<R, E, A>, __tsplusTrace?: string): IO<R, E, A> {
518
- return new GetDescriptor(f, __tsplusTrace);
514
+ return IO.withFiberContext((fiber) => f(fiber.unsafeGetDescriptor()));
519
515
  }
520
516
 
521
517
  /**
@@ -535,7 +531,10 @@ export function ensuring_<R, E, A, R1>(
535
531
  finalizer: IO<R1, never, any>,
536
532
  __tsplusTrace?: string,
537
533
  ): IO<R | R1, E, A> {
538
- return new Ensuring(self, finalizer, __tsplusTrace);
534
+ return IO.withFiberContext((fiber) => {
535
+ fiber.unsafeAddFinalizer(finalizer);
536
+ return self;
537
+ });
539
538
  }
540
539
 
541
540
  /**
@@ -1801,7 +1800,7 @@ export function sandboxWith_<R, E, A, E1>(
1801
1800
  * @tsplus static fncts.io.IOOps setRuntimeConfig
1802
1801
  */
1803
1802
  export function setRuntimeConfig(runtimeConfig: Lazy<RuntimeConfig>, __tsplusTrace?: string): UIO<void> {
1804
- return IO.defer(new SetRuntimeConfig(runtimeConfig(), __tsplusTrace));
1803
+ return IO.withFiberContext((fiber) => IO((fiber.runtimeConfig = runtimeConfig())));
1805
1804
  }
1806
1805
 
1807
1806
  /**
@@ -1839,21 +1838,6 @@ export function succeed<A>(effect: Lazy<A>, __tsplusTrace?: string): UIO<A> {
1839
1838
  return new Succeed(effect, __tsplusTrace);
1840
1839
  }
1841
1840
 
1842
- /**
1843
- *
1844
- * Returns an IO with the behavior of this one, but where all child
1845
- * fibers forked in the effect are reported to the specified supervisor.
1846
- *
1847
- * @tsplus fluent fncts.io.IO supervised
1848
- */
1849
- export function supervised_<R, E, A>(
1850
- fa: IO<R, E, A>,
1851
- supervisor: Supervisor<any>,
1852
- __tsplusTrace?: string,
1853
- ): IO<R, E, A> {
1854
- return new Supervise(fa, supervisor);
1855
- }
1856
-
1857
1841
  /**
1858
1842
  * @tsplus fluent fncts.io.IO summarized
1859
1843
  */
@@ -2051,7 +2035,22 @@ export function updateFiberRefs(
2051
2035
  f: (fiberId: FiberId.Runtime, fiberRefs: FiberRefs) => FiberRefs,
2052
2036
  __tsplusTrace?: string,
2053
2037
  ): UIO<void> {
2054
- return new FiberRefModifyAll((fiberId, fiberRefs) => [undefined, f(fiberId, fiberRefs)], __tsplusTrace);
2038
+ return IO.withFiberContext((fiber) =>
2039
+ IO(() => {
2040
+ const newFiberRefs = f(fiber.fiberId, fiber.fiberRefs);
2041
+ fiber.fiberRefLocals = newFiberRefs.fiberRefLocals;
2042
+ }),
2043
+ );
2044
+ }
2045
+
2046
+ /**
2047
+ * @tsplus static fncts.io.IOOps withFiberContext
2048
+ */
2049
+ export function withFiberContext<R, E, A>(
2050
+ onState: (fiber: FiberContext<E, A>, status: FiberStatus) => IO<R, E, A>,
2051
+ __tsplusTrace?: string,
2052
+ ): IO<R, E, A> {
2053
+ return new Stateful(onState, __tsplusTrace);
2055
2054
  }
2056
2055
 
2057
2056
  /**
@@ -1,3 +1,4 @@
1
+ import type { FiberStatus } from "../FiberStatus.js";
1
2
  import type { Trace as Trace_ } from "@fncts/base/data/Trace";
2
3
  import type { FiberContext } from "@fncts/io/Fiber/FiberContext";
3
4
 
@@ -7,7 +8,7 @@ export type IOTypeId = typeof IOTypeId;
7
8
  export interface IOF extends HKT {
8
9
  type: IO<this["R"], this["E"], this["A"]>;
9
10
  variance: {
10
- R: "-";
11
+ R: "+";
11
12
  E: "+";
12
13
  A: "+";
13
14
  };
@@ -66,22 +67,9 @@ export const enum IOTag {
66
67
  Fail = "Fail",
67
68
  Yield = "Yield",
68
69
  Race = "Race",
69
- SetInterrupt = "SetInterrupt",
70
- GetInterrupt = "GetInterrupt",
71
- GetDescriptor = "GetDescriptor",
72
- Supervise = "Supervise",
73
- FiberRefModify = "FiberRefModify",
74
- FiberRefLocally = "FiberRefLocally",
75
- FiberRefDelete = "FiberRefDelete",
76
- FiberRefWith = "FiberRefWith",
77
- FiberRefModifyAll = "FiberRefModifyAll",
78
- GetForkScope = "GetForkScope",
79
- OverrideForkScope = "OverrideForkScope",
80
70
  Trace = "Trace",
81
- GetRuntimeConfig = "GetRuntimeConfig",
82
- Ensuring = "Ensuring",
83
71
  Logged = "Logged",
84
- SetRuntimeConfig = "SetRuntimeConfig",
72
+ Stateful = "Stateful",
85
73
  }
86
74
 
87
75
  export function isIO(u: unknown): u is IO<any, any, any> {
@@ -236,134 +224,16 @@ export class Race<R, E, A, R1, E1, A1, R2, E2, A2, R3, E3, A3> extends IO<R | R1
236
224
  }
237
225
  }
238
226
 
239
- /**
240
- * @internal
241
- */
242
- export class SetInterrupt<R, E, A> extends IO<R, E, A> {
243
- readonly _tag = IOTag.SetInterrupt;
244
-
245
- constructor(readonly io: IO<R, E, A>, readonly flag: InterruptStatus, readonly trace?: string) {
246
- super();
247
- }
248
- }
249
-
250
- /**
251
- * @internal
252
- */
253
- export class GetInterrupt<R, E, A> extends IO<R, E, A> {
254
- readonly _tag = IOTag.GetInterrupt;
255
-
256
- constructor(readonly f: (_: InterruptStatus) => IO<R, E, A>, readonly trace?: string) {
257
- super();
258
- }
259
- }
260
-
261
- /**
262
- * @internal
263
- */
264
- export class GetDescriptor<R, E, A> extends IO<R, E, A> {
265
- readonly _tag = IOTag.GetDescriptor;
266
-
267
- constructor(readonly f: (_: FiberDescriptor) => IO<R, E, A>, readonly trace?: string) {
268
- super();
269
- }
270
- }
271
-
272
- /**
273
- * @internal
274
- */
275
- export class Supervise<R, E, A> extends IO<R, E, A> {
276
- readonly _tag = IOTag.Supervise;
277
-
278
- constructor(readonly io: IO<R, E, A>, readonly supervisor: Supervisor<any>, readonly trace?: string) {
279
- super();
280
- }
281
- }
282
-
283
- /**
284
- * @internal
285
- */
286
- export class FiberRefModify<A, B> extends IO<never, never, B> {
287
- readonly _tag = IOTag.FiberRefModify;
288
-
289
- constructor(readonly fiberRef: FiberRef<unknown>, readonly f: (a: A) => readonly [B, A], readonly trace?: string) {
290
- super();
291
- }
292
- }
293
-
294
- export class FiberRefLocally<V, R, E, A> extends IO<R, E, A> {
295
- readonly _tag = IOTag.FiberRefLocally;
227
+ export class Stateful<R, E, A> extends IO<R, E, A> {
228
+ readonly _tag = IOTag.Stateful;
296
229
  constructor(
297
- readonly localValue: V,
298
- readonly fiberRef: FiberRef<V>,
299
- readonly io: IO<R, E, A>,
230
+ readonly onState: (fiber: FiberContext<E, A>, status: FiberStatus) => IO<R, E, A>,
300
231
  readonly trace?: string,
301
232
  ) {
302
233
  super();
303
234
  }
304
235
  }
305
236
 
306
- export class FiberRefDelete extends IO<never, never, void> {
307
- readonly _tag = IOTag.FiberRefDelete;
308
- constructor(readonly fiberRef: FiberRef<unknown>, readonly trace?: string) {
309
- super();
310
- }
311
- }
312
-
313
- export class FiberRefWith<A, P, R, E, B> extends IO<R, E, B> {
314
- readonly _tag = IOTag.FiberRefWith;
315
- constructor(readonly fiberRef: FiberRef<A>, readonly f: (a: A) => IO<R, E, B>, readonly trace?: string) {
316
- super();
317
- }
318
- }
319
-
320
- export class FiberRefModifyAll<A> extends IO<never, never, A> {
321
- readonly _tag = IOTag.FiberRefModifyAll;
322
- constructor(
323
- readonly f: (fiberId: FiberId.Runtime, fiberRefs: FiberRefs) => readonly [A, FiberRefs],
324
- readonly trace?: string,
325
- ) {
326
- super();
327
- }
328
- }
329
-
330
- /**
331
- * @internal
332
- */
333
- export class GetForkScope<R, E, A> extends IO<R, E, A> {
334
- readonly _tag = IOTag.GetForkScope;
335
-
336
- constructor(readonly f: (_: FiberScope) => IO<R, E, A>, readonly trace?: string) {
337
- super();
338
- }
339
- }
340
-
341
- /**
342
- * @internal
343
- */
344
- export class OverrideForkScope<R, E, A> extends IO<R, E, A> {
345
- readonly _tag = IOTag.OverrideForkScope;
346
-
347
- constructor(readonly io: IO<R, E, A>, readonly forkScope: Maybe<FiberScope>, readonly trace?: string) {
348
- super();
349
- }
350
- }
351
-
352
- export class GetRuntimeConfig<R, E, A> extends IO<R, E, A> {
353
- readonly _tag = IOTag.GetRuntimeConfig;
354
-
355
- constructor(readonly f: (_: RuntimeConfig) => IO<R, E, A>, readonly trace?: string) {
356
- super();
357
- }
358
- }
359
-
360
- export class Ensuring<R, E, A, R1> extends IO<R | R1, E, A> {
361
- readonly _tag = IOTag.Ensuring;
362
- constructor(readonly io: IO<R, E, A>, readonly finalizer: IO<R1, never, any>, readonly trace?: string) {
363
- super();
364
- }
365
- }
366
-
367
237
  export class Logged extends IO<never, never, void> {
368
238
  readonly _tag = IOTag.Logged;
369
239
  constructor(
@@ -378,13 +248,6 @@ export class Logged extends IO<never, never, void> {
378
248
  }
379
249
  }
380
250
 
381
- export class SetRuntimeConfig extends IO<never, never, void> {
382
- readonly _tag = IOTag.SetRuntimeConfig;
383
- constructor(readonly runtimeConfig: RuntimeConfig, readonly trace?: string) {
384
- super();
385
- }
386
- }
387
-
388
251
  export type Instruction =
389
252
  | Chain<any, any, any, any, any, any>
390
253
  | SucceedNow<any>
@@ -393,27 +256,14 @@ export type Instruction =
393
256
  | Async<any, any, any, any>
394
257
  | Match<any, any, any, any, any, any, any, any, any>
395
258
  | Fork<any, any, any>
396
- | SetInterrupt<any, any, any>
397
- | GetInterrupt<any, any, any>
398
259
  | Fail<any>
399
- | GetDescriptor<any, any, any>
400
260
  | Yield
401
261
  | Defer<any, any, any>
402
262
  | DeferWith<any, any, any>
403
- | FiberRefModify<any, any>
404
- | FiberRefLocally<any, any, any, any>
405
- | FiberRefDelete
406
- | FiberRefWith<any, any, any, any, any>
407
- | FiberRefModifyAll<any>
263
+ | Stateful<any, any, any>
408
264
  | Race<any, any, any, any, any, any, any, any, any, any, any, any>
409
- | Supervise<any, any, any>
410
- | GetForkScope<any, any, any>
411
- | OverrideForkScope<any, any, any>
412
265
  | Trace
413
- | GetRuntimeConfig<any, any, any>
414
- | Ensuring<any, any, any, any>
415
- | Logged
416
- | SetRuntimeConfig;
266
+ | Logged;
417
267
 
418
268
  /**
419
269
  * @tsplus macro identity
@@ -17,11 +17,9 @@ export class Runtime<R> {
17
17
  fiberId,
18
18
  this.runtimeConfig,
19
19
  Stack.single(InterruptStatus.interruptible.toBoolean),
20
- new AtomicReference(
21
- HashMap<FiberRef<unknown>, Cons<readonly [FiberId.Runtime, unknown]>>(
22
- [FiberRef.currentEnvironment, Cons([fiberId, this.environment])],
23
- [IOEnv.services, Cons([fiberId, IOEnv.environment])],
24
- ),
20
+ HashMap<FiberRef<unknown>, Cons<readonly [FiberId.Runtime, unknown]>>(
21
+ [FiberRef.currentEnvironment, Cons([fiberId, this.environment])],
22
+ [IOEnv.services, Cons([fiberId, IOEnv.environment])],
25
23
  ),
26
24
  children,
27
25
  );
@@ -71,12 +69,10 @@ export class Runtime<R> {
71
69
  fiberId,
72
70
  this.runtimeConfig,
73
71
  Stack.single(InterruptStatus.interruptible.toBoolean),
74
- new AtomicReference(
75
- HashMap<FiberRef<unknown>, Cons<readonly [FiberId.Runtime, unknown]>>(
76
- [FiberRef.currentEnvironment, Cons([fiberId, this.environment])],
77
- [IOEnv.services, Cons([fiberId, IOEnv.environment])],
78
- [FiberRef.currentScheduler, Cons([fiberId, scheduler])],
79
- ),
72
+ HashMap<FiberRef<unknown>, Cons<readonly [FiberId.Runtime, unknown]>>(
73
+ [FiberRef.currentEnvironment, Cons([fiberId, this.environment])],
74
+ [IOEnv.services, Cons([fiberId, IOEnv.environment])],
75
+ [FiberRef.currentScheduler, Cons([fiberId, scheduler])],
80
76
  ),
81
77
  children,
82
78
  );
package/_src/IO.ts CHANGED
@@ -46,6 +46,7 @@ export * from "./IO/api/scopeWith.js";
46
46
  export * from "./IO/api/sequenceT.js";
47
47
  export * from "./IO/api/sleep.js";
48
48
  export * from "./IO/api/stateful.js";
49
+ export * from "./IO/api/supervised.js";
49
50
  export * from "./IO/api/timeout.js";
50
51
  export * from "./IO/api/withChildren.js";
51
52
  export * from "./IO/api/withEarlyRelease.js";
@@ -0,0 +1,28 @@
1
+ import type { Reloadable } from "./definition.js";
2
+
3
+ /**
4
+ * @tsplus static fncts.io.ReloadableOps get
5
+ */
6
+ export function get<Service>(
7
+ /** @tsplus auto */ tag: Tag<Reloadable<Service>>,
8
+ ): IO<Reloadable<Service>, unknown, Service> {
9
+ return IO.serviceWithIO((service) => service.get, tag);
10
+ }
11
+
12
+ /**
13
+ * @tsplus static fncts.io.ReloadableOps reload
14
+ */
15
+ export function reload<Service>(
16
+ /** @tsplua auto */ tag: Tag<Reloadable<Service>>,
17
+ ): IO<Reloadable<Service>, unknown, void> {
18
+ return IO.serviceWithIO((service) => service.reload, tag);
19
+ }
20
+
21
+ /**
22
+ * @tsplus static fncts.io.ReloadableOps reloadFork
23
+ */
24
+ export function reloadFork<Service>(
25
+ /** @tsplus auto */ tag: Tag<Reloadable<Service>>,
26
+ ): IO<Reloadable<Service>, never, void> {
27
+ return IO.serviceWithIO((service) => service.reloadFork, tag);
28
+ }
@@ -0,0 +1,60 @@
1
+ import { Reloadable } from "./definition.js";
2
+
3
+ /**
4
+ * @tsplus static fncts.io.ReloadableOps auto
5
+ */
6
+ export function auto<In, E, Out, Env>(
7
+ layer: Layer<In, E, Out>,
8
+ tag: Tag<Out>,
9
+ schedule: Schedule<Env, any, any>,
10
+ /** @tsplus auto */ reloadableTag: Tag<Reloadable<Out>>
11
+ ): Layer<In | Env, E, Reloadable<Out>> {
12
+ return Layer.scoped(
13
+ Do((Δ) => {
14
+ const env = Δ(Reloadable.manual(layer, tag, reloadableTag).build);
15
+ const reloadable = env.unsafeGet(reloadableTag);
16
+ Δ(IO.acquireRelease(reloadable.reload.schedule(schedule).forkDaemon, (fiber) => fiber.interrupt));
17
+ return reloadable;
18
+ }),
19
+ reloadableTag,
20
+ );
21
+ }
22
+
23
+ /**
24
+ * @tsplus static fncts.io.ReloadableOps autoFromConfig
25
+ */
26
+ export function autoFromConfig<In, E, Out, Env>(
27
+ layer: Layer<In, E, Out>,
28
+ tag: Tag<Out>,
29
+ scheduleFromConfig: (env: Environment<In>) => Schedule<Env, any, any>,
30
+ /** @tsplus auto */ reloadableTag: Tag<Reloadable<Out>>
31
+ ): Layer<In | Env, E, Reloadable<Out>> {
32
+ return Layer.scoped(
33
+ Do((Δ) => {
34
+ const inp = Δ(IO.environment<In>());
35
+ const schedule = scheduleFromConfig(inp);
36
+ const env = Δ(Reloadable.auto(layer, tag, schedule).build);
37
+ return env.unsafeGet(reloadableTag);
38
+ }),
39
+ reloadableTag,
40
+ );
41
+ }
42
+
43
+ /**
44
+ * @tsplus static fncts.io.ReloadableOps manual
45
+ */
46
+ export function manual<In, E, Out>(
47
+ layer: Layer<In, E, Out>,
48
+ tag: Tag<Out>,
49
+ /** @tsplus auto */ reloadableTag: Tag<Reloadable<Out>>,
50
+ ): Layer<In, E, Reloadable<Out>> {
51
+ return Layer.scoped(
52
+ Do((Δ) => {
53
+ const inp = Δ(IO.environment<In>());
54
+ const ref = Δ(ScopedRef.fromAcquire(layer.build.map((env) => env.unsafeGet(tag))));
55
+ const reload = ref.set(layer.build.map((env) => env.unsafeGet(tag))).provideEnvironment(inp);
56
+ return new Reloadable(ref, reload);
57
+ }),
58
+ reloadableTag,
59
+ );
60
+ }