Skip to content

Commit c3a778c

Browse files
committed
add isDisturbed helper
1 parent f2b304f commit c3a778c

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

index.js

+7
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ const WRITE_BACKPRESSURE_STATUS = WRITE_UNDRAINED | DESTROY_STATUS | WRITE_FINIS
112112
const WRITE_UPDATE_SYNC_STATUS = WRITE_UPDATING | OPEN_STATUS | WRITE_NEXT_TICK | WRITE_PRIMARY
113113
const WRITE_DROP_DATA = WRITE_FINISHING | WRITE_DONE | DESTROY_STATUS
114114

115+
const DISTURBED_STATUS = OPEN_STATUS | READ_RESUMED_READ_AHEAD | WRITE_QUEUED
116+
115117
const asyncIterator = Symbol.asyncIterator || Symbol('asyncIterator')
116118

117119
class WritableState {
@@ -1134,6 +1136,10 @@ function isReadStreamx (stream) {
11341136
return isStreamx(stream) && stream.readable
11351137
}
11361138

1139+
function isDisturbed (stream) {
1140+
return (stream._duplexState & DISTURBED_STATUS) !== OPENING
1141+
}
1142+
11371143
function isTypedArray (data) {
11381144
return typeof data === 'object' && data !== null && typeof data.byteLength === 'number'
11391145
}
@@ -1157,6 +1163,7 @@ module.exports = {
11571163
pipelinePromise,
11581164
isStream,
11591165
isStreamx,
1166+
isDisturbed,
11601167
isEnded,
11611168
isFinished,
11621169
getStreamError,

test/readable.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const test = require('brittle')
22
const b4a = require('b4a')
3-
const { Readable } = require('../')
3+
const { Readable, isDisturbed } = require('../')
44

55
test('ondata', function (t) {
66
t.plan(4)
@@ -403,6 +403,16 @@ test('setEncoding empty string', async function (t) {
403403
}
404404
})
405405

406+
test('is disturbed', function (t) {
407+
const r = new Readable()
408+
409+
t.not(isDisturbed(r))
410+
411+
r.resume()
412+
413+
t.ok(isDisturbed(r))
414+
})
415+
406416
function nextImmediate () {
407417
return new Promise(resolve => setImmediate(resolve))
408418
}

0 commit comments

Comments
 (0)