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
@@ -39,7 +39,6 @@ import type { NoInfer, TupleOf } from "../Types.js"
39
39
  import * as channel from "./channel.js"
40
40
  import * as channelExecutor from "./channel/channelExecutor.js"
41
41
  import * as MergeStrategy from "./channel/mergeStrategy.js"
42
- import * as singleProducerAsyncInput from "./channel/singleProducerAsyncInput.js"
43
42
  import * as core from "./core-stream.js"
44
43
  import * as doNotation from "./doNotation.js"
45
44
  import { RingBuffer } from "./ringBuffer.js"
@@ -191,8 +190,7 @@ export const aggregateWithinEither = dual<
191
190
  Ref.make(false),
192
191
  Ref.make(false)
193
192
  ])
194
- return pipe(
195
- fromEffect(layer),
193
+ return fromEffect(layer).pipe(
196
194
  flatMap(([handoff, sinkEndReason, sinkLeftovers, scheduleDriver, consumed, endAfterEmit]) => {
197
195
  const handoffProducer: Channel.Channel<never, Chunk.Chunk<A>, never, E | E2, unknown, unknown> = core
198
196
  .readWithCause({
@@ -299,7 +297,7 @@ export const aggregateWithinEither = dual<
299
297
  handoffConsumer,
300
298
  channel.pipeToOrFail(_sink.toChannel(sink)),
301
299
  core.collectElements,
302
- channelExecutor.run,
300
+ channel.run,
303
301
  Effect.forkIn(scope)
304
302
  )
305
303
  )
@@ -421,31 +419,21 @@ export const aggregateWithinEither = dual<
421
419
  })
422
420
  )
423
421
  }
424
- return unwrapScoped(
425
- pipe(
426
- self,
427
- toChannel,
428
- core.pipeTo(handoffProducer),
429
- channelExecutor.run,
430
- Effect.forkScoped,
422
+ return unwrapScopedWith((scope) =>
423
+ core.pipeTo(toChannel(self), handoffProducer).pipe(
424
+ channel.run,
425
+ Effect.forkIn(scope),
431
426
  Effect.zipRight(
432
- pipe(
433
- handoffConsumer,
434
- channel.pipeToOrFail(_sink.toChannel(sink)),
427
+ channel.pipeToOrFail(handoffConsumer, _sink.toChannel(sink)).pipe(
435
428
  core.collectElements,
436
- channelExecutor.run,
437
- Effect.forkScoped,
429
+ channel.run,
430
+ Effect.forkIn(scope),
438
431
  Effect.flatMap((sinkFiber) =>
439
- pipe(
440
- Effect.forkScoped(timeout(Option.none())),
441
- Effect.flatMap((scheduleFiber) =>
442
- pipe(
443
- Effect.scope,
444
- Effect.map((scope) =>
445
- new StreamImpl(
446
- scheduledAggregator(sinkFiber, scheduleFiber, scope)
447
- )
448
- )
432
+ timeout(Option.none()).pipe(
433
+ Effect.forkIn(scope),
434
+ Effect.map((scheduleFiber) =>
435
+ new StreamImpl(
436
+ scheduledAggregator(sinkFiber, scheduleFiber, scope)
449
437
  )
450
438
  )
451
439
  )
@@ -1209,7 +1197,7 @@ const bufferSignal = <A, E, R>(
1209
1197
  pipe(
1210
1198
  bufferChannel,
1211
1199
  core.pipeTo(producer(queue, ref)),
1212
- channelExecutor.runScoped,
1200
+ channel.runScoped,
1213
1201
  Effect.forkScoped
1214
1202
  )
1215
1203
  ),
@@ -1530,19 +1518,20 @@ export const combine = dual<
1530
1518
  pullRight: Effect.Effect<A2, Option.Option<E2>, R4>
1531
1519
  ) => Effect.Effect<Exit.Exit<readonly [A3, S], Option.Option<E2 | E>>, never, R5>
1532
1520
  ): Stream.Stream<A3, E2 | E, R2 | R3 | R4 | R5 | R> => {
1533
- const producer = <Err, Elem>(
1521
+ function producer<Err, Elem>(
1534
1522
  handoff: Handoff.Handoff<Exit.Exit<Elem, Option.Option<Err>>>,
1535
1523
  latch: Handoff.Handoff<void>
1536
- ): Channel.Channel<never, Elem, never, Err, unknown, unknown, R> =>
1537
- pipe(
1538
- core.fromEffect(Handoff.take(latch)),
1524
+ ): Channel.Channel<never, Elem, never, Err, unknown, unknown, R> {
1525
+ return core.fromEffect(Handoff.take(latch)).pipe(
1539
1526
  channel.zipRight(core.readWithCause({
1540
1527
  onInput: (input) =>
1541
1528
  core.flatMap(
1542
- core.fromEffect(pipe(
1543
- handoff,
1544
- Handoff.offer<Exit.Exit<Elem, Option.Option<Err>>>(Exit.succeed(input))
1545
- )),
1529
+ core.fromEffect(
1530
+ Handoff.offer<Exit.Exit<Elem, Option.Option<Err>>>(
1531
+ handoff,
1532
+ Exit.succeed(input)
1533
+ )
1534
+ ),
1546
1535
  () => producer(handoff, latch)
1547
1536
  ),
1548
1537
  onFailure: (cause) =>
@@ -1564,41 +1553,41 @@ export const combine = dual<
1564
1553
  )
1565
1554
  }))
1566
1555
  )
1556
+ }
1567
1557
  return new StreamImpl(
1568
- channel.unwrapScoped(
1569
- Effect.gen(function*($) {
1570
- const left = yield* $(Handoff.make<Exit.Exit<A, Option.Option<E>>>())
1571
- const right = yield* $(Handoff.make<Exit.Exit<A2, Option.Option<E2>>>())
1572
- const latchL = yield* $(Handoff.make<void>())
1573
- const latchR = yield* $(Handoff.make<void>())
1574
- yield* $(
1575
- toChannel(self),
1576
- channel.concatMap(channel.writeChunk),
1577
- core.pipeTo(producer(left, latchL)),
1578
- channelExecutor.runScoped,
1579
- Effect.forkScoped
1580
- )
1581
- yield* $(
1582
- toChannel(that),
1583
- channel.concatMap(channel.writeChunk),
1584
- core.pipeTo(producer(right, latchR)),
1585
- channelExecutor.runScoped,
1586
- Effect.forkScoped
1587
- )
1588
- const pullLeft = pipe(
1589
- latchL,
1590
- Handoff.offer<void>(void 0),
1591
- // TODO: remove
1592
- Effect.zipRight(pipe(Handoff.take(left), Effect.flatMap((exit) => Effect.suspend(() => exit))))
1593
- )
1594
- const pullRight = pipe(
1595
- latchR,
1596
- Handoff.offer<void>(void 0),
1597
- // TODO: remove
1598
- Effect.zipRight(pipe(Handoff.take(right), Effect.flatMap((exit) => Effect.suspend(() => exit))))
1599
- )
1600
- return toChannel(unfoldEffect(s, (s) => Effect.flatMap(f(s, pullLeft, pullRight), unsome)))
1601
- })
1558
+ channel.unwrapScopedWith((scope) =>
1559
+ Effect.all([
1560
+ Handoff.make<Exit.Exit<A, Option.Option<E>>>(),
1561
+ Handoff.make<Exit.Exit<A2, Option.Option<E2>>>(),
1562
+ Handoff.make<void>(),
1563
+ Handoff.make<void>()
1564
+ ]).pipe(
1565
+ Effect.tap(([left, _, latchL]) =>
1566
+ toChannel(self).pipe(
1567
+ channel.concatMap(channel.writeChunk),
1568
+ core.pipeTo(producer(left, latchL)),
1569
+ channelExecutor.runIn(scope),
1570
+ Effect.forkIn(scope)
1571
+ )
1572
+ ),
1573
+ Effect.tap(([, right, _, rightL]) =>
1574
+ toChannel(that).pipe(
1575
+ channel.concatMap(channel.writeChunk),
1576
+ core.pipeTo(producer(right, rightL)),
1577
+ channelExecutor.runIn(scope),
1578
+ Effect.forkIn(scope)
1579
+ )
1580
+ ),
1581
+ Effect.map(([left, right, latchL, latchR]) => {
1582
+ const pullLeft = Handoff.offer<void>(latchL, void 0).pipe(
1583
+ Effect.zipRight(Handoff.take(left).pipe(Effect.flatMap(identity)))
1584
+ )
1585
+ const pullRight = Handoff.offer<void>(latchR, void 0).pipe(
1586
+ Effect.zipRight(Handoff.take(right).pipe(Effect.flatMap(identity)))
1587
+ )
1588
+ return toChannel(unfoldEffect(s, (s) => Effect.flatMap(f(s, pullLeft, pullRight), unsome)))
1589
+ })
1590
+ )
1602
1591
  )
1603
1592
  )
1604
1593
  })
@@ -1661,53 +1650,35 @@ export const combineChunks = dual<
1661
1650
  })
1662
1651
  )
1663
1652
  return new StreamImpl(
1664
- pipe(
1653
+ channel.unwrapScopedWith((scope) =>
1665
1654
  Effect.all([
1666
1655
  Handoff.make<Take.Take<A, E>>(),
1667
1656
  Handoff.make<Take.Take<A2, E2>>(),
1668
1657
  Handoff.make<void>(),
1669
1658
  Handoff.make<void>()
1670
- ]),
1671
- Effect.tap(([left, _, latchL]) =>
1672
- pipe(
1673
- toChannel(self),
1674
- core.pipeTo(producer(left, latchL)),
1675
- channelExecutor.runScoped,
1676
- Effect.forkScoped
1677
- )
1678
- ),
1679
- Effect.tap(([_, right, __, latchR]) =>
1680
- pipe(
1681
- toChannel(that),
1682
- core.pipeTo(producer(right, latchR)),
1683
- channelExecutor.runScoped,
1684
- Effect.forkScoped
1685
- )
1686
- ),
1687
- Effect.map(([left, right, latchL, latchR]) => {
1688
- const pullLeft = pipe(
1689
- latchL,
1690
- Handoff.offer<void>(void 0),
1691
- Effect.zipRight(
1692
- pipe(
1693
- Handoff.take(left),
1694
- Effect.flatMap(InternalTake.done)
1695
- )
1659
+ ]).pipe(
1660
+ Effect.tap(([left, _, latchL]) =>
1661
+ core.pipeTo(toChannel(self), producer(left, latchL)).pipe(
1662
+ channelExecutor.runIn(scope),
1663
+ Effect.forkIn(scope)
1696
1664
  )
1697
- )
1698
- const pullRight = pipe(
1699
- latchR,
1700
- Handoff.offer<void>(void 0),
1701
- Effect.zipRight(
1702
- pipe(
1703
- Handoff.take(right),
1704
- Effect.flatMap(InternalTake.done)
1705
- )
1665
+ ),
1666
+ Effect.tap(([_, right, __, latchR]) =>
1667
+ core.pipeTo(toChannel(that), producer(right, latchR)).pipe(
1668
+ channelExecutor.runIn(scope),
1669
+ Effect.forkIn(scope)
1706
1670
  )
1707
- )
1708
- return toChannel(unfoldChunkEffect(s, (s) => Effect.flatMap(f(s, pullLeft, pullRight), unsome)))
1709
- }),
1710
- channel.unwrapScoped
1671
+ ),
1672
+ Effect.map(([left, right, latchL, latchR]) => {
1673
+ const pullLeft = Handoff.offer<void>(latchL, void 0).pipe(
1674
+ Effect.zipRight(Handoff.take(left).pipe(Effect.flatMap(InternalTake.done)))
1675
+ )
1676
+ const pullRight = Handoff.offer<void>(latchR, void 0).pipe(
1677
+ Effect.zipRight(Handoff.take(right).pipe(Effect.flatMap(InternalTake.done)))
1678
+ )
1679
+ return toChannel(unfoldChunkEffect(s, (s) => Effect.flatMap(f(s, pullLeft, pullRight), unsome)))
1680
+ })
1681
+ )
1711
1682
  )
1712
1683
  )
1713
1684
  })
@@ -1809,101 +1780,114 @@ export const crossWith: {
1809
1780
  export const debounce = dual<
1810
1781
  (duration: Duration.DurationInput) => <A, E, R>(self: Stream.Stream<A, E, R>) => Stream.Stream<A, E, R>,
1811
1782
  <A, E, R>(self: Stream.Stream<A, E, R>, duration: Duration.DurationInput) => Stream.Stream<A, E, R>
1812
- >(2, <A, E, R>(self: Stream.Stream<A, E, R>, duration: Duration.DurationInput): Stream.Stream<A, E, R> =>
1813
- pipe(
1814
- singleProducerAsyncInput.make<never, Chunk.Chunk<A>, unknown>(),
1815
- Effect.flatMap((input) =>
1816
- Effect.transplant((grafter) =>
1817
- pipe(
1818
- Handoff.make<HandoffSignal.HandoffSignal<A, E>>(),
1819
- Effect.map((handoff) => {
1820
- const enqueue = (last: Chunk.Chunk<A>): Effect.Effect<
1821
- Channel.Channel<Chunk.Chunk<A>, unknown, E, unknown, unknown, unknown>
1822
- > =>
1823
- pipe(
1824
- Clock.sleep(duration),
1825
- Effect.as(last),
1826
- Effect.fork,
1827
- grafter,
1828
- Effect.map((fiber) => consumer(DebounceState.previous(fiber)))
1783
+ >(
1784
+ 2,
1785
+ <A, E, R>(self: Stream.Stream<A, E, R>, duration: Duration.DurationInput): Stream.Stream<A, E, R> =>
1786
+ unwrapScopedWith((scope) =>
1787
+ Effect.gen(function*() {
1788
+ const handoff = yield* Handoff.make<HandoffSignal.HandoffSignal<A, E>>()
1789
+
1790
+ function enqueue(last: Chunk.Chunk<A>): Effect.Effect<
1791
+ Channel.Channel<Chunk.Chunk<A>, unknown, E, unknown, unknown, unknown>
1792
+ > {
1793
+ return Clock.sleep(duration).pipe(
1794
+ Effect.as(last),
1795
+ Effect.forkIn(scope),
1796
+ Effect.map((fiber) => consumer(DebounceState.previous(fiber)))
1797
+ )
1798
+ }
1799
+
1800
+ const producer: Channel.Channel<never, Chunk.Chunk<A>, E, E, unknown, unknown> = core.readWithCause({
1801
+ onInput: (input: Chunk.Chunk<A>) =>
1802
+ Option.match(Chunk.last(input), {
1803
+ onNone: () => producer,
1804
+ onSome: (elem) =>
1805
+ core.fromEffect(Handoff.offer(handoff, HandoffSignal.emit(Chunk.of(elem)))).pipe(
1806
+ core.flatMap(() => producer)
1807
+ )
1808
+ }),
1809
+ onFailure: (cause) =>
1810
+ core.fromEffect(
1811
+ Handoff.offer<HandoffSignal.HandoffSignal<A, E>>(handoff, HandoffSignal.halt(cause))
1812
+ ),
1813
+ onDone: () =>
1814
+ core.fromEffect(
1815
+ Handoff.offer<HandoffSignal.HandoffSignal<A, E>>(
1816
+ handoff,
1817
+ HandoffSignal.end(SinkEndReason.UpstreamEnd)
1829
1818
  )
1830
- const producer: Channel.Channel<never, Chunk.Chunk<A>, E, E, unknown, unknown> = core
1831
- .readWithCause({
1832
- onInput: (input: Chunk.Chunk<A>) =>
1833
- Option.match(Chunk.last(input), {
1834
- onNone: () => producer,
1835
- onSome: (last) =>
1836
- core.flatMap(
1837
- core.fromEffect(
1838
- Handoff.offer<HandoffSignal.HandoffSignal<A, E>>(
1839
- handoff,
1840
- HandoffSignal.emit(Chunk.of(last))
1841
- )
1842
- ),
1843
- () => producer
1844
- )
1845
- }),
1846
- onFailure: (cause) =>
1847
- core.fromEffect(
1848
- Handoff.offer<HandoffSignal.HandoffSignal<A, E>>(handoff, HandoffSignal.halt(cause))
1849
- ),
1850
- onDone: () =>
1851
- core.fromEffect(
1852
- Handoff.offer<HandoffSignal.HandoffSignal<A, E>>(
1853
- handoff,
1854
- HandoffSignal.end(SinkEndReason.UpstreamEnd)
1855
- )
1856
- )
1857
- })
1858
- const consumer = (
1859
- state: DebounceState.DebounceState<A, E>
1860
- ): Channel.Channel<Chunk.Chunk<A>, unknown, E, unknown, unknown, unknown> => {
1861
- switch (state._tag) {
1862
- case DebounceState.OP_NOT_STARTED: {
1863
- return pipe(
1864
- Handoff.take(handoff),
1865
- Effect.map((signal) => {
1866
- switch (signal._tag) {
1867
- case HandoffSignal.OP_EMIT: {
1868
- return channel.unwrap(enqueue(signal.elements))
1869
- }
1870
- case HandoffSignal.OP_HALT: {
1871
- return core.failCause(signal.cause)
1872
- }
1873
- case HandoffSignal.OP_END: {
1874
- return core.void
1875
- }
1819
+ )
1820
+ })
1821
+
1822
+ function consumer(
1823
+ state: DebounceState.DebounceState<A, E>
1824
+ ): Channel.Channel<Chunk.Chunk<A>, unknown, E, unknown, unknown, unknown> {
1825
+ switch (state._tag) {
1826
+ case DebounceState.OP_NOT_STARTED: {
1827
+ return channel.unwrap(
1828
+ Handoff.take(handoff).pipe(
1829
+ Effect.map((signal) => {
1830
+ switch (signal._tag) {
1831
+ case HandoffSignal.OP_EMIT: {
1832
+ return channel.unwrap(enqueue(signal.elements))
1876
1833
  }
1877
- }),
1878
- channel.unwrap
1879
- )
1880
- }
1881
- case DebounceState.OP_PREVIOUS: {
1882
- return channel.unwrap(
1883
- Effect.raceWith(Fiber.join(state.fiber), Handoff.take(handoff), {
1834
+ case HandoffSignal.OP_HALT: {
1835
+ return core.failCause(signal.cause)
1836
+ }
1837
+ case HandoffSignal.OP_END: {
1838
+ return core.void
1839
+ }
1840
+ }
1841
+ })
1842
+ )
1843
+ )
1844
+ }
1845
+ case DebounceState.OP_PREVIOUS: {
1846
+ return channel.unwrap(
1847
+ Handoff.take(handoff).pipe(
1848
+ Effect.forkIn(scope),
1849
+ Effect.flatMap((handoffFiber) =>
1850
+ Effect.raceWith(Fiber.join(state.fiber), Fiber.join(handoffFiber), {
1884
1851
  onSelfDone: (leftExit, current) =>
1885
1852
  Exit.match(leftExit, {
1886
- onFailure: (cause) => pipe(Fiber.interrupt(current), Effect.as(core.failCause(cause))),
1853
+ onFailure: (cause) =>
1854
+ Fiber.interrupt(current).pipe(
1855
+ Effect.as(core.failCause(cause))
1856
+ ),
1887
1857
  onSuccess: (chunk) =>
1888
- Effect.succeed(
1889
- pipe(core.write(chunk), core.flatMap(() => consumer(DebounceState.current(current))))
1858
+ Fiber.interrupt(current).pipe(
1859
+ Effect.zipRight(Effect.succeed(
1860
+ core.write(chunk).pipe(
1861
+ core.flatMap(() => consumer(DebounceState.current(handoffFiber)))
1862
+ )
1863
+ ))
1890
1864
  )
1891
1865
  }),
1892
1866
  onOtherDone: (rightExit, previous) =>
1893
1867
  Exit.match(rightExit, {
1894
- onFailure: (cause) => pipe(Fiber.interrupt(previous), Effect.as(core.failCause(cause))),
1868
+ onFailure: (cause) =>
1869
+ Fiber.interrupt(previous).pipe(
1870
+ Effect.as(core.failCause(cause))
1871
+ ),
1895
1872
  onSuccess: (signal) => {
1896
1873
  switch (signal._tag) {
1897
1874
  case HandoffSignal.OP_EMIT: {
1898
- return pipe(Fiber.interrupt(previous), Effect.zipRight(enqueue(signal.elements)))
1875
+ return Fiber.interrupt(previous).pipe(
1876
+ Effect.zipRight(enqueue(signal.elements))
1877
+ )
1899
1878
  }
1900
1879
  case HandoffSignal.OP_HALT: {
1901
- return pipe(Fiber.interrupt(previous), Effect.as(core.failCause(signal.cause)))
1880
+ return Fiber.interrupt(previous).pipe(
1881
+ Effect.as(core.failCause(signal.cause))
1882
+ )
1902
1883
  }
1903
1884
  case HandoffSignal.OP_END: {
1904
- return pipe(
1905
- Fiber.join(previous),
1906
- Effect.map((chunk) => pipe(core.write(chunk), channel.zipRight(core.void)))
1885
+ return Fiber.join(previous).pipe(
1886
+ Effect.map((chunk) =>
1887
+ core.write(chunk).pipe(
1888
+ channel.zipRight(core.void)
1889
+ )
1890
+ )
1907
1891
  )
1908
1892
  }
1909
1893
  }
@@ -1911,46 +1895,40 @@ export const debounce = dual<
1911
1895
  })
1912
1896
  })
1913
1897
  )
1914
- }
1915
- case DebounceState.OP_CURRENT: {
1916
- return pipe(
1917
- Fiber.join(state.fiber),
1918
- Effect.map((signal) => {
1919
- switch (signal._tag) {
1920
- case HandoffSignal.OP_EMIT: {
1921
- return channel.unwrap(enqueue(signal.elements))
1922
- }
1923
- case HandoffSignal.OP_HALT: {
1924
- return core.failCause(signal.cause)
1925
- }
1926
- case HandoffSignal.OP_END: {
1927
- return core.void
1928
- }
1898
+ )
1899
+ )
1900
+ }
1901
+ case DebounceState.OP_CURRENT: {
1902
+ return channel.unwrap(
1903
+ Fiber.join(state.fiber).pipe(
1904
+ Effect.map((signal) => {
1905
+ switch (signal._tag) {
1906
+ case HandoffSignal.OP_EMIT: {
1907
+ return channel.unwrap(enqueue(signal.elements))
1929
1908
  }
1930
- }),
1931
- channel.unwrap
1932
- )
1933
- }
1934
- }
1909
+ case HandoffSignal.OP_HALT: {
1910
+ return core.failCause(signal.cause)
1911
+ }
1912
+ case HandoffSignal.OP_END: {
1913
+ return core.void
1914
+ }
1915
+ }
1916
+ })
1917
+ )
1918
+ )
1935
1919
  }
1936
- const debounceChannel: Channel.Channel<Chunk.Chunk<A>, Chunk.Chunk<A>, E, E, unknown, unknown> = pipe(
1937
- channel.fromInput(input),
1938
- core.pipeTo(producer),
1939
- channelExecutor.run,
1940
- Effect.forkScoped,
1941
- Effect.as(pipe(
1942
- consumer(DebounceState.notStarted),
1943
- core.embedInput<E, Chunk.Chunk<A>, unknown>(input as any)
1944
- )),
1945
- channel.unwrapScoped
1946
- )
1947
- return new StreamImpl(pipe(toChannel(self), core.pipeTo(debounceChannel)))
1948
- })
1949
- )
1950
- )
1951
- ),
1952
- unwrap
1953
- ))
1920
+ }
1921
+ }
1922
+
1923
+ return scopedWith((scope) =>
1924
+ core.pipeTo(toChannel(self), producer).pipe(
1925
+ channelExecutor.runIn(scope),
1926
+ Effect.forkIn(scope)
1927
+ )
1928
+ ).pipe(crossRight(new StreamImpl(consumer(DebounceState.notStarted))))
1929
+ })
1930
+ )
1931
+ )
1954
1932
 
1955
1933
  /** @internal */
1956
1934
  export const die = (defect: unknown): Stream.Stream<never> => fromEffect(Effect.die(defect))
@@ -2270,22 +2248,16 @@ export const drainFork = dual<
2270
2248
  self: Stream.Stream<A, E, R>,
2271
2249
  that: Stream.Stream<A2, E2, R2>
2272
2250
  ): Stream.Stream<A, E2 | E, R2 | R> =>
2273
- pipe(
2274
- fromEffect(Deferred.make<never, E2>()),
2275
- flatMap((backgroundDied) =>
2276
- pipe(
2277
- scoped(
2278
- pipe(
2279
- that,
2280
- runForEachScoped(() => Effect.void),
2281
- Effect.catchAllCause((cause) => Deferred.failCause(backgroundDied, cause)),
2282
- Effect.forkScoped
2283
- )
2284
- ),
2285
- crossRight(pipe(self, interruptWhenDeferred(backgroundDied)))
2251
+ fromEffect(Deferred.make<never, E2>()).pipe(flatMap((backgroundDied) =>
2252
+ scopedWith((scope) =>
2253
+ toChannel(that).pipe(
2254
+ channel.drain,
2255
+ channelExecutor.runIn(scope),
2256
+ Effect.catchAllCause((cause) => Deferred.failCause(backgroundDied, cause)),
2257
+ Effect.forkIn(scope)
2286
2258
  )
2287
- )
2288
- )
2259
+ ).pipe(crossRight(interruptWhenDeferred(self, backgroundDied)))
2260
+ ))
2289
2261
  )
2290
2262
 
2291
2263
  /** @internal */
@@ -3544,10 +3516,11 @@ export const haltWhen = dual<
3544
3516
  channel.unwrap
3545
3517
  )
3546
3518
  return new StreamImpl(
3547
- pipe(
3548
- Effect.forkScoped(effect),
3549
- Effect.map((fiber) => pipe(toChannel(self), core.pipeTo(writer(fiber)))),
3550
- channel.unwrapScoped
3519
+ channel.unwrapScopedWith((scope) =>
3520
+ effect.pipe(
3521
+ Effect.forkIn(scope),
3522
+ Effect.map((fiber) => toChannel(self).pipe(core.pipeTo(writer(fiber))))
3523
+ )
3551
3524
  )
3552
3525
  )
3553
3526
  }
@@ -3656,26 +3629,24 @@ export const interleaveWith = dual<
3656
3629
  )
3657
3630
  })
3658
3631
  return new StreamImpl(
3659
- channel.unwrapScoped(
3632
+ channel.unwrapScopedWith((scope) =>
3660
3633
  pipe(
3661
3634
  Handoff.make<Take.Take<A | A2, E | E2 | E3>>(),
3662
3635
  Effect.zip(Handoff.make<Take.Take<A | A2, E | E2 | E3>>()),
3663
3636
  Effect.tap(([left]) =>
3664
- pipe(
3665
- toChannel(self),
3637
+ toChannel(self).pipe(
3666
3638
  channel.concatMap(channel.writeChunk),
3667
3639
  core.pipeTo(producer(left)),
3668
- channelExecutor.runScoped,
3669
- Effect.forkScoped
3640
+ channelExecutor.runIn(scope),
3641
+ Effect.forkIn(scope)
3670
3642
  )
3671
3643
  ),
3672
3644
  Effect.tap(([_, right]) =>
3673
- pipe(
3674
- toChannel(that),
3645
+ toChannel(that).pipe(
3675
3646
  channel.concatMap(channel.writeChunk),
3676
3647
  core.pipeTo(producer(right)),
3677
- channelExecutor.runScoped,
3678
- Effect.forkScoped
3648
+ channelExecutor.runIn(scope),
3649
+ Effect.forkIn(scope)
3679
3650
  )
3680
3651
  ),
3681
3652
  Effect.map(([left, right]) => {
@@ -4895,10 +4866,11 @@ export const provideLayer = dual<
4895
4866
  layer: Layer.Layer<ROut, E2, RIn>
4896
4867
  ): Stream.Stream<A, E2 | E, RIn> =>
4897
4868
  new StreamImpl(
4898
- channel.unwrapScoped(pipe(
4899
- Layer.build(layer),
4900
- Effect.map((env) => pipe(toChannel(self), core.provideContext(env)))
4901
- ))
4869
+ channel.unwrapScopedWith((scope) =>
4870
+ Layer.buildWithScope(layer, scope).pipe(
4871
+ Effect.map((env) => pipe(toChannel(self), core.provideContext(env)))
4872
+ )
4873
+ )
4902
4874
  )
4903
4875
  )
4904
4876
 
@@ -5508,44 +5480,50 @@ export const retry = dual<
5508
5480
  export const run = dual<
5509
5481
  <A2, A, E2, R2>(
5510
5482
  sink: Sink.Sink<A2, A, unknown, E2, R2>
5511
- ) => <E, R>(self: Stream.Stream<A, E, R>) => Effect.Effect<A2, E2 | E, Exclude<R | R2, Scope.Scope>>,
5483
+ ) => <E, R>(
5484
+ self: Stream.Stream<A, E, R>
5485
+ ) => Effect.Effect<A2, E2 | E, R | R2>,
5512
5486
  <A, E, R, A2, E2, R2>(
5513
5487
  self: Stream.Stream<A, E, R>,
5514
5488
  sink: Sink.Sink<A2, A, unknown, E2, R2>
5515
- ) => Effect.Effect<A2, E2 | E, Exclude<R | R2, Scope.Scope>>
5489
+ ) => Effect.Effect<A2, E2 | E, R | R2>
5516
5490
  >(2, <A, E, R, A2, E2, R2>(
5517
5491
  self: Stream.Stream<A, E, R>,
5518
5492
  sink: Sink.Sink<A2, A, unknown, E2, R2>
5519
- ): Effect.Effect<A2, E2 | E, Exclude<R | R2, Scope.Scope>> =>
5520
- pipe(toChannel(self), channel.pipeToOrFail(_sink.toChannel(sink)), channel.runDrain))
5493
+ ): Effect.Effect<A2, E2 | E, R | R2> =>
5494
+ toChannel(self).pipe(
5495
+ channel.pipeToOrFail(_sink.toChannel(sink)),
5496
+ channel.runDrain
5497
+ ))
5521
5498
 
5522
5499
  /** @internal */
5523
5500
  export const runCollect = <A, E, R>(
5524
5501
  self: Stream.Stream<A, E, R>
5525
- ): Effect.Effect<Chunk.Chunk<A>, E, Exclude<R, Scope.Scope>> => pipe(self, run(_sink.collectAll()))
5502
+ ): Effect.Effect<Chunk.Chunk<A>, E, R> => run(self, _sink.collectAll())
5526
5503
 
5527
5504
  /** @internal */
5528
- export const runCount = <A, E, R>(self: Stream.Stream<A, E, R>): Effect.Effect<number, E, Exclude<R, Scope.Scope>> =>
5529
- pipe(self, run(_sink.count))
5505
+ export const runCount = <A, E, R>(self: Stream.Stream<A, E, R>): Effect.Effect<number, E, R> => run(self, _sink.count)
5530
5506
 
5531
5507
  /** @internal */
5532
- export const runDrain = <A, E, R>(self: Stream.Stream<A, E, R>): Effect.Effect<void, E, Exclude<R, Scope.Scope>> =>
5533
- pipe(self, run(_sink.drain))
5508
+ export const runDrain = <A, E, R>(self: Stream.Stream<A, E, R>): Effect.Effect<void, E, R> => run(self, _sink.drain)
5534
5509
 
5535
5510
  /** @internal */
5536
5511
  export const runFold = dual<
5537
5512
  <S, A>(
5538
5513
  s: S,
5539
5514
  f: (s: S, a: A) => S
5540
- ) => <E, R>(self: Stream.Stream<A, E, R>) => Effect.Effect<S, E, Exclude<R, Scope.Scope>>,
5541
- <A, E, R, S>(self: Stream.Stream<A, E, R>, s: S, f: (s: S, a: A) => S) => Effect.Effect<S, E, Exclude<R, Scope.Scope>>
5542
- >(
5543
- 3,
5515
+ ) => <E, R>(
5516
+ self: Stream.Stream<A, E, R>
5517
+ ) => Effect.Effect<S, E, R>,
5544
5518
  <A, E, R, S>(
5545
5519
  self: Stream.Stream<A, E, R>,
5546
5520
  s: S,
5547
5521
  f: (s: S, a: A) => S
5548
- ): Effect.Effect<S, E, Exclude<R, Scope.Scope>> => pipe(self, runFoldWhileScoped(s, constTrue, f), Effect.scoped)
5522
+ ) => Effect.Effect<S, E, R>
5523
+ >(
5524
+ 3,
5525
+ <A, E, R, S>(self: Stream.Stream<A, E, R>, s: S, f: (s: S, a: A) => S): Effect.Effect<S, E, R> =>
5526
+ runFoldWhile(self, s, constTrue, f)
5549
5527
  )
5550
5528
 
5551
5529
  /** @internal */
@@ -5553,18 +5531,17 @@ export const runFoldEffect = dual<
5553
5531
  <S, A, E2, R2>(
5554
5532
  s: S,
5555
5533
  f: (s: S, a: A) => Effect.Effect<S, E2, R2>
5556
- ) => <E, R>(self: Stream.Stream<A, E, R>) => Effect.Effect<S, E2 | E, Exclude<R | R2, Scope.Scope>>,
5534
+ ) => <E, R>(self: Stream.Stream<A, E, R>) => Effect.Effect<S, E2 | E, R | R2>,
5557
5535
  <A, E, R, S, E2, R2>(
5558
5536
  self: Stream.Stream<A, E, R>,
5559
5537
  s: S,
5560
5538
  f: (s: S, a: A) => Effect.Effect<S, E2, R2>
5561
- ) => Effect.Effect<S, E2 | E, Exclude<R | R2, Scope.Scope>>
5539
+ ) => Effect.Effect<S, E2 | E, R | R2>
5562
5540
  >(3, <A, E, R, S, E2, R2>(
5563
5541
  self: Stream.Stream<A, E, R>,
5564
5542
  s: S,
5565
5543
  f: (s: S, a: A) => Effect.Effect<S, E2, R2>
5566
- ): Effect.Effect<S, E2 | E, Exclude<R | R2, Scope.Scope>> =>
5567
- pipe(self, runFoldWhileScopedEffect(s, constTrue, f), Effect.scoped))
5544
+ ): Effect.Effect<S, E2 | E, R | R2> => runFoldWhileEffect(self, s, constTrue, f))
5568
5545
 
5569
5546
  /** @internal */
5570
5547
  export const runFoldScoped = dual<
@@ -5599,19 +5576,21 @@ export const runFoldWhile = dual<
5599
5576
  s: S,
5600
5577
  cont: Predicate<S>,
5601
5578
  f: (s: S, a: A) => S
5602
- ) => <E, R>(self: Stream.Stream<A, E, R>) => Effect.Effect<S, E, Exclude<R, Scope.Scope>>,
5579
+ ) => <E, R>(
5580
+ self: Stream.Stream<A, E, R>
5581
+ ) => Effect.Effect<S, E, R>,
5603
5582
  <A, E, R, S>(
5604
5583
  self: Stream.Stream<A, E, R>,
5605
5584
  s: S,
5606
5585
  cont: Predicate<S>,
5607
5586
  f: (s: S, a: A) => S
5608
- ) => Effect.Effect<S, E, Exclude<R, Scope.Scope>>
5587
+ ) => Effect.Effect<S, E, R>
5609
5588
  >(4, <A, E, R, S>(
5610
5589
  self: Stream.Stream<A, E, R>,
5611
5590
  s: S,
5612
5591
  cont: Predicate<S>,
5613
5592
  f: (s: S, a: A) => S
5614
- ): Effect.Effect<S, E, Exclude<R, Scope.Scope>> => pipe(self, runFoldWhileScoped(s, cont, f), Effect.scoped))
5593
+ ): Effect.Effect<S, E, R> => run(self, _sink.fold(s, cont, f)))
5615
5594
 
5616
5595
  /** @internal */
5617
5596
  export const runFoldWhileEffect = dual<
@@ -5619,20 +5598,21 @@ export const runFoldWhileEffect = dual<
5619
5598
  s: S,
5620
5599
  cont: Predicate<S>,
5621
5600
  f: (s: S, a: A) => Effect.Effect<S, E2, R2>
5622
- ) => <E, R>(self: Stream.Stream<A, E, R>) => Effect.Effect<S, E2 | E, Exclude<R | R2, Scope.Scope>>,
5601
+ ) => <E, R>(
5602
+ self: Stream.Stream<A, E, R>
5603
+ ) => Effect.Effect<S, E2 | E, R | R2>,
5623
5604
  <A, E, R, S, E2, R2>(
5624
5605
  self: Stream.Stream<A, E, R>,
5625
5606
  s: S,
5626
5607
  cont: Predicate<S>,
5627
5608
  f: (s: S, a: A) => Effect.Effect<S, E2, R2>
5628
- ) => Effect.Effect<S, E2 | E, Exclude<R | R2, Scope.Scope>>
5609
+ ) => Effect.Effect<S, E2 | E, R | R2>
5629
5610
  >(4, <A, E, R, S, E2, R2>(
5630
5611
  self: Stream.Stream<A, E, R>,
5631
5612
  s: S,
5632
5613
  cont: Predicate<S>,
5633
5614
  f: (s: S, a: A) => Effect.Effect<S, E2, R2>
5634
- ): Effect.Effect<S, E2 | E, Exclude<R | R2, Scope.Scope>> =>
5635
- pipe(self, runFoldWhileScopedEffect(s, cont, f), Effect.scoped))
5615
+ ): Effect.Effect<S, E2 | E, R | R2> => run(self, _sink.foldEffect(s, cont, f)))
5636
5616
 
5637
5617
  /** @internal */
5638
5618
  export const runFoldWhileScoped = dual<
@@ -5678,29 +5658,33 @@ export const runFoldWhileScopedEffect = dual<
5678
5658
  export const runForEach = dual<
5679
5659
  <A, X, E2, R2>(
5680
5660
  f: (a: A) => Effect.Effect<X, E2, R2>
5681
- ) => <E, R>(self: Stream.Stream<A, E, R>) => Effect.Effect<void, E2 | E, Exclude<R | R2, Scope.Scope>>,
5661
+ ) => <E, R>(
5662
+ self: Stream.Stream<A, E, R>
5663
+ ) => Effect.Effect<void, E2 | E, R | R2>,
5682
5664
  <A, E, R, X, E2, R2>(
5683
5665
  self: Stream.Stream<A, E, R>,
5684
5666
  f: (a: A) => Effect.Effect<X, E2, R2>
5685
- ) => Effect.Effect<void, E2 | E, Exclude<R | R2, Scope.Scope>>
5667
+ ) => Effect.Effect<void, E2 | E, R | R2>
5686
5668
  >(2, <A, E, R, X, E2, R2>(
5687
5669
  self: Stream.Stream<A, E, R>,
5688
5670
  f: (a: A) => Effect.Effect<X, E2, R2>
5689
- ): Effect.Effect<void, E2 | E, Exclude<R | R2, Scope.Scope>> => pipe(self, run(_sink.forEach(f))))
5671
+ ): Effect.Effect<void, E2 | E, R | R2> => run(self, _sink.forEach(f)))
5690
5672
 
5691
5673
  /** @internal */
5692
5674
  export const runForEachChunk = dual<
5693
5675
  <A, X, E2, R2>(
5694
5676
  f: (a: Chunk.Chunk<A>) => Effect.Effect<X, E2, R2>
5695
- ) => <E, R>(self: Stream.Stream<A, E, R>) => Effect.Effect<void, E2 | E, Exclude<R | R2, Scope.Scope>>,
5677
+ ) => <E, R>(
5678
+ self: Stream.Stream<A, E, R>
5679
+ ) => Effect.Effect<void, E2 | E, R | R2>,
5696
5680
  <A, E, R, X, E2, R2>(
5697
5681
  self: Stream.Stream<A, E, R>,
5698
5682
  f: (a: Chunk.Chunk<A>) => Effect.Effect<X, E2, R2>
5699
- ) => Effect.Effect<void, E2 | E, Exclude<R | R2, Scope.Scope>>
5683
+ ) => Effect.Effect<void, E2 | E, R | R2>
5700
5684
  >(2, <A, E, R, X, E2, R2>(
5701
5685
  self: Stream.Stream<A, E, R>,
5702
5686
  f: (a: Chunk.Chunk<A>) => Effect.Effect<X, E2, R2>
5703
- ): Effect.Effect<void, E2 | E, Exclude<R | R2, Scope.Scope>> => pipe(self, run(_sink.forEachChunk(f))))
5687
+ ): Effect.Effect<void, E2 | E, R | R2> => run(self, _sink.forEachChunk(f)))
5704
5688
 
5705
5689
  /** @internal */
5706
5690
  export const runForEachChunkScoped = dual<
@@ -5734,15 +5718,17 @@ export const runForEachScoped = dual<
5734
5718
  export const runForEachWhile = dual<
5735
5719
  <A, E2, R2>(
5736
5720
  f: (a: A) => Effect.Effect<boolean, E2, R2>
5737
- ) => <E, R>(self: Stream.Stream<A, E, R>) => Effect.Effect<void, E2 | E, Exclude<R | R2, Scope.Scope>>,
5721
+ ) => <E, R>(
5722
+ self: Stream.Stream<A, E, R>
5723
+ ) => Effect.Effect<void, E2 | E, R | R2>,
5738
5724
  <A, E, R, E2, R2>(
5739
5725
  self: Stream.Stream<A, E, R>,
5740
5726
  f: (a: A) => Effect.Effect<boolean, E2, R2>
5741
- ) => Effect.Effect<void, E2 | E, Exclude<R | R2, Scope.Scope>>
5727
+ ) => Effect.Effect<void, E2 | E, R | R2>
5742
5728
  >(2, <A, E, R, E2, R2>(
5743
5729
  self: Stream.Stream<A, E, R>,
5744
5730
  f: (a: A) => Effect.Effect<boolean, E2, R2>
5745
- ): Effect.Effect<void, E2 | E, Exclude<R | R2, Scope.Scope>> => pipe(self, run(_sink.forEachWhile(f))))
5731
+ ): Effect.Effect<void, E2 | E, R | R2> => run(self, _sink.forEachWhile(f)))
5746
5732
 
5747
5733
  /** @internal */
5748
5734
  export const runForEachWhileScoped = dual<
@@ -5761,7 +5747,7 @@ export const runForEachWhileScoped = dual<
5761
5747
  /** @internal */
5762
5748
  export const runHead = <A, E, R>(
5763
5749
  self: Stream.Stream<A, E, R>
5764
- ): Effect.Effect<Option.Option<A>, E, Exclude<R, Scope.Scope>> => pipe(self, run(_sink.head<A>()))
5750
+ ): Effect.Effect<Option.Option<A>, E, R> => run(self, _sink.head<A>())
5765
5751
 
5766
5752
  /** @internal */
5767
5753
  export const runIntoPubSub = dual<
@@ -5837,7 +5823,7 @@ export const runIntoQueueElementsScoped = dual<
5837
5823
  return pipe(
5838
5824
  core.pipeTo(toChannel(self), writer),
5839
5825
  channel.drain,
5840
- channelExecutor.runScoped,
5826
+ channel.runScoped,
5841
5827
  Effect.asVoid
5842
5828
  )
5843
5829
  })
@@ -5865,7 +5851,7 @@ export const runIntoQueueScoped = dual<
5865
5851
  core.pipeTo(toChannel(self), writer),
5866
5852
  channel.mapOutEffect((take) => Queue.offer(queue, take)),
5867
5853
  channel.drain,
5868
- channelExecutor.runScoped,
5854
+ channel.runScoped,
5869
5855
  Effect.asVoid
5870
5856
  )
5871
5857
  })
@@ -5873,7 +5859,7 @@ export const runIntoQueueScoped = dual<
5873
5859
  /** @internal */
5874
5860
  export const runLast = <A, E, R>(
5875
5861
  self: Stream.Stream<A, E, R>
5876
- ): Effect.Effect<Option.Option<A>, E, Exclude<R, Scope.Scope>> => pipe(self, run(_sink.last()))
5862
+ ): Effect.Effect<Option.Option<A>, E, R> => run(self, _sink.last())
5877
5863
 
5878
5864
  /** @internal */
5879
5865
  export const runScoped = dual<
@@ -5892,12 +5878,11 @@ export const runScoped = dual<
5892
5878
  toChannel(self),
5893
5879
  channel.pipeToOrFail(_sink.toChannel(sink)),
5894
5880
  channel.drain,
5895
- channelExecutor.runScoped
5881
+ channel.runScoped
5896
5882
  ))
5897
5883
 
5898
5884
  /** @internal */
5899
- export const runSum = <E, R>(self: Stream.Stream<number, E, R>): Effect.Effect<number, E, Exclude<R, Scope.Scope>> =>
5900
- pipe(self, run(_sink.sum))
5885
+ export const runSum = <E, R>(self: Stream.Stream<number, E, R>): Effect.Effect<number, E, R> => run(self, _sink.sum)
5901
5886
 
5902
5887
  /** @internal */
5903
5888
  export const scan = dual<
@@ -6085,6 +6070,16 @@ export const scoped = <A, E, R>(
6085
6070
  ): Stream.Stream<A, E, Exclude<R, Scope.Scope>> =>
6086
6071
  new StreamImpl(channel.ensuring(channel.scoped(pipe(effect, Effect.map(Chunk.of))), Effect.void))
6087
6072
 
6073
+ /** @internal */
6074
+ export const scopedWith = <A, E, R>(
6075
+ f: (scope: Scope.Scope) => Effect.Effect<A, E, R>
6076
+ ): Stream.Stream<A, E, R> =>
6077
+ new StreamImpl(channel.scopedWith((scope) =>
6078
+ f(scope).pipe(
6079
+ Effect.map(Chunk.of)
6080
+ )
6081
+ ))
6082
+
6088
6083
  /** @internal */
6089
6084
  export const some = <A, E, R>(self: Stream.Stream<Option.Option<A>, E, R>): Stream.Stream<A, Option.Option<E>, R> =>
6090
6085
  pipe(self, mapError(Option.some), someOrFail(() => Option.none()))
@@ -7279,6 +7274,11 @@ export const unwrapScoped = <A, E2, R2, E, R>(
7279
7274
  effect: Effect.Effect<Stream.Stream<A, E2, R2>, E, R>
7280
7275
  ): Stream.Stream<A, E | E2, Exclude<R, Scope.Scope> | R2> => flatten(scoped(effect))
7281
7276
 
7277
+ /** @internal */
7278
+ export const unwrapScopedWith = <A, E2, R2, E, R>(
7279
+ f: (scope: Scope.Scope) => Effect.Effect<Stream.Stream<A, E2, R2>, E, R>
7280
+ ): Stream.Stream<A, E | E2, R | R2> => flatten(scopedWith((scope) => f(scope)))
7281
+
7282
7282
  /** @internal */
7283
7283
  export const updateService = dual<
7284
7284
  <T extends Context.Tag<any, any>>(