Skip to content

Commit

Permalink
Add some extra asserts to TransformStream to clarify invariants
Browse files Browse the repository at this point in the history
It's a subtle point that TransformStreamEnqueueToReadable only ever
changes _backpressure to true, so add an assert for it.

The fact that WritableStream only ever calls sink write() when the state
is "writable" is important to the correct functioning of TransformStream,
so add an assert for that too. Also assert that
_backpressureChangePromise has been initialised in write(), for
consistency with pull().

Also a minor comment fix to TransformStreamError.

No functional changes.
  • Loading branch information
ricea authored and domenic committed Sep 26, 2017
1 parent d5dbb63 commit 567d320
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions reference-implementation/lib/transform-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ function TransformStreamEnqueueToReadable(stream, chunk) {

const backpressure = ReadableStreamDefaultControllerHasBackpressure(controller);
if (backpressure !== stream._backpressure) {
TransformStreamSetBackpressure(stream, backpressure);
assert(backpressure === true, 'backpressure is *true*');
TransformStreamSetBackpressure(stream, true);
}
}

Expand All @@ -131,9 +132,9 @@ function TransformStreamError(stream, e) {
ReadableStreamDefaultControllerError(stream._readable._readableStreamController, e);
}
if (stream._backpressure === true) {
// Pretend that pull() was called to permit any pending write() or start() calls to complete.
// TransformStreamSetBackpressure() cannot be called from enqueue() or pull() once the ReadableStream is errored,
// so this will will be the final time _backpressure is set.
// Pretend that pull() was called to permit any pending write() calls to complete. TransformStreamSetBackpressure()
// cannot be called from enqueue() or pull() once the ReadableStream is errored, so this will will be the final time
// _backpressure is set.
TransformStreamSetBackpressure(stream, false);
}
}
Expand Down Expand Up @@ -277,8 +278,11 @@ class TransformStreamDefaultSink {
// console.log('TransformStreamDefaultSink.write()');

const stream = this._ownerTransformStream;
assert(stream._writable._state === 'writable', 'stream.[[writable]].[[state]] is `"writable"`');

if (stream._backpressure === true) {
assert(stream._backpressureChangePromise !== undefined,
'_backpressureChangePromise should have been initialized');
return stream._backpressureChangePromise
.then(() => {
const writable = stream._writable;
Expand Down

0 comments on commit 567d320

Please sign in to comment.