2020-04-22 10:06:51 -04:00
|
|
|
// 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,
|
2020-04-30 10:40:10 -04:00
|
|
|
setFunctionName,
|
2020-04-22 10:06:51 -04:00
|
|
|
} from "./internals.ts";
|
2020-07-08 05:26:39 -04:00
|
|
|
import type { ReadableStreamImpl } from "./readable_stream.ts";
|
2020-04-22 10:06:51 -04:00
|
|
|
import * as sym from "./symbols.ts";
|
2020-04-30 10:40:10 -04:00
|
|
|
import { customInspect } from "../console.ts";
|
2020-04-22 10:06:51 -04:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2020-04-27 19:06:03 -04:00
|
|
|
[customInspect](): string {
|
2020-04-30 10:40:10 -04:00
|
|
|
return `${this.constructor.name} { desiredSize: ${String(
|
2020-04-22 10:06:51 -04:00
|
|
|
this.desiredSize
|
|
|
|
)} }`;
|
|
|
|
}
|
|
|
|
}
|
2020-04-30 10:40:10 -04:00
|
|
|
|
|
|
|
setFunctionName(
|
|
|
|
ReadableStreamDefaultControllerImpl,
|
|
|
|
"ReadableStreamDefaultController"
|
|
|
|
);
|