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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import {
CancelAlgorithm,
dequeueValue,
isReadableStreamDefaultController,
Pair,
PullAlgorithm,
readableStreamAddReadRequest,
readableStreamClose,
readableStreamCreateReadResult,
readableStreamDefaultControllerCallPullIfNeeded,
readableStreamDefaultControllerCanCloseOrEnqueue,
readableStreamDefaultControllerClearAlgorithms,
readableStreamDefaultControllerClose,
readableStreamDefaultControllerEnqueue,
readableStreamDefaultControllerError,
readableStreamDefaultControllerGetDesiredSize,
resetQueue,
SizeAlgorithm,
setFunctionName,
} from "./internals.ts";
import type { ReadableStreamImpl } from "./readable_stream.ts";
import * as sym from "./symbols.ts";
import { customInspect } from "../console.ts";
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export class ReadableStreamDefaultControllerImpl<R = any>
implements ReadableStreamDefaultController<R> {
[sym.cancelAlgorithm]: CancelAlgorithm;
[sym.closeRequested]: boolean;
[sym.controlledReadableStream]: ReadableStreamImpl<R>;
[sym.pullAgain]: boolean;
[sym.pullAlgorithm]: PullAlgorithm;
[sym.pulling]: boolean;
[sym.queue]: Array<Pair<R>>;
[sym.queueTotalSize]: number;
[sym.started]: boolean;
[sym.strategyHWM]: number;
[sym.strategySizeAlgorithm]: SizeAlgorithm<R>;
private constructor() {
throw new TypeError(
"ReadableStreamDefaultController's constructor cannot be called."
);
}
get desiredSize(): number | null {
if (!isReadableStreamDefaultController(this)) {
throw new TypeError("Invalid ReadableStreamDefaultController.");
}
return readableStreamDefaultControllerGetDesiredSize(this);
}
close(): void {
if (!isReadableStreamDefaultController(this)) {
throw new TypeError("Invalid ReadableStreamDefaultController.");
}
if (!readableStreamDefaultControllerCanCloseOrEnqueue(this)) {
throw new TypeError(
"ReadableStreamDefaultController cannot close or enqueue."
);
}
readableStreamDefaultControllerClose(this);
}
enqueue(chunk: R): void {
if (!isReadableStreamDefaultController(this)) {
throw new TypeError("Invalid ReadableStreamDefaultController.");
}
if (!readableStreamDefaultControllerCanCloseOrEnqueue(this)) {
throw new TypeError("ReadableSteamController cannot enqueue.");
}
return readableStreamDefaultControllerEnqueue(this, chunk);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
error(error?: any): void {
if (!isReadableStreamDefaultController(this)) {
throw new TypeError("Invalid ReadableStreamDefaultController.");
}
readableStreamDefaultControllerError(this, error);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[sym.cancelSteps](reason?: any): PromiseLike<void> {
resetQueue(this);
const result = this[sym.cancelAlgorithm](reason);
readableStreamDefaultControllerClearAlgorithms(this);
return result;
}
[sym.pullSteps](): Promise<ReadableStreamReadResult<R>> {
const stream = this[sym.controlledReadableStream];
if (this[sym.queue].length) {
const chunk = dequeueValue<R>(this);
if (this[sym.closeRequested] && this[sym.queue].length === 0) {
readableStreamDefaultControllerClearAlgorithms(this);
readableStreamClose(stream);
} else {
readableStreamDefaultControllerCallPullIfNeeded(this);
}
return Promise.resolve(
readableStreamCreateReadResult(
chunk,
false,
stream[sym.reader]![sym.forAuthorCode]
)
);
}
const pendingPromise = readableStreamAddReadRequest(stream);
readableStreamDefaultControllerCallPullIfNeeded(this);
return pendingPromise;
}
[customInspect](): string {
return `${this.constructor.name} { desiredSize: ${String(
this.desiredSize
)} }`;
}
}
setFunctionName(
ReadableStreamDefaultControllerImpl,
"ReadableStreamDefaultController"
);
|