Skip to content

Commit

Permalink
add isDisturbed helper
Browse files Browse the repository at this point in the history
  • Loading branch information
mafintosh committed Dec 3, 2024
1 parent f2b304f commit c3a778c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ const WRITE_BACKPRESSURE_STATUS = WRITE_UNDRAINED | DESTROY_STATUS | WRITE_FINIS
const WRITE_UPDATE_SYNC_STATUS = WRITE_UPDATING | OPEN_STATUS | WRITE_NEXT_TICK | WRITE_PRIMARY
const WRITE_DROP_DATA = WRITE_FINISHING | WRITE_DONE | DESTROY_STATUS

const DISTURBED_STATUS = OPEN_STATUS | READ_RESUMED_READ_AHEAD | WRITE_QUEUED

const asyncIterator = Symbol.asyncIterator || Symbol('asyncIterator')

class WritableState {
Expand Down Expand Up @@ -1134,6 +1136,10 @@ function isReadStreamx (stream) {
return isStreamx(stream) && stream.readable
}

function isDisturbed (stream) {
return (stream._duplexState & DISTURBED_STATUS) !== OPENING
}

function isTypedArray (data) {
return typeof data === 'object' && data !== null && typeof data.byteLength === 'number'
}
Expand All @@ -1157,6 +1163,7 @@ module.exports = {
pipelinePromise,
isStream,
isStreamx,
isDisturbed,
isEnded,
isFinished,
getStreamError,
Expand Down
12 changes: 11 additions & 1 deletion test/readable.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const test = require('brittle')
const b4a = require('b4a')
const { Readable } = require('../')
const { Readable, isDisturbed } = require('../')

test('ondata', function (t) {
t.plan(4)
Expand Down Expand Up @@ -403,6 +403,16 @@ test('setEncoding empty string', async function (t) {
}
})

test('is disturbed', function (t) {
const r = new Readable()

t.not(isDisturbed(r))

r.resume()

t.ok(isDisturbed(r))
})

function nextImmediate () {
return new Promise(resolve => setImmediate(resolve))
}

0 comments on commit c3a778c

Please sign in to comment.