Skip to content

Commit

Permalink
Ready promise caching
Browse files Browse the repository at this point in the history
  • Loading branch information
domenic committed Feb 4, 2015
1 parent a039ead commit d878de6
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
1 change: 1 addition & 0 deletions reference-implementation/lib/exclusive-stream-reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default class ExclusiveStreamReader {
}

stream._readableStreamReader = this;
stream._readyPromiseCache = undefined;

this._encapsulatedReadableStream = stream;

Expand Down
4 changes: 4 additions & 0 deletions reference-implementation/lib/readable-stream-abstract-ops.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export function CallReadableStreamPull(stream) {

export function CloseReadableStream(stream) {
stream._state = 'closed';
stream._readyPromiseCache = undefined;
stream._resolveClosedPromise(undefined);

if (stream._readableStreamReader !== undefined) {
Expand Down Expand Up @@ -106,6 +107,7 @@ export function CreateReadableStreamEnqueueFunction(stream) {

if (stream._state === 'waiting') {
stream._state = 'readable';
stream._readyPromiseCache = undefined;
stream._resolveReadyPromise(undefined);
}

Expand All @@ -126,6 +128,7 @@ export function CreateReadableStreamErrorFunction(stream) {
}
if (stream._state === 'waiting' || stream._state === 'readable') {
stream._state = 'errored';
stream._readyPromiseCache = undefined;
stream._storedError = e;
stream._rejectClosedPromise(e);
if (stream._readableStreamReader !== undefined) {
Expand Down Expand Up @@ -180,6 +183,7 @@ export function ReadFromReadableStream(stream) {
CloseReadableStream(stream);
} else {
stream._state = 'waiting';
stream._readyPromiseCache = undefined;
stream._initReadyPromise();
}
}
Expand Down
15 changes: 10 additions & 5 deletions reference-implementation/lib/readable-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default class ReadableStream {
this._draining = false;
this._pulling = false;
this._readableStreamReader = undefined;
this._readyPromiseCache = undefined;

this._enqueue = CreateReadableStreamEnqueueFunction(this);
this._close = CreateReadableStreamCloseFunction(this);
Expand Down Expand Up @@ -190,15 +191,19 @@ export default class ReadableStream {
return Promise.reject(new TypeError('ReadableStream.prototype.ready can only be used on a ReadableStream'));
}

if (this._readableStreamReader !== undefined) {
return this._readableStreamReader._lockReleased.then(() => this.ready);
if (this._readyPromiseCache !== undefined) {
return this._readyPromiseCache;
}

if (this._state === 'waiting') {
return this._readyPromise.then(() => this.ready);
if (this._readableStreamReader !== undefined) {
this._readyPromiseCache = this._readableStreamReader._lockReleased.then(() => this.ready);
} else if (this._state === 'waiting') {
this._readyPromiseCache = this._readyPromise.then(() => this.ready);
} else {
this._readyPromiseCache = this._readyPromise;
}

return this._readyPromise;
return this._readyPromiseCache;
}

_initReadyPromise() {
Expand Down
17 changes: 17 additions & 0 deletions reference-implementation/test/readable-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -680,3 +680,20 @@ test('ReadableStream should call underlying source methods as methods', t => {

rs.ready.then(() => rs.cancel());
});

test('ReadableStream ready returns the same value when called on a new, empty stream', t => {
var rs = new ReadableStream();
t.equal(rs.ready, rs.ready, 'rs.ready should not change between gets');
t.end();
});

test('ReadableStream ready returns the same value when called on a readable stream', t => {
var rs = new ReadableStream({
start(enqueue) {
enqueue('a');
}
});

t.equal(rs.ready, rs.ready, 'rs.ready should not change between gets');
t.end();
});

0 comments on commit d878de6

Please sign in to comment.