Skip to content

Commit

Permalink
Update with code review from @yutakahirano
Browse files Browse the repository at this point in the history
  • Loading branch information
domenic committed Dec 15, 2014
1 parent 2bc75e9 commit b2d4194
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
4 changes: 3 additions & 1 deletion index.bs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ based the total size of all chunks in the stream's internal queue.

<!-- TODO: writable streams too, probably -->

A <dfn>exclusive stream reader</dfn> or simply reader is an object that encapsulates a <a>readable stream</a>,
An <dfn>exclusive stream reader</dfn> or simply reader is an object that encapsulates a <a>readable stream</a>,
preventing access to the stream except through the reader's interface. We say in this case the stream is
<dfn title="locked to a reader">locked to the reader</dfn>, and that the reader is
<dfn title="active reader">active</dfn>.
Expand Down Expand Up @@ -626,6 +626,8 @@ Instances of <code>ReadableStream</code> are created with the internal slots des
</div>

<ol>
<li> If <b>this</b>@\[[state]] is <code>"closed"</code>, throw a <b>TypeError</b> exception.
<li> If <b>this</b>@\[[state]] is <code>"errored"</code>, throw <b>this</b>@\[[storedError]].
<li> Return Construct(<code>ExclusiveStreamReader</code>, (<b>this</b>)).
</ol>

Expand Down
7 changes: 7 additions & 0 deletions reference-implementation/lib/readable-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ export default class ReadableStream {
}

getReader() {
if (this._state === 'closed') {
throw new TypeError('The stream has already been closed, so a reader cannot be acquired.');
}
if (this._state === 'errored') {
throw this._storedError;
}

return new ExclusiveStreamReader(this);
}

Expand Down
34 changes: 34 additions & 0 deletions reference-implementation/test/exclusive-stream-reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,40 @@ test('cancel() on a reader calls this.releaseLock directly instead of cheating',
reader.cancel();
});

test('getReader() on a closed stream should fail', t => {
var rs = new ReadableStream({
start(enqueue, close) {
close();
}
});

t.equal(rs.state, 'closed', 'the stream should be closed');
t.throws(() => rs.getReader(), /TypeError/, 'getReader() threw a TypeError');
t.end();
});

test('getReader() on a cancelled stream should fail (since cancelling closes)', t => {
var rs = new ReadableStream();
rs.cancel(new Error('fun time is over'));

t.equal(rs.state, 'closed', 'the stream should be closed');
t.throws(() => rs.getReader(), /TypeError/, 'getReader() threw a TypeError');
t.end();
});

test('getReader() on an errored stream should rethrow the error', t => {
var theError = new Error('don\'t say i didn\'t warn ya');
var rs = new ReadableStream({
start(enqueue, close, error) {
error(theError);
}
});

t.equal(rs.state, 'errored', 'the stream should be errored');
t.throws(() => rs.getReader(), /don't say i didn't warn ya/, 'getReader() threw the error');
t.end();
});

// TODO: test that you can read(), get reader and read() from it, release, read() from stream, get another reader and
// read() from it, release, read() from stream.

Expand Down

0 comments on commit b2d4194

Please sign in to comment.