effect 3.10.19 → 3.11.0

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 (167) hide show
  1. package/dist/cjs/BigDecimal.js +125 -24
  2. package/dist/cjs/BigDecimal.js.map +1 -1
  3. package/dist/cjs/Channel.js +44 -4
  4. package/dist/cjs/Channel.js.map +1 -1
  5. package/dist/cjs/Config.js +8 -1
  6. package/dist/cjs/Config.js.map +1 -1
  7. package/dist/cjs/Context.js +26 -1
  8. package/dist/cjs/Context.js.map +1 -1
  9. package/dist/cjs/Cron.js +75 -67
  10. package/dist/cjs/Cron.js.map +1 -1
  11. package/dist/cjs/DateTime.js +114 -664
  12. package/dist/cjs/DateTime.js.map +1 -1
  13. package/dist/cjs/Effect.js +82 -4
  14. package/dist/cjs/Effect.js.map +1 -1
  15. package/dist/cjs/Inspectable.js +8 -4
  16. package/dist/cjs/Inspectable.js.map +1 -1
  17. package/dist/cjs/JSONSchema.js.map +1 -1
  18. package/dist/cjs/Micro.js +1099 -1072
  19. package/dist/cjs/Micro.js.map +1 -1
  20. package/dist/cjs/STM.js.map +1 -1
  21. package/dist/cjs/Schema.js +57 -8
  22. package/dist/cjs/Schema.js.map +1 -1
  23. package/dist/cjs/Sink.js +9 -1
  24. package/dist/cjs/Sink.js.map +1 -1
  25. package/dist/cjs/Stream.js +25 -7
  26. package/dist/cjs/Stream.js.map +1 -1
  27. package/dist/cjs/Utils.js +7 -1
  28. package/dist/cjs/Utils.js.map +1 -1
  29. package/dist/cjs/internal/channel/channelExecutor.js +5 -9
  30. package/dist/cjs/internal/channel/channelExecutor.js.map +1 -1
  31. package/dist/cjs/internal/channel.js +156 -130
  32. package/dist/cjs/internal/channel.js.map +1 -1
  33. package/dist/cjs/internal/config.js +13 -4
  34. package/dist/cjs/internal/config.js.map +1 -1
  35. package/dist/cjs/internal/context.js +46 -3
  36. package/dist/cjs/internal/context.js.map +1 -1
  37. package/dist/cjs/internal/dateTime.js +747 -0
  38. package/dist/cjs/internal/dateTime.js.map +1 -0
  39. package/dist/cjs/internal/fiberRuntime.js +34 -11
  40. package/dist/cjs/internal/fiberRuntime.js.map +1 -1
  41. package/dist/cjs/internal/groupBy.js +9 -3
  42. package/dist/cjs/internal/groupBy.js.map +1 -1
  43. package/dist/cjs/internal/layer.js +1 -1
  44. package/dist/cjs/internal/layer.js.map +1 -1
  45. package/dist/cjs/internal/mailbox.js +1 -1
  46. package/dist/cjs/internal/mailbox.js.map +1 -1
  47. package/dist/cjs/internal/sink.js +25 -21
  48. package/dist/cjs/internal/sink.js.map +1 -1
  49. package/dist/cjs/internal/stream.js +70 -71
  50. package/dist/cjs/internal/stream.js.map +1 -1
  51. package/dist/cjs/internal/version.js +1 -1
  52. package/dist/cjs/internal/version.js.map +1 -1
  53. package/dist/dts/BigDecimal.d.ts +56 -1
  54. package/dist/dts/BigDecimal.d.ts.map +1 -1
  55. package/dist/dts/Channel.d.ts +66 -5
  56. package/dist/dts/Channel.d.ts.map +1 -1
  57. package/dist/dts/Config.d.ts +23 -1
  58. package/dist/dts/Config.d.ts.map +1 -1
  59. package/dist/dts/Context.d.ts +111 -0
  60. package/dist/dts/Context.d.ts.map +1 -1
  61. package/dist/dts/Cron.d.ts +15 -6
  62. package/dist/dts/Cron.d.ts.map +1 -1
  63. package/dist/dts/DateTime.d.ts +40 -49
  64. package/dist/dts/DateTime.d.ts.map +1 -1
  65. package/dist/dts/Effect.d.ts +88 -1
  66. package/dist/dts/Effect.d.ts.map +1 -1
  67. package/dist/dts/Inspectable.d.ts.map +1 -1
  68. package/dist/dts/JSONSchema.d.ts +1 -0
  69. package/dist/dts/JSONSchema.d.ts.map +1 -1
  70. package/dist/dts/Micro.d.ts +875 -872
  71. package/dist/dts/Micro.d.ts.map +1 -1
  72. package/dist/dts/STM.d.ts +2 -0
  73. package/dist/dts/STM.d.ts.map +1 -1
  74. package/dist/dts/Schema.d.ts +32 -0
  75. package/dist/dts/Schema.d.ts.map +1 -1
  76. package/dist/dts/Sink.d.ts +8 -0
  77. package/dist/dts/Sink.d.ts.map +1 -1
  78. package/dist/dts/Stream.d.ts +50 -32
  79. package/dist/dts/Stream.d.ts.map +1 -1
  80. package/dist/dts/Utils.d.ts +4 -0
  81. package/dist/dts/Utils.d.ts.map +1 -1
  82. package/dist/dts/internal/context.d.ts +1 -1
  83. package/dist/dts/internal/context.d.ts.map +1 -1
  84. package/dist/dts/internal/dateTime.d.ts +2 -0
  85. package/dist/dts/internal/dateTime.d.ts.map +1 -0
  86. package/dist/dts/internal/fiberRuntime.d.ts.map +1 -1
  87. package/dist/dts/internal/stream.d.ts.map +1 -1
  88. package/dist/esm/BigDecimal.js +119 -20
  89. package/dist/esm/BigDecimal.js.map +1 -1
  90. package/dist/esm/Channel.js +42 -2
  91. package/dist/esm/Channel.js.map +1 -1
  92. package/dist/esm/Config.js +7 -0
  93. package/dist/esm/Config.js.map +1 -1
  94. package/dist/esm/Context.js +25 -0
  95. package/dist/esm/Context.js.map +1 -1
  96. package/dist/esm/Cron.js +75 -67
  97. package/dist/esm/Cron.js.map +1 -1
  98. package/dist/esm/DateTime.js +112 -627
  99. package/dist/esm/DateTime.js.map +1 -1
  100. package/dist/esm/Effect.js +77 -0
  101. package/dist/esm/Effect.js.map +1 -1
  102. package/dist/esm/Inspectable.js +8 -4
  103. package/dist/esm/Inspectable.js.map +1 -1
  104. package/dist/esm/JSONSchema.js.map +1 -1
  105. package/dist/esm/Micro.js +1077 -1047
  106. package/dist/esm/Micro.js.map +1 -1
  107. package/dist/esm/STM.js.map +1 -1
  108. package/dist/esm/Schema.js +54 -0
  109. package/dist/esm/Schema.js.map +1 -1
  110. package/dist/esm/Sink.js +8 -0
  111. package/dist/esm/Sink.js.map +1 -1
  112. package/dist/esm/Stream.js +23 -5
  113. package/dist/esm/Stream.js.map +1 -1
  114. package/dist/esm/Utils.js +5 -0
  115. package/dist/esm/Utils.js.map +1 -1
  116. package/dist/esm/internal/channel/channelExecutor.js +5 -7
  117. package/dist/esm/internal/channel/channelExecutor.js.map +1 -1
  118. package/dist/esm/internal/channel.js +152 -129
  119. package/dist/esm/internal/channel.js.map +1 -1
  120. package/dist/esm/internal/config.js +11 -3
  121. package/dist/esm/internal/config.js.map +1 -1
  122. package/dist/esm/internal/context.js +42 -2
  123. package/dist/esm/internal/context.js.map +1 -1
  124. package/dist/esm/internal/dateTime.js +704 -0
  125. package/dist/esm/internal/dateTime.js.map +1 -0
  126. package/dist/esm/internal/fiberRuntime.js +31 -9
  127. package/dist/esm/internal/fiberRuntime.js.map +1 -1
  128. package/dist/esm/internal/groupBy.js +9 -3
  129. package/dist/esm/internal/groupBy.js.map +1 -1
  130. package/dist/esm/internal/layer.js +1 -1
  131. package/dist/esm/internal/layer.js.map +1 -1
  132. package/dist/esm/internal/mailbox.js +1 -1
  133. package/dist/esm/internal/mailbox.js.map +1 -1
  134. package/dist/esm/internal/sink.js +23 -20
  135. package/dist/esm/internal/sink.js.map +1 -1
  136. package/dist/esm/internal/stream.js +66 -69
  137. package/dist/esm/internal/stream.js.map +1 -1
  138. package/dist/esm/internal/version.js +1 -1
  139. package/dist/esm/internal/version.js.map +1 -1
  140. package/package.json +1 -1
  141. package/src/BigDecimal.ts +131 -21
  142. package/src/Channel.ts +81 -5
  143. package/src/Config.ts +24 -1
  144. package/src/Context.ts +119 -0
  145. package/src/Cron.ts +85 -68
  146. package/src/DateTime.ts +155 -757
  147. package/src/Effect.ts +340 -1
  148. package/src/Inspectable.ts +11 -7
  149. package/src/JSONSchema.ts +1 -0
  150. package/src/Micro.ts +2005 -1757
  151. package/src/STM.ts +2 -0
  152. package/src/Schema.ts +60 -0
  153. package/src/Sink.ts +11 -0
  154. package/src/Stream.ts +55 -44
  155. package/src/Utils.ts +8 -0
  156. package/src/internal/channel/channelExecutor.ts +37 -33
  157. package/src/internal/channel.ts +504 -467
  158. package/src/internal/config.ts +18 -6
  159. package/src/internal/context.ts +56 -4
  160. package/src/internal/dateTime.ts +1126 -0
  161. package/src/internal/fiberRuntime.ts +35 -16
  162. package/src/internal/groupBy.ts +13 -22
  163. package/src/internal/layer.ts +5 -8
  164. package/src/internal/mailbox.ts +6 -4
  165. package/src/internal/sink.ts +55 -35
  166. package/src/internal/stream.ts +299 -299
  167. package/src/internal/version.ts +1 -1
@@ -620,6 +620,27 @@ export class FiberRuntime<in out A, in out E = never> extends Effectable.Class<A
620
620
  this.getChildren().delete(child)
621
621
  }
622
622
 
623
+ /**
624
+ * Transfers all children of this fiber that are currently running to the
625
+ * specified fiber scope.
626
+ *
627
+ * **NOTE**: This method must be invoked by the fiber itself after it has
628
+ * evaluated the effects but prior to exiting.
629
+ */
630
+ transferChildren(scope: fiberScope.FiberScope) {
631
+ const children = this._children
632
+ // Clear the children of the current fiber
633
+ this._children = null
634
+ if (children !== null && children.size > 0) {
635
+ for (const child of children) {
636
+ // If the child is still running, add it to the scope
637
+ if (child._exitValue === null) {
638
+ scope.add(this.currentRuntimeFlags, child)
639
+ }
640
+ }
641
+ }
642
+ }
643
+
623
644
  /**
624
645
  * On the current thread, executes all messages in the fiber's inbox. This
625
646
  * method may return before all work is done, in the event the fiber executes
@@ -1093,23 +1114,21 @@ export class FiberRuntime<in out A, in out E = never> extends Effectable.Class<A
1093
1114
 
1094
1115
  ["Micro"](op: Micro.Micro<any, any, never> & { _op: "Micro" }) {
1095
1116
  return core.unsafeAsync<any, any>((microResume) => {
1096
- const env = Micro.envUnsafeMakeEmpty().pipe(
1097
- Micro.envSet(Micro.currentContext, this.getFiberRef(core.currentContext))
1098
- )
1099
1117
  let resume = microResume
1100
- op[Micro.runSymbol](env, (result) => {
1101
- if (result._tag === "Right") {
1102
- return resume(core.exitSucceed(result.right))
1118
+ const fiber = Micro.runFork(Micro.provideContext(op, this.currentContext))
1119
+ fiber.addObserver((exit) => {
1120
+ if (exit._tag === "Success") {
1121
+ return resume(core.exitSucceed(exit.value))
1103
1122
  }
1104
- switch (result.left._tag) {
1123
+ switch (exit.cause._tag) {
1105
1124
  case "Interrupt": {
1106
1125
  return resume(core.exitFailCause(internalCause.interrupt(FiberId.none)))
1107
1126
  }
1108
1127
  case "Fail": {
1109
- return resume(core.fail(result.left.error))
1128
+ return resume(core.fail(exit.cause.error))
1110
1129
  }
1111
1130
  case "Die": {
1112
- return resume(core.die(result.left.defect))
1131
+ return resume(core.die(exit.cause.defect))
1113
1132
  }
1114
1133
  }
1115
1134
  })
@@ -1117,7 +1136,7 @@ export class FiberRuntime<in out A, in out E = never> extends Effectable.Class<A
1117
1136
  resume = (_: any) => {
1118
1137
  abortResume(core.void)
1119
1138
  }
1120
- Micro.envGet(env, Micro.currentAbortController).abort()
1139
+ fiber.unsafeInterrupt()
1121
1140
  })
1122
1141
  })
1123
1142
  }
@@ -2835,6 +2854,11 @@ export const scopeWith = <A, E, R>(
2835
2854
  f: (scope: Scope.Scope) => Effect.Effect<A, E, R>
2836
2855
  ): Effect.Effect<A, E, R | Scope.Scope> => core.flatMap(scopeTag, f)
2837
2856
 
2857
+ /** @internal */
2858
+ export const scopedWith = <A, E, R>(
2859
+ f: (scope: Scope.Scope) => Effect.Effect<A, E, R>
2860
+ ): Effect.Effect<A, E, R> => core.flatMap(scopeMake(), (scope) => core.onExit(f(scope), (exit) => scope.close(exit)))
2861
+
2838
2862
  /* @internal */
2839
2863
  export const scopedEffect = <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, Exclude<R, Scope.Scope>> =>
2840
2864
  core.flatMap(scopeMake(), (scope) => scopeUse(effect, scope))
@@ -2878,12 +2902,7 @@ export const using = dual<
2878
2902
  self: Effect.Effect<A, E, R>,
2879
2903
  use: (a: A) => Effect.Effect<A2, E2, R2>
2880
2904
  ) => Effect.Effect<A2, E | E2, Exclude<R, Scope.Scope> | R2>
2881
- >(2, (self, use) =>
2882
- core.acquireUseRelease(
2883
- scopeMake(),
2884
- (scope) => core.flatMap(scopeExtend(self, scope), use),
2885
- (scope, exit) => core.scopeClose(scope, exit)
2886
- ))
2905
+ >(2, (self, use) => scopedWith((scope) => core.flatMap(scopeExtend(self, scope), use)))
2887
2906
 
2888
2907
  /** @internal */
2889
2908
  export const validate = dual<
@@ -12,6 +12,7 @@ import { pipeArguments } from "../Pipeable.js"
12
12
  import { hasProperty, type Predicate } from "../Predicate.js"
13
13
  import * as Queue from "../Queue.js"
14
14
  import * as Ref from "../Ref.js"
15
+ import * as Scope from "../Scope.js"
15
16
  import type * as Stream from "../Stream.js"
16
17
  import type * as Take from "../Take.js"
17
18
  import type { NoInfer } from "../Types.js"
@@ -487,29 +488,19 @@ export const groupByKey = dual<
487
488
  )
488
489
  )
489
490
  })
490
- return make(stream.unwrapScoped(
491
- pipe(
492
- Effect.sync(() => new Map<K, Queue.Queue<Take.Take<A, E>>>()),
493
- Effect.flatMap((map) =>
494
- pipe(
495
- Effect.acquireRelease(
496
- Queue.unbounded<Take.Take<readonly [K, Queue.Queue<Take.Take<A, E>>], E>>(),
497
- (queue) => Queue.shutdown(queue)
498
- ),
499
- Effect.flatMap((queue) =>
500
- pipe(
501
- self,
502
- stream.toChannel,
503
- core.pipeTo(loop(map, queue)),
504
- channel.drain,
505
- channelExecutor.runScoped,
506
- Effect.forkScoped,
507
- Effect.as(stream.flattenTake(stream.fromQueue(queue, { shutdown: true })))
508
- )
509
- )
510
- )
491
+ return make(stream.unwrapScopedWith((scope) =>
492
+ Effect.gen(function*() {
493
+ const map = new Map<K, Queue.Queue<Take.Take<A, E>>>()
494
+ const queue = yield* Queue.unbounded<Take.Take<readonly [K, Queue.Queue<Take.Take<A, E>>], E>>()
495
+ yield* Scope.addFinalizer(scope, Queue.shutdown(queue))
496
+ return yield* stream.toChannel(self).pipe(
497
+ core.pipeTo(loop(map, queue)),
498
+ channel.drain,
499
+ channelExecutor.runIn(scope),
500
+ Effect.forkIn(scope),
501
+ Effect.as(stream.flattenTake(stream.fromQueue(queue, { shutdown: true })))
511
502
  )
512
- )
503
+ })
513
504
  ))
514
505
  }
515
506
  )
@@ -1274,14 +1274,11 @@ const provideSomeLayer = dual<
1274
1274
  layer: Layer.Layer<A2, E2, R2>
1275
1275
  ) => Effect.Effect<A, E | E2, R2 | Exclude<R, A2>>
1276
1276
  >(2, (self, layer) =>
1277
- core.acquireUseRelease(
1278
- fiberRuntime.scopeMake(),
1279
- (scope) =>
1280
- core.flatMap(
1281
- buildWithScope(layer, scope),
1282
- (context) => core.provideSomeContext(self, context)
1283
- ),
1284
- (scope, exit) => core.scopeClose(scope, exit)
1277
+ fiberRuntime.scopedWith((scope) =>
1278
+ core.flatMap(
1279
+ buildWithScope(layer, scope),
1280
+ (context) => core.provideSomeContext(self, context)
1281
+ )
1285
1282
  ))
1286
1283
 
1287
1284
  const provideSomeRuntime = dual<
@@ -547,10 +547,12 @@ export const fromStream: {
547
547
  onFailure: (cause: Cause<E>) => mailbox.failCause(cause),
548
548
  onDone: () => mailbox.end
549
549
  })
550
- return stream.toChannel(self).pipe(
551
- coreChannel.pipeTo(writer),
552
- channelExecutor.runScoped,
553
- circular.forkScoped
550
+ return channel.unwrapScopedWith((scope) =>
551
+ stream.toChannel(self).pipe(
552
+ coreChannel.pipeTo(writer),
553
+ channelExecutor.runIn(scope),
554
+ circular.forkIn(scope)
555
+ )
554
556
  )
555
557
  }
556
558
  ))
@@ -19,7 +19,7 @@ import { hasProperty, type Predicate, type Refinement } from "../Predicate.js"
19
19
  import * as PubSub from "../PubSub.js"
20
20
  import * as Queue from "../Queue.js"
21
21
  import * as Ref from "../Ref.js"
22
- import type * as Scope from "../Scope.js"
22
+ import * as Scope from "../Scope.js"
23
23
  import type * as Sink from "../Sink.js"
24
24
  import * as channel from "./channel.js"
25
25
  import * as mergeDecision from "./channel/mergeDecision.js"
@@ -1656,38 +1656,43 @@ export const raceWith = dual<
1656
1656
  readonly capacity?: number | undefined
1657
1657
  }
1658
1658
  ): Sink.Sink<A3 | A4, In & In2, L2 | L, E2 | E, R2 | R> => {
1659
- const scoped = Effect.gen(function*($) {
1660
- const pubsub = yield* $(
1661
- PubSub.bounded<Either.Either<Chunk.Chunk<In & In2>, Exit.Exit<unknown>>>(options?.capacity ?? 16)
1662
- )
1663
- const channel1 = yield* $(channel.fromPubSubScoped(pubsub))
1664
- const channel2 = yield* $(channel.fromPubSubScoped(pubsub))
1665
- const reader = channel.toPubSub(pubsub)
1666
- const writer = pipe(
1667
- channel1,
1668
- core.pipeTo(toChannel(self)),
1669
- channel.mergeWith({
1670
- other: pipe(channel2, core.pipeTo(toChannel(options.other))),
1671
- onSelfDone: options.onSelfDone,
1672
- onOtherDone: options.onOtherDone
1673
- })
1674
- )
1675
- const racedChannel: Channel.Channel<
1676
- Chunk.Chunk<L | L2>,
1677
- Chunk.Chunk<In & In2>,
1678
- E | E2,
1679
- never,
1680
- A3 | A4,
1681
- unknown,
1682
- R | R2
1683
- > = channel.mergeWith(reader, {
1684
- other: writer,
1685
- onSelfDone: (_) => mergeDecision.Await((exit) => Effect.suspend(() => exit)),
1686
- onOtherDone: (done) => mergeDecision.Done(Effect.suspend(() => done))
1659
+ function race(scope: Scope.Scope) {
1660
+ return Effect.gen(function*() {
1661
+ const pubsub = yield* PubSub.bounded<
1662
+ Either.Either<Chunk.Chunk<In & In2>, Exit.Exit<unknown>>
1663
+ >(options?.capacity ?? 16)
1664
+ const subscription1 = yield* Scope.extend(PubSub.subscribe(pubsub), scope)
1665
+ const subscription2 = yield* Scope.extend(PubSub.subscribe(pubsub), scope)
1666
+ const reader = channel.toPubSub(pubsub)
1667
+ const writer = channel.fromQueue(subscription1).pipe(
1668
+ core.pipeTo(toChannel(self)),
1669
+ channel.zipLeft(core.fromEffect(Queue.shutdown(subscription1))),
1670
+ channel.mergeWith({
1671
+ other: channel.fromQueue(subscription2).pipe(
1672
+ core.pipeTo(toChannel(options.other)),
1673
+ channel.zipLeft(core.fromEffect(Queue.shutdown(subscription2)))
1674
+ ),
1675
+ onSelfDone: options.onSelfDone,
1676
+ onOtherDone: options.onOtherDone
1677
+ })
1678
+ )
1679
+ const racedChannel = channel.mergeWith(reader, {
1680
+ other: writer,
1681
+ onSelfDone: () => mergeDecision.Await(identity),
1682
+ onOtherDone: (exit) => mergeDecision.Done(exit)
1683
+ }) as Channel.Channel<
1684
+ Chunk.Chunk<L | L2>,
1685
+ Chunk.Chunk<In & In2>,
1686
+ E | E2,
1687
+ never,
1688
+ A3 | A4,
1689
+ unknown,
1690
+ R | R2
1691
+ >
1692
+ return new SinkImpl(racedChannel)
1687
1693
  })
1688
- return new SinkImpl(racedChannel)
1689
- })
1690
- return unwrapScoped(scoped)
1694
+ }
1695
+ return unwrapScopedWith(race)
1691
1696
  }
1692
1697
  )
1693
1698
 
@@ -1923,9 +1928,24 @@ export const unwrap = <A, In, L, E2, R2, E, R>(
1923
1928
  /** @internal */
1924
1929
  export const unwrapScoped = <A, In, L, E, R>(
1925
1930
  effect: Effect.Effect<Sink.Sink<A, In, L, E, R>, E, R>
1926
- ): Sink.Sink<A, In, L, E, Exclude<R, Scope.Scope>> => {
1927
- return new SinkImpl(channel.unwrapScoped(pipe(effect, Effect.map((sink) => toChannel(sink)))))
1928
- }
1931
+ ): Sink.Sink<A, In, L, E, Exclude<R, Scope.Scope>> =>
1932
+ new SinkImpl(
1933
+ channel.unwrapScoped(effect.pipe(
1934
+ Effect.map((sink) => toChannel(sink))
1935
+ ))
1936
+ )
1937
+
1938
+ /** @internal */
1939
+ export const unwrapScopedWith = <A, In, L, E, R>(
1940
+ f: (scope: Scope.Scope) => Effect.Effect<Sink.Sink<A, In, L, E, R>, E, R>
1941
+ ): Sink.Sink<A, In, L, E, R> =>
1942
+ new SinkImpl(
1943
+ channel.unwrapScopedWith((scope) =>
1944
+ f(scope).pipe(
1945
+ Effect.map((sink) => toChannel(sink))
1946
+ )
1947
+ )
1948
+ )
1929
1949
 
1930
1950
  /** @internal */
1931
1951
  export const withDuration = <A, In, L, E, R>(