| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 178caa5 commit e2a2a3f
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -681,20 +681,17 @@ Readable.prototype.pipe = function(dest, pipeOpts) { | |||
| 681 | 681 | dest.end(); | |
| 682 | 682 | } | |
| 683 | 683 | ||
| 684 | - // When the dest drains, it reduces the awaitDrain counter | ||
| 685 | - // on the source. This would be more elegant with a .once() | ||
| 686 | - // handler in flow(), but adding and removing repeatedly is | ||
| 687 | - // too slow. | ||
| 688 | - const ondrain = pipeOnDrain(src); | ||
| 689 | - dest.on('drain', ondrain); | ||
| 684 | + let ondrain; | ||
| 690 | 685 | ||
| 691 | 686 | var cleanedUp = false; | |
| 692 | 687 | function cleanup() { | |
| 693 | 688 | debug('cleanup'); | |
| 694 | 689 | // Cleanup event handlers once the pipe is broken | |
| 695 | 690 | dest.removeListener('close', onclose); | |
| 696 | 691 | dest.removeListener('finish', onfinish); | |
| 697 | - dest.removeListener('drain', ondrain); | ||
| 692 | + if (ondrain) { | ||
| 693 | + dest.removeListener('drain', ondrain); | ||
| 694 | + } | ||
| 698 | 695 | dest.removeListener('error', onerror); | |
| 699 | 696 | dest.removeListener('unpipe', onunpipe); | |
| 700 | 697 | src.removeListener('end', onend); | |
@@ -708,7 +705,7 @@ Readable.prototype.pipe = function(dest, pipeOpts) { | |||
| 708 | 705 | // flowing again. | |
| 709 | 706 | // So, if this is awaiting a drain, then we just call it now. | |
| 710 | 707 | // If we don't know, then assume that we are waiting for one. | |
| 711 | - if (state.awaitDrain && | ||
| 708 | + if (ondrain && state.awaitDrain && | ||
| 712 | 709 | (!dest._writableState || dest._writableState.needDrain)) | |
| 713 | 710 | ondrain(); | |
| 714 | 711 | } | |
@@ -729,6 +726,14 @@ Readable.prototype.pipe = function(dest, pipeOpts) { | |||
| 729 | 726 | debug('false write response, pause', state.awaitDrain); | |
| 730 | 727 | state.awaitDrain++; | |
| 731 | 728 | } | |
| 729 | + if (!ondrain) { | ||
| 730 | + // When the dest drains, it reduces the awaitDrain counter | ||
| 731 | + // on the source. This would be more elegant with a .once() | ||
| 732 | + // handler in flow(), but adding and removing repeatedly is | ||
| 733 | + // too slow. | ||
| 734 | + ondrain = pipeOnDrain(src); | ||
| 735 | + dest.on('drain', ondrain); | ||
| 736 | + } | ||
| 732 | 737 | src.pause(); | |
| 733 | 738 | } | |
| 734 | 739 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,6 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | 2 | const common = require('../common'); | |
| 3 | + const assert = require('assert'); | ||
| 3 | 4 | const { Readable, Writable, PassThrough } = require('stream'); | |
| 4 | 5 | ||
| 5 | 6 | { | |
@@ -65,3 +66,25 @@ const { Readable, Writable, PassThrough } = require('stream'); | |||
| 65 | 66 | wrapper.resume(); | |
| 66 | 67 | wrapper.on('end', common.mustCall()); | |
| 67 | 68 | } | |
| 69 | + | ||
| 70 | + { | ||
| 71 | + // Only register drain if there is backpressure. | ||
| 72 | + const rs = new Readable({ read() {} }); | ||
| 73 | + | ||
| 74 | + const pt = rs | ||
| 75 | + .pipe(new PassThrough({ objectMode: true, highWaterMark: 2 })); | ||
| 76 | + assert.strictEqual(pt.listenerCount('drain'), 0); | ||
| 77 | + pt.on('finish', () => { | ||
| 78 | + assert.strictEqual(pt.listenerCount('drain'), 0); | ||
| 79 | + }); | ||
| 80 | + | ||
| 81 | + rs.push('asd'); | ||
| 82 | + assert.strictEqual(pt.listenerCount('drain'), 0); | ||
| 83 | + | ||
| 84 | + process.nextTick(() => { | ||
| 85 | + rs.push('asd'); | ||
| 86 | + assert.strictEqual(pt.listenerCount('drain'), 0); | ||
| 87 | + rs.push(null); | ||
| 88 | + assert.strictEqual(pt.listenerCount('drain'), 0); | ||
| 89 | + }); | ||
| 90 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -52,11 +52,4 @@ function drain() { | |||
| 52 | 52 | ||
| 53 | 53 | w.end = common.mustCall(); | |
| 54 | 54 | ||
| 55 | - // Just for kicks, let's mess with the drain count. | ||
| 56 | - // This verifies that even if it gets negative in the | ||
| 57 | - // pipe() cleanup function, we'll still function properly. | ||
| 58 | - r.on('readable', function() { | ||
| 59 | - w.emit('drain'); | ||
| 60 | - }); | ||
| 61 | - | ||
| 62 | 55 | r.pipe(w); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments