@@ -27,10 +27,11 @@ const BUFFERSHIFT = Symbol('bufferShift')
27
27
const OBJECTMODE = Symbol ( 'objectMode' )
28
28
const DESTROYED = Symbol ( 'destroyed' )
29
29
const EMITDATA = Symbol ( 'emitData' )
30
- const EMITEND = Symbol ( 'emitData' )
30
+ const EMITEND = Symbol ( 'emitEnd' )
31
+ const EMITEND2 = Symbol ( 'emitEnd2' )
31
32
const ASYNC = Symbol ( 'async' )
32
33
33
- const defer = fn => process . nextTick ( fn )
34
+ const defer = fn => Promise . resolve ( ) . then ( fn )
34
35
35
36
// TODO remove when Node v8 support drops
36
37
const doIter = global . _MP_NO_ITERATOR_SYMBOLS_ !== '1'
@@ -427,10 +428,7 @@ module.exports = class Minipass extends Stream {
427
428
: this [ ASYNC ] ? defer ( ( ) => this [ EMITDATA ] ( data ) )
428
429
: this [ EMITDATA ] ( data )
429
430
} else if ( ev === 'end' ) {
430
- // only actual end gets this treatment
431
- return this [ EMITTED_END ] ? false
432
- : this [ ASYNC ] ? defer ( ( ) => this [ EMITEND ] ( ) )
433
- : this [ EMITEND ] ( )
431
+ return this [ EMITEND ] ( )
434
432
} else if ( ev === 'close' ) {
435
433
this [ CLOSED ] = true
436
434
// don't emit close before 'end' and 'finish'
@@ -471,9 +469,18 @@ module.exports = class Minipass extends Stream {
471
469
}
472
470
473
471
[ EMITEND ] ( ) {
472
+ if ( this [ EMITTED_END ] )
473
+ return
474
+
474
475
this [ EMITTED_END ] = true
475
476
this . readable = false
477
+ if ( this [ ASYNC ] )
478
+ defer ( ( ) => this [ EMITEND2 ] ( ) )
479
+ else
480
+ this [ EMITEND2 ] ( )
481
+ }
476
482
483
+ [ EMITEND2 ] ( ) {
477
484
if ( this [ DECODER ] ) {
478
485
const data = this [ DECODER ] . end ( )
479
486
if ( data ) {
0 commit comments