forked from whatwg/streams
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReadableStreamDefaultController-impl.js
60 lines (47 loc) · 1.75 KB
/
ReadableStreamDefaultController-impl.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
'use strict';
const { CancelSteps, PullSteps, ReleaseSteps } = require('./abstract-ops/internal-methods.js');
const { DequeueValue, ResetQueue } = require('./abstract-ops/queue-with-sizes.js');
const aos = require('./abstract-ops/readable-streams.js');
exports.implementation = class ReadableStreamDefaultControllerImpl {
get desiredSize() {
return aos.ReadableStreamDefaultControllerGetDesiredSize(this);
}
close() {
if (aos.ReadableStreamDefaultControllerCanCloseOrEnqueue(this) === false) {
throw new TypeError('The stream is not in a state that permits close');
}
aos.ReadableStreamDefaultControllerClose(this);
}
enqueue(chunk) {
if (aos.ReadableStreamDefaultControllerCanCloseOrEnqueue(this) === false) {
throw new TypeError('The stream is not in a state that permits enqueue');
}
return aos.ReadableStreamDefaultControllerEnqueue(this, chunk);
}
error(e) {
aos.ReadableStreamDefaultControllerError(this, e);
}
[CancelSteps](reason) {
ResetQueue(this);
const result = this._cancelAlgorithm(reason);
aos.ReadableStreamDefaultControllerClearAlgorithms(this);
return result;
}
[PullSteps](readRequest) {
const stream = this._stream;
if (this._queue.length > 0) {
const chunk = DequeueValue(this);
if (this._closeRequested === true && this._queue.length === 0) {
aos.ReadableStreamDefaultControllerClearAlgorithms(this);
aos.ReadableStreamClose(stream);
} else {
aos.ReadableStreamDefaultControllerCallPullIfNeeded(this);
}
readRequest.chunkSteps(chunk);
} else {
aos.ReadableStreamAddReadRequest(stream, readRequest);
aos.ReadableStreamDefaultControllerCallPullIfNeeded(this);
}
}
[ReleaseSteps]() {}
};