| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 4df59bc commit 9e38fc6
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1259,6 +1259,17 @@ added: v11.4.0 | |||
| 1259 | 1259 | Is `true` if it is safe to call [`readable.read()`][stream-read], which means | |
| 1260 | 1260 | the stream has not been destroyed or emitted `'error'` or `'end'`. | |
| 1261 | 1261 | ||
| 1262 | + ##### `readable.readableDidRead` | ||
| 1263 | + <!-- YAML | ||
| 1264 | + added: REPLACEME | ||
| 1265 | + --> | ||
| 1266 | + | ||
| 1267 | + * {boolean} | ||
| 1268 | + | ||
| 1269 | + Allows determining if the stream has been or is about to be read. | ||
| 1270 | + Returns true if `'data'`, `'end'`, `'error'` or `'close'` has been | ||
| 1271 | + emitted. | ||
| 1272 | + | ||
| 1262 | 1273 | ##### `readable.readableEncoding` | |
| 1263 | 1274 | <!-- YAML | |
| 1264 | 1275 | added: v12.7.0 | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -167,6 +167,8 @@ function ReadableState(options, stream, isDuplex) { | |||
| 167 | 167 | // If true, a maybeReadMore has been scheduled. | |
| 168 | 168 | this.readingMore = false; | |
| 169 | 169 | ||
| 170 | + this.dataEmitted = false; | ||
| 171 | + | ||
| 170 | 172 | this.decoder = null; | |
| 171 | 173 | this.encoding = null; | |
| 172 | 174 | if (options && options.encoding) { | |
@@ -309,6 +311,7 @@ function addChunk(stream, state, chunk, addToFront) { | |||
| 309 | 311 | } else { | |
| 310 | 312 | state.awaitDrainWriters = null; | |
| 311 | 313 | } | |
| 314 | + state.dataEmitted = true; | ||
| 312 | 315 | stream.emit('data', chunk); | |
| 313 | 316 | } else { | |
| 314 | 317 | // Update the buffer info. | |
@@ -519,8 +522,10 @@ Readable.prototype.read = function(n) { | |||
| 519 | 522 | endReadable(this); | |
| 520 | 523 | } | |
| 521 | 524 | ||
| 522 | - if (ret !== null) | ||
| 525 | + if (ret !== null) { | ||
| 526 | + state.dataEmitted = true; | ||
| 523 | 527 | this.emit('data', ret); | |
| 528 | + } | ||
| 524 | 529 | ||
| 525 | 530 | return ret; | |
| 526 | 531 | }; | |
@@ -1183,6 +1188,18 @@ ObjectDefineProperties(Readable.prototype, { | |||
| 1183 | 1188 | } | |
| 1184 | 1189 | }, | |
| 1185 | 1190 | ||
| 1191 | + readableDidRead: { | ||
| 1192 | + enumerable: false, | ||
| 1193 | + get: function() { | ||
| 1194 | + return ( | ||
| 1195 | + this._readableState.dataEmitted || | ||
| 1196 | + this._readableState.endEmitted || | ||
| 1197 | + this._readableState.errorEmitted || | ||
| 1198 | + this._readableState.closeEmitted | ||
| 1199 | + ); | ||
| 1200 | + } | ||
| 1201 | + }, | ||
| 1202 | + | ||
| 1186 | 1203 | readableHighWaterMark: { | |
| 1187 | 1204 | enumerable: false, | |
| 1188 | 1205 | get: function() { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,104 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + const common = require('../common'); | ||
| 3 | + const assert = require('assert'); | ||
| 4 | + const Readable = require('stream').Readable; | ||
| 5 | + | ||
| 6 | + function noop() {} | ||
| 7 | + | ||
| 8 | + function check(readable, data, fn) { | ||
| 9 | + assert.strictEqual(readable.readableDidRead, false); | ||
| 10 | + if (data === -1) { | ||
| 11 | + readable.on('error', common.mustCall()); | ||
| 12 | + readable.on('data', common.mustNotCall()); | ||
| 13 | + readable.on('end', common.mustNotCall()); | ||
| 14 | + } else { | ||
| 15 | + readable.on('error', common.mustNotCall()); | ||
| 16 | + if (data === -2) { | ||
| 17 | + readable.on('end', common.mustNotCall()); | ||
| 18 | + } else { | ||
| 19 | + readable.on('end', common.mustCall()); | ||
| 20 | + } | ||
| 21 | + if (data > 0) { | ||
| 22 | + readable.on('data', common.mustCallAtLeast(data)); | ||
| 23 | + } else { | ||
| 24 | + readable.on('data', common.mustNotCall()); | ||
| 25 | + } | ||
| 26 | + } | ||
| 27 | + readable.on('close', common.mustCall()); | ||
| 28 | + fn(); | ||
| 29 | + setImmediate(() => { | ||
| 30 | + assert.strictEqual(readable.readableDidRead, true); | ||
| 31 | + }); | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + { | ||
| 35 | + const readable = new Readable({ | ||
| 36 | + read() { | ||
| 37 | + this.push(null); | ||
| 38 | + } | ||
| 39 | + }); | ||
| 40 | + check(readable, 0, () => { | ||
| 41 | + readable.read(); | ||
| 42 | + }); | ||
| 43 | + } | ||
| 44 | + | ||
| 45 | + { | ||
| 46 | + const readable = new Readable({ | ||
| 47 | + read() { | ||
| 48 | + this.push(null); | ||
| 49 | + } | ||
| 50 | + }); | ||
| 51 | + check(readable, 0, () => { | ||
| 52 | + readable.resume(); | ||
| 53 | + }); | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | + { | ||
| 57 | + const readable = new Readable({ | ||
| 58 | + read() { | ||
| 59 | + this.push(null); | ||
| 60 | + } | ||
| 61 | + }); | ||
| 62 | + check(readable, -2, () => { | ||
| 63 | + readable.destroy(); | ||
| 64 | + }); | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + { | ||
| 68 | + const readable = new Readable({ | ||
| 69 | + read() { | ||
| 70 | + this.push(null); | ||
| 71 | + } | ||
| 72 | + }); | ||
| 73 | + | ||
| 74 | + check(readable, -1, () => { | ||
| 75 | + readable.destroy(new Error()); | ||
| 76 | + }); | ||
| 77 | + } | ||
| 78 | + | ||
| 79 | + { | ||
| 80 | + const readable = new Readable({ | ||
| 81 | + read() { | ||
| 82 | + this.push('data'); | ||
| 83 | + this.push(null); | ||
| 84 | + } | ||
| 85 | + }); | ||
| 86 | + | ||
| 87 | + check(readable, 1, () => { | ||
| 88 | + readable.on('data', noop); | ||
| 89 | + }); | ||
| 90 | + } | ||
| 91 | + | ||
| 92 | + { | ||
| 93 | + const readable = new Readable({ | ||
| 94 | + read() { | ||
| 95 | + this.push('data'); | ||
| 96 | + this.push(null); | ||
| 97 | + } | ||
| 98 | + }); | ||
| 99 | + | ||
| 100 | + check(readable, 1, () => { | ||
| 101 | + readable.on('data', noop); | ||
| 102 | + readable.off('data', noop); | ||
| 103 | + }); | ||
| 104 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments