@@ -48,7 +48,7 @@ import * as pull from "./stream/pull.js"
48
48
import * as SinkEndReason from "./stream/sinkEndReason.js"
49
49
import * as ZipAllState from "./stream/zipAllState.js"
50
50
import * as ZipChunksState from "./stream/zipChunksState.js"
51
- import * as _take from "./take.js"
51
+ import * as InternalTake from "./take.js"
52
52
53
53
/** @internal */
54
54
const StreamSymbolKey = "effect/Stream"
@@ -461,13 +461,57 @@ export const as = dual<
461
461
462
462
/** @internal */
463
463
export const _async = < A , E = never , R = never > (
464
- register : ( emit : Emit . Emit < R , E , A , void > ) => void ,
464
+ register : (
465
+ emit : Emit . Emit < R , E , A , void >
466
+ ) => Effect . Effect < void , never , R > | void ,
465
467
outputBuffer = 16
466
468
) : Stream . Stream < A , E , R > =>
467
- asyncOption ( ( cb ) => {
468
- register ( cb )
469
- return Option . none ( )
470
- } , outputBuffer )
469
+ Effect . acquireRelease (
470
+ Queue . bounded < Take . Take < A , E > > ( outputBuffer ) ,
471
+ ( queue ) => Queue . shutdown ( queue )
472
+ ) . pipe (
473
+ Effect . flatMap ( ( output ) =>
474
+ Effect . runtime < R > ( ) . pipe (
475
+ Effect . flatMap ( ( runtime ) =>
476
+ Effect . sync ( ( ) => {
477
+ const runPromiseExit = Runtime . runPromiseExit ( runtime )
478
+ const canceler = register ( emit . make < R , E , A , void > ( ( resume ) =>
479
+ InternalTake . fromPull ( resume ) . pipe (
480
+ Effect . flatMap ( ( take ) => Queue . offer ( output , take ) ) ,
481
+ Effect . asUnit ,
482
+ runPromiseExit
483
+ ) . then ( ( exit ) => {
484
+ if ( Exit . isFailure ( exit ) ) {
485
+ if ( ! Cause . isInterrupted ( exit . cause ) ) {
486
+ throw Cause . squash ( exit . cause )
487
+ }
488
+ }
489
+ } )
490
+ ) )
491
+ return canceler
492
+ } )
493
+ ) ,
494
+ Effect . map ( ( value ) => {
495
+ const loop : Channel . Channel < Chunk . Chunk < A > , unknown , E , unknown , void , unknown > = Queue . take ( output ) . pipe (
496
+ Effect . flatMap ( ( take ) => InternalTake . done ( take ) ) ,
497
+ Effect . match ( {
498
+ onFailure : ( maybeError ) =>
499
+ core . fromEffect ( Queue . shutdown ( output ) ) . pipe (
500
+ channel . zipRight ( Option . match ( maybeError , {
501
+ onNone : ( ) => core . unit ,
502
+ onSome : ( error ) => core . fail ( error )
503
+ } ) )
504
+ ) ,
505
+ onSuccess : ( chunk ) => core . write ( chunk ) . pipe ( core . flatMap ( ( ) => loop ) )
506
+ } ) ,
507
+ channel . unwrap
508
+ )
509
+ return fromChannel ( loop ) . pipe ( ensuring ( value ?? Effect . unit ) )
510
+ } )
511
+ )
512
+ ) ,
513
+ unwrapScoped
514
+ )
471
515
472
516
/** @internal */
473
517
export const asyncEffect = < A , E = never , R = never > (
@@ -487,7 +531,7 @@ export const asyncEffect = <A, E = never, R = never>(
487
531
register (
488
532
emit . make ( ( k ) =>
489
533
pipe (
490
- _take . fromPull ( k ) ,
534
+ InternalTake . fromPull ( k ) ,
491
535
Effect . flatMap ( ( take ) => Queue . offer ( output , take ) ) ,
492
536
Effect . asUnit ,
493
537
Runtime . runPromiseExit ( runtime )
@@ -503,7 +547,7 @@ export const asyncEffect = <A, E = never, R = never>(
503
547
Effect . map ( ( ) => {
504
548
const loop : Channel . Channel < Chunk . Chunk < A > , unknown , E , unknown , void , unknown > = pipe (
505
549
Queue . take ( output ) ,
506
- Effect . flatMap ( _take . done ) ,
550
+ Effect . flatMap ( InternalTake . done ) ,
507
551
Effect . match ( {
508
552
onFailure : ( maybeError ) =>
509
553
pipe (
@@ -524,84 +568,6 @@ export const asyncEffect = <A, E = never, R = never>(
524
568
fromChannel
525
569
)
526
570
527
- /** @internal */
528
- export const asyncInterrupt = < A , E = never , R = never > (
529
- register : (
530
- emit : Emit . Emit < R , E , A , void >
531
- ) => Either . Either < Effect . Effect < unknown , never , R > , Stream . Stream < A , E , R > > ,
532
- outputBuffer = 16
533
- ) : Stream . Stream < A , E , R > =>
534
- pipe (
535
- Effect . acquireRelease (
536
- Queue . bounded < Take . Take < A , E > > ( outputBuffer ) ,
537
- ( queue ) => Queue . shutdown ( queue )
538
- ) ,
539
- Effect . flatMap ( ( output ) =>
540
- pipe (
541
- Effect . runtime < R > ( ) ,
542
- Effect . flatMap ( ( runtime ) =>
543
- pipe (
544
- Effect . sync ( ( ) =>
545
- register (
546
- emit . make ( ( k ) =>
547
- pipe (
548
- _take . fromPull ( k ) ,
549
- Effect . flatMap ( ( take ) => Queue . offer ( output , take ) ) ,
550
- Effect . asUnit ,
551
- Runtime . runPromiseExit ( runtime )
552
- ) . then ( ( exit ) => {
553
- if ( Exit . isFailure ( exit ) ) {
554
- if ( ! Cause . isInterrupted ( exit . cause ) ) {
555
- throw Cause . squash ( exit . cause )
556
- }
557
- }
558
- } )
559
- )
560
- )
561
- ) ,
562
- Effect . map ( Either . match ( {
563
- onLeft : ( canceler ) => {
564
- const loop : Channel . Channel < Chunk . Chunk < A > , unknown , E , unknown , void , unknown > = pipe (
565
- Queue . take ( output ) ,
566
- Effect . flatMap ( _take . done ) ,
567
- Effect . match ( {
568
- onFailure : ( maybeError ) =>
569
- channel . zipRight (
570
- core . fromEffect ( Queue . shutdown ( output ) ) ,
571
- Option . match ( maybeError , {
572
- onNone : ( ) => core . unit ,
573
- onSome : core . fail
574
- } )
575
- ) ,
576
- onSuccess : ( chunk ) => pipe ( core . write ( chunk ) , core . flatMap ( ( ) => loop ) )
577
- } ) ,
578
- channel . unwrap
579
- )
580
- return pipe ( fromChannel ( loop ) , ensuring ( canceler ) )
581
- } ,
582
- onRight : ( stream ) => unwrap ( pipe ( Queue . shutdown ( output ) , Effect . as ( stream ) ) )
583
- } ) )
584
- )
585
- )
586
- )
587
- ) ,
588
- unwrapScoped
589
- )
590
-
591
- /** @internal */
592
- export const asyncOption = < A , E = never , R = never > (
593
- register : ( emit : Emit . Emit < R , E , A , void > ) => Option . Option < Stream . Stream < A , E , R > > ,
594
- outputBuffer = 16
595
- ) : Stream . Stream < A , E , R > =>
596
- asyncInterrupt (
597
- ( emit ) =>
598
- Option . match ( register ( emit ) , {
599
- onNone : ( ) => Either . left ( Effect . unit ) ,
600
- onSome : Either . right
601
- } ) ,
602
- outputBuffer
603
- )
604
-
605
571
/** @internal */
606
572
export const asyncScoped = < A , E = never , R = never > (
607
573
register : ( emit : Emit . Emit < R , E , A , void > ) => Effect . Effect < unknown , E , R | Scope . Scope > ,
@@ -620,7 +586,7 @@ export const asyncScoped = <A, E = never, R = never>(
620
586
register (
621
587
emit . make ( ( k ) =>
622
588
pipe (
623
- _take . fromPull ( k ) ,
589
+ InternalTake . fromPull ( k ) ,
624
590
Effect . flatMap ( ( take ) => Queue . offer ( output , take ) ) ,
625
591
Effect . asUnit ,
626
592
Runtime . runPromiseExit ( runtime )
@@ -642,7 +608,7 @@ export const asyncScoped = <A, E = never, R = never>(
642
608
pull . end ( ) :
643
609
pipe (
644
610
Queue . take ( output ) ,
645
- Effect . flatMap ( _take . done ) ,
611
+ Effect . flatMap ( InternalTake . done ) ,
646
612
Effect . onError ( ( ) =>
647
613
pipe (
648
614
Ref . set ( ref , true ) ,
@@ -884,7 +850,7 @@ export const bufferChunks = dual<
884
850
Effect . map ( queue , ( queue ) => {
885
851
const process : Channel . Channel < Chunk . Chunk < A > , unknown , E , unknown , void , unknown > = pipe (
886
852
core . fromEffect ( Queue . take ( queue ) ) ,
887
- core . flatMap ( _take . match ( {
853
+ core . flatMap ( InternalTake . match ( {
888
854
onEnd : ( ) => core . unit ,
889
855
onFailure : core . failCause ,
890
856
onSuccess : ( value ) => pipe ( core . write ( value ) , core . flatMap ( ( ) => process ) )
@@ -947,7 +913,7 @@ const bufferUnbounded = <A, E, R>(self: Stream.Stream<A, E, R>): Stream.Stream<A
947
913
Effect . map ( queue , ( queue ) => {
948
914
const process : Channel . Channel < Chunk . Chunk < A > , unknown , E , unknown , void , unknown > = pipe (
949
915
core . fromEffect ( Queue . take ( queue ) ) ,
950
- core . flatMap ( _take . match ( {
916
+ core . flatMap ( InternalTake . match ( {
951
917
onEnd : ( ) => core . unit ,
952
918
onFailure : core . failCause ,
953
919
onSuccess : ( value ) => core . flatMap ( core . write ( value ) , ( ) => process )
@@ -989,16 +955,16 @@ const bufferSignal = <A, E, R>(
989
955
Effect . flatMap (
990
956
( deferred ) =>
991
957
pipe (
992
- Queue . offer ( queue , [ _take . chunk ( input ) , deferred ] as const ) ,
958
+ Queue . offer ( queue , [ InternalTake . chunk ( input ) , deferred ] as const ) ,
993
959
Effect . flatMap ( ( added ) => pipe ( Ref . set ( ref , deferred ) , Effect . when ( ( ) => added ) ) )
994
960
)
995
961
) ,
996
962
Effect . asUnit ,
997
963
core . fromEffect ,
998
964
core . flatMap ( ( ) => producer ( queue , ref ) )
999
965
) ,
1000
- onFailure : ( error ) => terminate ( _take . failCause ( error ) ) ,
1001
- onDone : ( ) => terminate ( _take . end )
966
+ onFailure : ( error ) => terminate ( InternalTake . failCause ( error ) ) ,
967
+ onDone : ( ) => terminate ( InternalTake . end )
1002
968
} )
1003
969
}
1004
970
const consumer = (
@@ -1009,7 +975,7 @@ const bufferSignal = <A, E, R>(
1009
975
core . flatMap ( ( [ take , deferred ] ) =>
1010
976
channel . zipRight (
1011
977
core . fromEffect ( Deferred . succeed ( deferred , void 0 ) ) ,
1012
- _take . match ( take , {
978
+ InternalTake . match ( take , {
1013
979
onEnd : ( ) => core . unit ,
1014
980
onFailure : core . failCause ,
1015
981
onSuccess : ( value ) => pipe ( core . write ( value ) , core . flatMap ( ( ) => process ) )
@@ -1469,19 +1435,19 @@ export const combineChunks = dual<
1469
1435
core . flatMap (
1470
1436
core . fromEffect ( pipe (
1471
1437
handoff ,
1472
- Handoff . offer < Take . Take < Elem , Err > > ( _take . chunk ( input ) )
1438
+ Handoff . offer < Take . Take < Elem , Err > > ( InternalTake . chunk ( input ) )
1473
1439
) ) ,
1474
1440
( ) => producer ( handoff , latch )
1475
1441
) ,
1476
1442
onFailure : ( cause ) =>
1477
1443
core . fromEffect (
1478
1444
Handoff . offer < Take . Take < Elem , Err > > (
1479
1445
handoff ,
1480
- _take . failCause ( cause )
1446
+ InternalTake . failCause ( cause )
1481
1447
)
1482
1448
) ,
1483
1449
onDone : ( ) : Channel . Channel < never , Chunk . Chunk < Elem > , never , Err , unknown , unknown , R > =>
1484
- core . fromEffect ( Handoff . offer < Take . Take < Elem , Err > > ( handoff , _take . end ) )
1450
+ core . fromEffect ( Handoff . offer < Take . Take < Elem , Err > > ( handoff , InternalTake . end ) )
1485
1451
} )
1486
1452
)
1487
1453
return new StreamImpl (
@@ -1515,7 +1481,7 @@ export const combineChunks = dual<
1515
1481
Effect . zipRight (
1516
1482
pipe (
1517
1483
Handoff . take ( left ) ,
1518
- Effect . flatMap ( _take . done )
1484
+ Effect . flatMap ( InternalTake . done )
1519
1485
)
1520
1486
)
1521
1487
)
@@ -1525,7 +1491,7 @@ export const combineChunks = dual<
1525
1491
Effect . zipRight (
1526
1492
pipe (
1527
1493
Handoff . take ( right ) ,
1528
- Effect . flatMap ( _take . done )
1494
+ Effect . flatMap ( InternalTake . done )
1529
1495
)
1530
1496
)
1531
1497
)
@@ -3388,20 +3354,20 @@ export const interleaveWith = dual<
3388
3354
onInput : ( value : A | A2 ) =>
3389
3355
core . flatMap (
3390
3356
core . fromEffect (
3391
- Handoff . offer < Take . Take < A | A2 , E | E2 | E3 > > ( handoff , _take . of ( value ) )
3357
+ Handoff . offer < Take . Take < A | A2 , E | E2 | E3 > > ( handoff , InternalTake . of ( value ) )
3392
3358
) ,
3393
3359
( ) => producer ( handoff )
3394
3360
) ,
3395
3361
onFailure : ( cause ) =>
3396
3362
core . fromEffect (
3397
3363
Handoff . offer < Take . Take < A | A2 , E | E2 | E3 > > (
3398
3364
handoff ,
3399
- _take . failCause ( cause )
3365
+ InternalTake . failCause ( cause )
3400
3366
)
3401
3367
) ,
3402
3368
onDone : ( ) =>
3403
3369
core . fromEffect (
3404
- Handoff . offer < Take . Take < A | A2 , E | E2 | E3 > > ( handoff , _take . end )
3370
+ Handoff . offer < Take . Take < A | A2 , E | E2 | E3 > > ( handoff , InternalTake . end )
3405
3371
)
3406
3372
} )
3407
3373
return new StreamImpl (
@@ -3437,7 +3403,7 @@ export const interleaveWith = dual<
3437
3403
if ( bool && ! leftDone ) {
3438
3404
return pipe (
3439
3405
core . fromEffect ( Handoff . take ( left ) ) ,
3440
- core . flatMap ( _take . match ( {
3406
+ core . flatMap ( InternalTake . match ( {
3441
3407
onEnd : ( ) => rightDone ? core . unit : process ( true , rightDone ) ,
3442
3408
onFailure : core . failCause ,
3443
3409
onSuccess : ( chunk ) => pipe ( core . write ( chunk ) , core . flatMap ( ( ) => process ( leftDone , rightDone ) ) )
@@ -3447,7 +3413,7 @@ export const interleaveWith = dual<
3447
3413
if ( ! bool && ! rightDone ) {
3448
3414
return pipe (
3449
3415
core . fromEffect ( Handoff . take ( right ) ) ,
3450
- core . flatMap ( _take . match ( {
3416
+ core . flatMap ( InternalTake . match ( {
3451
3417
onEnd : ( ) => leftDone ? core . unit : process ( leftDone , true ) ,
3452
3418
onFailure : core . failCause ,
3453
3419
onSuccess : ( chunk ) => pipe ( core . write ( chunk ) , core . flatMap ( ( ) => process ( leftDone , rightDone ) ) )
@@ -5450,9 +5416,9 @@ export const runIntoQueueScoped = dual<
5450
5416
) : Effect . Effect < void , never , Scope . Scope | R > => {
5451
5417
const writer : Channel . Channel < Take . Take < A , E > , Chunk . Chunk < A > , never , E , unknown , unknown , R > = core
5452
5418
. readWithCause ( {
5453
- onInput : ( input : Chunk . Chunk < A > ) => core . flatMap ( core . write ( _take . chunk ( input ) ) , ( ) => writer ) ,
5454
- onFailure : ( cause ) => core . write ( _take . failCause ( cause ) ) ,
5455
- onDone : ( ) => core . write ( _take . end )
5419
+ onInput : ( input : Chunk . Chunk < A > ) => core . flatMap ( core . write ( InternalTake . chunk ( input ) ) , ( ) => writer ) ,
5420
+ onFailure : ( cause ) => core . write ( InternalTake . failCause ( cause ) ) ,
5421
+ onDone : ( ) => core . write ( InternalTake . end )
5456
5422
} )
5457
5423
return pipe (
5458
5424
core . pipeTo ( toChannel ( self ) , writer ) ,
@@ -6229,23 +6195,23 @@ export const tapSink = dual<
6229
6195
. readWithCause ( {
6230
6196
onInput : ( chunk : Chunk . Chunk < A > ) =>
6231
6197
pipe (
6232
- core . fromEffect ( Queue . offer ( queue , _take . chunk ( chunk ) ) ) ,
6198
+ core . fromEffect ( Queue . offer ( queue , InternalTake . chunk ( chunk ) ) ) ,
6233
6199
core . foldCauseChannel ( {
6234
6200
onFailure : ( ) => core . flatMap ( core . write ( chunk ) , ( ) => channel . identityChannel ( ) ) ,
6235
6201
onSuccess : ( ) => core . flatMap ( core . write ( chunk ) , ( ) => loop )
6236
6202
} )
6237
6203
) as Channel . Channel < Chunk . Chunk < A > , Chunk . Chunk < A > , E | E2 , E , unknown , unknown , R2 > ,
6238
6204
onFailure : ( cause : Cause . Cause < E | E2 > ) =>
6239
6205
pipe (
6240
- core . fromEffect ( Queue . offer ( queue , _take . failCause ( cause ) ) ) ,
6206
+ core . fromEffect ( Queue . offer ( queue , InternalTake . failCause ( cause ) ) ) ,
6241
6207
core . foldCauseChannel ( {
6242
6208
onFailure : ( ) => core . failCause ( cause ) ,
6243
6209
onSuccess : ( ) => core . failCause ( cause )
6244
6210
} )
6245
6211
) ,
6246
6212
onDone : ( ) =>
6247
6213
pipe (
6248
- core . fromEffect ( Queue . offer ( queue , _take . end ) ) ,
6214
+ core . fromEffect ( Queue . offer ( queue , InternalTake . end ) ) ,
6249
6215
core . foldCauseChannel ( {
6250
6216
onFailure : ( ) => core . unit ,
6251
6217
onSuccess : ( ) => core . unit
@@ -6256,7 +6222,7 @@ export const tapSink = dual<
6256
6222
new StreamImpl ( pipe (
6257
6223
core . pipeTo ( toChannel ( self ) , loop ) ,
6258
6224
channel . ensuring ( Effect . zipRight (
6259
- Effect . forkDaemon ( Queue . offer ( queue , _take . end ) ) ,
6225
+ Effect . forkDaemon ( Queue . offer ( queue , InternalTake . end ) ) ,
6260
6226
Deferred . await ( deferred )
6261
6227
) )
6262
6228
) ) ,
0 commit comments