@@ -24,6 +24,10 @@ const {
2424} = primordials ;
2525
2626const { lazyDOMException } = require ( 'internal/util' ) ;
27+ const {
28+ AbortController,
29+ abortSignal,
30+ } = require ( 'internal/abort_controller' ) ;
2731
2832const {
2933 codes : {
@@ -63,6 +67,7 @@ const {
6367 parsePullArgs,
6468 toWriterUint8Array,
6569 validateBatchEntry,
70+ yieldAbortable,
6671} = require ( 'internal/streams/iter/utils' ) ;
6772const {
6873 converters,
@@ -79,6 +84,7 @@ const kAbort = Symbol('kAbort');
7984const kCanWrite = Symbol ( 'kCanWrite' ) ;
8085const kOnBufferDrained = Symbol ( 'kOnBufferDrained' ) ;
8186const kOnEndDrained = Symbol ( 'kOnEndDrained' ) ;
87+ const kOnCancel = Symbol ( 'kOnCancel' ) ;
8288const kPendingWriteRemoved = Symbol ( 'kPendingWriteRemoved' ) ;
8389const kNoBroadcastError = Symbol ( 'kNoBroadcastError' ) ;
8490
@@ -120,6 +126,7 @@ class BroadcastImpl {
120126 this . #options = options ;
121127 this [ kOnBufferDrained ] = null ;
122128 this [ kOnEndDrained ] = null ;
129+ this [ kOnCancel ] = null ;
123130 }
124131
125132 setWriter ( writer ) {
@@ -307,6 +314,9 @@ class BroadcastImpl {
307314 }
308315 this . #consumers. clear ( ) ;
309316 this . #cachedMinCursorConsumers = 0 ;
317+ const onCancel = this [ kOnCancel ] ;
318+ this [ kOnCancel ] = null ;
319+ onCancel ?. ( reason ) ;
310320 }
311321
312322 [ SymbolDispose ] ( ) {
@@ -915,13 +925,23 @@ const Broadcast = {
915925 } ) ;
916926 const result = broadcast ( options ) ;
917927 const { signal } = options ;
928+ const controller = new AbortController ( ) ;
929+ if ( signal ?. aborted ) {
930+ abortSignal ( controller . signal , signal . reason ) ;
931+ }
932+ const onCancel = ( reason ) => {
933+ if ( ! controller . signal . aborted ) {
934+ abortSignal ( controller . signal , reason ) ;
935+ }
936+ } ;
937+ result . broadcast [ kOnCancel ] = onCancel ;
918938
919939 const pump = async ( ) => {
920940 const w = result . writer ;
921941 try {
922942 if ( isAsyncIterable ( source ) ) {
923- for await ( const chunks of source ) {
924- signal ? .throwIfAborted ( ) ;
943+ for await ( const chunks of yieldAbortable ( source , controller . signal ) ) {
944+ controller . signal . throwIfAborted ( ) ;
925945 if ( ArrayIsArray ( chunks ) ) {
926946 if ( ! w . writevSync ( chunks ) ) {
927947 await w . writev ( chunks , signal ? { signal } : undefined ) ;
@@ -932,7 +952,7 @@ const Broadcast = {
932952 }
933953 } else if ( isSyncIterable ( source ) ) {
934954 for ( const chunks of source ) {
935- signal ? .throwIfAborted ( ) ;
955+ controller . signal . throwIfAborted ( ) ;
936956 if ( ArrayIsArray ( chunks ) ) {
937957 if ( ! w . writevSync ( chunks ) ) {
938958 await w . writev ( chunks , signal ? { signal } : undefined ) ;
@@ -946,7 +966,13 @@ const Broadcast = {
946966 await w . end ( signal ? { signal } : undefined ) ;
947967 }
948968 } catch ( error ) {
949- w . fail ( error ) ;
969+ if ( ! controller . signal . aborted ) {
970+ w . fail ( error ) ;
971+ }
972+ } finally {
973+ if ( result . broadcast [ kOnCancel ] === onCancel ) {
974+ result . broadcast [ kOnCancel ] = null ;
975+ }
950976 }
951977 } ;
952978 PromisePrototypeThen ( pump ( ) , undefined , ( ) => { } ) ;
0 commit comments