-
Notifications
You must be signed in to change notification settings - Fork 161
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
WIP: exclusive reader interface #251
Conversation
07e7bfd
to
b261b34
Compare
Implements option 3 of #251. Now the tests are passing since ReadableByteStream can participate in locking as well.
Implemented option 3 in a follow-up commit. Next up: new tests, then spec. |
Added some tests which revealed some issues; see commit message. I'm starting to feel like I may have dug myself into a hole with option 3 and I want to go back to e.g. option 2. |
} | ||
|
||
get ready() { | ||
EnsureStreamReaderIsExclusive(this); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function throws when the stream is not locked by this
, right?
I think returning a rejected Promise is good, as written in https://github.com/domenic/promises-unwrapping/blob/master/docs/writing-specifications-with-promises.md#promise-returning-functions-should-never-throw.
ditto for .closed
and cancel()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, thanks for catching
I emailed Allen (ES6 spec editor; purposefully not @-mentioning him on GitHub to avoid spamming his email) about whether 1 would be acceptable and he said yes. So, phew, I'm just going to do that. |
Right now I am struggling with what to do when the stream closes and/or whether the reader should stop working after the lock is released.
|
Fixes @yutakahirano's comment at #251 (comment).
603463c
to
0699244
Compare
Implements option 3 of #251. Now the tests are passing since ReadableByteStream can participate in locking as well.
Fixes @yutakahirano's comment at #251 (comment).
Tests are looking OK; suggestions for other things to test welcome. I went with not automatically releasing the lock when the stream closes for now, but thoughts welcome. On to the spec work I think. |
a03bc1b
to
5155a0d
Compare
Skeleton of spec work previewable at https://streams.spec.whatwg.org/branch-snapshots/exclusive-reader/. (Had to fix the deploy process, eek.) A few interesting spots: |
f27b786
to
2bc75e9
Compare
Implements option 3 of #251. Now the tests are passing since ReadableByteStream can participate in locking as well.
Fixes @yutakahirano's comment at #251 (comment).
|
||
<!-- TODO: writable streams too, probably --> | ||
|
||
A <dfn>exclusive stream reader</dfn> or simply reader is an object that encapsulates a <a>readable stream</a>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An exclusive stream reader
a80496e
to
3c4ece3
Compare
OK, I updated to a design where closing or erroring the stream auto-releases the lock. Also, when the lock is released, This allows us to make I added the TODO'ed tests, and updated the sections of the spec that I'd already written. I'll flesh out the missing parts of the spec next. |
3c4ece3
to
d0b6d73
Compare
lgtm. Thanks! |
d0b6d73
to
0186f25
Compare
Closes #241. Adds the new getReader() method to readable streams, which returns an ExclusiveStreamReader instance that can be used to read from and observe the stream while denying anyone else the ability to read from or observe it. The implementation of pipeTo makes use of this, and other constructs like the the consume-body algorithm of Fetch are anticipated to make use of this (see e.g. https://github.com/yutakahirano/fetch-with-streams/).
0186f25
to
63372f5
Compare
A start at solving #241. /cc @yutakahirano.
Didn't quite get as far as I hoped in terms of spec changes, but got a design doc and reference implementation up. The reference implementation passes all tests except for one, which I will talk about next. There are a lot of tests that need to be written, although the modifications I had to make to existing ones show that this is basically working as desired. The design doc also includes notes on how to optimize.
The test that is failing is the test of using
pipeTo
on ReadableByteStream. This is failing because we only allownew ExclusiveStreamReader(s)
to work ifs
is a ReadableStream. We do that by checking the existence of the [[reader]] internal slot, which ReadableStreams have but ReadableByteStreams do not.There are three ways I see of addressing this:
new ExclusiveStreamReader(stream)
tostream.getReader()
, which would be specced as essentiallyreturn new ExclusiveStreamReader(this, { getReader: () => this@[[reader]], setReader: v => this@[[reader]] = v })
. People creating custom readable stream implementations would then implement their owngetReader()
method that does e.g.return new ExclusiveStreamReader(stream, { getReader: () => this._reader, setReader: v => this._reader = v })
or uses a weak set or some other mechanism of their choosing. This seems a bit more complicated for implementers, although it's presumably possible to optimize and simplify. E.g. maybe the built-ingetReader()
doesn't actually have to callnew ExclusiveStreamReader(...)
; it can just set up a version of the class that is implemented with explicit ties to its progenitor. Referencing the design doc, it gives level 1 developers a slightly different interface, and allows level 3 developers to get locking without reimplementing the entire thing themselves.I will sleep on this a bit. In the meantime thoughts on choosing an approach, as well as code review on the existing stuff, much appreciated.