Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ReadableStreamDefaultControllerHasBackpressure operation and use it in TransformStream #767

Merged
merged 4 commits into from
Aug 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions index.bs
Original file line number Diff line number Diff line change
Expand Up @@ -1770,6 +1770,18 @@ Specifications should <em>not</em> use this on streams they did not create.
1. Return _controller_.[[strategyHWM]] − _controller_.[[queueTotalSize]].
</emu-alg>

<h4 id="rs-default-controller-has-backpressure" aoid="ReadableStreamDefaultControllerHasBackpressure"
nothrow>ReadableStreamDefaultControllerHasBackpressure ( <var>controller</var> )</h4>

<div class="note">
This method is used in the implementation of TransformStream.
</div>

<emu-alg>
1. If ! ReadableStreamDefaultControllerShouldCallPull(_controller_) is *true*, return *false*.
1. Otherwise, return *true*.
</emu-alg>

<h3 id="rbs-controller-class" interface lt="ReadableByteStreamController">Class
<code>ReadableByteStreamController</code></h3>

Expand Down
12 changes: 11 additions & 1 deletion reference-implementation/lib/readable-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,8 @@ module.exports = {
ReadableStreamDefaultControllerClose,
ReadableStreamDefaultControllerEnqueue,
ReadableStreamDefaultControllerError,
ReadableStreamDefaultControllerGetDesiredSize
ReadableStreamDefaultControllerGetDesiredSize,
ReadableStreamDefaultControllerHasBackpressure
};

// Abstract operations for the ReadableStream.
Expand Down Expand Up @@ -1141,6 +1142,15 @@ function ReadableStreamDefaultControllerGetDesiredSize(controller) {
return controller._strategyHWM - controller._queueTotalSize;
}

// This is used in the implementation of TransformStream.
function ReadableStreamDefaultControllerHasBackpressure(controller) {
if (ReadableStreamDefaultControllerShouldCallPull(controller) === true) {
return false;
}

return true;
}

class ReadableStreamBYOBRequest {
constructor(controller, view) {
this._associatedReadableByteStreamController = controller;
Expand Down
36 changes: 7 additions & 29 deletions reference-implementation/lib/transform-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ const assert = require('assert');
const { InvokeOrNoop, PromiseInvokeOrPerformFallback, PromiseInvokeOrNoop, typeIsObject } = require('./helpers.js');
const { ReadableStream, ReadableStreamDefaultControllerClose,
ReadableStreamDefaultControllerEnqueue, ReadableStreamDefaultControllerError,
ReadableStreamDefaultControllerGetDesiredSize } = require('./readable-stream.js');
ReadableStreamDefaultControllerGetDesiredSize,
ReadableStreamDefaultControllerHasBackpressure } = require('./readable-stream.js');
const { WritableStream, WritableStreamDefaultControllerError } = require('./writable-stream.js');

// Class TransformStream
Expand Down Expand Up @@ -45,10 +46,7 @@ class TransformStream {
assert(this._writableController !== undefined);
assert(this._readableController !== undefined);

const desiredSize = ReadableStreamDefaultControllerGetDesiredSize(this._readableController);
// Set _backpressure based on desiredSize. As there is no read() at this point, we can just interpret
// desiredSize being non-positive as backpressure.
TransformStreamSetBackpressure(this, desiredSize <= 0);
TransformStreamSetBackpressure(this, true);

const transformStream = this;
const startResult = InvokeOrNoop(transformer, 'start',
Expand Down Expand Up @@ -154,16 +152,9 @@ function TransformStreamEnqueueToReadable(transformStream, chunk) {
throw transformStream._storedError;
}

const desiredSize = ReadableStreamDefaultControllerGetDesiredSize(controller);
const maybeBackpressure = desiredSize <= 0;

if (maybeBackpressure === true && transformStream._backpressure === false) {
// This allows pull() again. When desiredSize is 0, it's possible that a pull() will happen immediately (but
// asynchronously) after this because of pending read()s and set _backpressure back to false.
//
// If pull() could be called from inside enqueue(), then this logic would be wrong. This cannot happen
// because there is always a promise pending from start() or pull() when _backpressure is false.
TransformStreamSetBackpressure(transformStream, true);
const backpressure = ReadableStreamDefaultControllerHasBackpressure(controller);
if (backpressure !== transformStream._backpressure) {
TransformStreamSetBackpressure(transformStream, backpressure);
}
}

Expand Down Expand Up @@ -400,20 +391,7 @@ class TransformStreamDefaultSource {

transformStream._readableController = c;

return this._startPromise.then(() => {
// Prevent the first pull() call until there is backpressure.

assert(transformStream._backpressureChangePromise !== undefined,
'_backpressureChangePromise should have been initialized');

if (transformStream._backpressure === true) {
return Promise.resolve();
}

assert(transformStream._backpressure === false, '_backpressure should have been initialized');

return transformStream._backpressureChangePromise;
});
return this._startPromise;
}

pull() {
Expand Down