Skip to content

Commit

Permalink
Editorial: remove forAuthorCode and use dictionaries for read()
Browse files Browse the repository at this point in the history
  • Loading branch information
domenic authored and yutakahirano committed Jun 3, 2021
1 parent 833008d commit 8eb41f7
Show file tree
Hide file tree
Showing 12 changed files with 328 additions and 307 deletions.
358 changes: 188 additions & 170 deletions index.bs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';
const assert = require('assert');

const { promiseResolvedWith, promiseRejectedWith } = require('./helpers/webidl.js');
const { CancelSteps, PullSteps } = require('./abstract-ops/internal-methods.js');
const { ResetQueue } = require('./abstract-ops/queue-with-sizes.js');
const aos = require('./abstract-ops/readable-streams.js');
Expand Down Expand Up @@ -79,7 +78,7 @@ exports.implementation = class ReadableByteStreamControllerImpl {
return result;
}

[PullSteps]() {
[PullSteps](readRequest) {
const stream = this._controlledReadableStream;
assert(aos.ReadableStreamHasDefaultReader(stream) === true);

Expand All @@ -93,7 +92,8 @@ exports.implementation = class ReadableByteStreamControllerImpl {

const view = new Uint8Array(entry.buffer, entry.byteOffset, entry.byteLength);

return promiseResolvedWith(aos.ReadableStreamCreateReadResult(view, false, stream._reader._forAuthorCode));
readRequest.chunkSteps(view);
return;
}

const autoAllocateChunkSize = this._autoAllocateChunkSize;
Expand All @@ -102,7 +102,8 @@ exports.implementation = class ReadableByteStreamControllerImpl {
try {
buffer = new ArrayBuffer(autoAllocateChunkSize);
} catch (bufferE) {
return promiseRejectedWith(bufferE);
readRequest.errorSteps(bufferE);
return;
}

const pullIntoDescriptor = {
Expand All @@ -118,10 +119,7 @@ exports.implementation = class ReadableByteStreamControllerImpl {
this._pendingPullIntos.push(pullIntoDescriptor);
}

const promise = aos.ReadableStreamAddReadRequest(stream);

aos.ReadableStreamAddReadRequest(stream, readRequest);
aos.ReadableByteStreamControllerCallPullIfNeeded(this);

return promise;
}
};
38 changes: 15 additions & 23 deletions reference-implementation/lib/ReadableStream-impl.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
'use strict';
const assert = require('assert');

const { promiseResolvedWith, promiseRejectedWith, setPromiseIsHandledToTrue, transformPromiseWith } =
require('./helpers/webidl.js');
const { typeIsObject } = require('./helpers/miscellaneous.js');
const { newPromise, resolvePromise, rejectPromise, promiseResolvedWith, promiseRejectedWith,
setPromiseIsHandledToTrue } = require('./helpers/webidl.js');
const { ExtractHighWaterMark, ExtractSizeAlgorithm } = require('./abstract-ops/queuing-strategy.js');
const aos = require('./abstract-ops/readable-streams.js');
const wsAOs = require('./abstract-ops/writable-streams.js');
Expand Down Expand Up @@ -53,11 +52,11 @@ exports.implementation = class ReadableStreamImpl {

getReader(options) {
if (!('mode' in options)) {
return aos.AcquireReadableStreamDefaultReader(this, true);
return aos.AcquireReadableStreamDefaultReader(this);
}

assert(options.mode === 'byob');
return aos.AcquireReadableStreamBYOBReader(this, true);
return aos.AcquireReadableStreamBYOBReader(this);
}

pipeThrough(transform, options) {
Expand Down Expand Up @@ -126,27 +125,20 @@ exports.implementation = class ReadableStreamImpl {
);
}

return transformPromiseWith(
aos.ReadableStreamDefaultReaderRead(reader),
result => {
assert(typeIsObject(result));

const { done } = result;
assert(typeof done === 'boolean');

if (done === true) {
aos.ReadableStreamReaderGenericRelease(reader);
return idlUtils.asyncIteratorEOI;
}

const { value } = result;
return value;
const promise = newPromise();
const readRequest = {
chunkSteps: chunk => resolvePromise(promise, chunk),
closeSteps: () => {
aos.ReadableStreamReaderGenericRelease(reader);
resolvePromise(promise, idlUtils.asyncIteratorEOI);
},
reason => {
errorSteps: e => {
aos.ReadableStreamReaderGenericRelease(reader);
throw reason;
rejectPromise(promise, e);
}
);
};
aos.ReadableStreamDefaultReaderRead(reader, readRequest);
return promise;
}

[idlUtils.asyncIteratorReturn](iterator, arg) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dictionary ReadableStreamBYOBReadResult {
ArrayBufferView chunk;
boolean done;
};
11 changes: 9 additions & 2 deletions reference-implementation/lib/ReadableStreamBYOBReader-impl.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { promiseRejectedWith } = require('./helpers/webidl.js');
const { newPromise, resolvePromise, rejectPromise, promiseRejectedWith } = require('./helpers/webidl.js');
const aos = require('./abstract-ops/readable-streams.js');

exports.implementation = class ReadableStreamBYOBReaderImpl {
Expand Down Expand Up @@ -32,7 +32,14 @@ exports.implementation = class ReadableStreamBYOBReaderImpl {
return promiseRejectedWith(readerLockException('read'));
}

return aos.ReadableStreamBYOBReaderRead(this, view);
const promise = newPromise();
const readIntoRequest = {
chunkSteps: chunk => resolvePromise(promise, { value: chunk, done: false }),
closeSteps: chunk => resolvePromise(promise, { value: chunk, done: true }),
errorSteps: e => rejectPromise(promise, e)
};
aos.ReadableStreamBYOBReaderRead(this, view, readIntoRequest);
return promise;
}

releaseLock() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ interface ReadableStreamBYOBReader {
readonly attribute Promise<void> closed;

Promise<void> cancel(optional any reason);
Promise<any> read(ArrayBufferView view);
Promise<ReadableStreamBYOBReadResult> read(ArrayBufferView view);
void releaseLock();
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

const { promiseResolvedWith } = require('./helpers/webidl.js');
const { CancelSteps, PullSteps } = require('./abstract-ops/internal-methods.js');
const { DequeueValue, ResetQueue } = require('./abstract-ops/queue-with-sizes.js');
const aos = require('./abstract-ops/readable-streams.js');
Expand Down Expand Up @@ -37,7 +36,7 @@ exports.implementation = class ReadableStreamDefaultControllerImpl {
return result;
}

[PullSteps]() {
[PullSteps](readRequest) {
const stream = this._controlledReadableStream;

if (this._queue.length > 0) {
Expand All @@ -50,11 +49,10 @@ exports.implementation = class ReadableStreamDefaultControllerImpl {
aos.ReadableStreamDefaultControllerCallPullIfNeeded(this);
}

return promiseResolvedWith(aos.ReadableStreamCreateReadResult(chunk, false, stream._reader._forAuthorCode));
readRequest.chunkSteps(chunk);
} else {
aos.ReadableStreamAddReadRequest(stream, readRequest);
aos.ReadableStreamDefaultControllerCallPullIfNeeded(this);
}

const pendingPromise = aos.ReadableStreamAddReadRequest(stream);
aos.ReadableStreamDefaultControllerCallPullIfNeeded(this);
return pendingPromise;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dictionary ReadableStreamDefaultReadResult {
any chunk;
boolean done;
};
12 changes: 10 additions & 2 deletions reference-implementation/lib/ReadableStreamDefaultReader-impl.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { promiseRejectedWith } = require('./helpers/webidl.js');
const { newPromise, resolvePromise, rejectPromise, promiseRejectedWith } = require('./helpers/webidl.js');
const aos = require('./abstract-ops/readable-streams.js');

exports.implementation = class ReadableStreamDefaultReaderImpl {
Expand All @@ -25,7 +25,15 @@ exports.implementation = class ReadableStreamDefaultReaderImpl {
return promiseRejectedWith(readerLockException('read from'));
}

return aos.ReadableStreamDefaultReaderRead(this);
const promise = newPromise();
const readRequest = {
chunkSteps: chunk => resolvePromise(promise, { value: chunk, done: false }),
closeSteps: () => resolvePromise(promise, { value: undefined, done: true }),
errorSteps: e => rejectPromise(promise, e)
};

aos.ReadableStreamDefaultReaderRead(this, readRequest);
return promise;
}

releaseLock() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ interface ReadableStreamDefaultReader {
readonly attribute Promise<void> closed;

Promise<void> cancel(optional any reason);
Promise<any> read();
Promise<ReadableStreamDefaultReadResult> read();
void releaseLock();
};
Loading

0 comments on commit 8eb41f7

Please sign in to comment.