summaryrefslogtreecommitdiff
path: root/cli/js/web/streams/writable_stream_default_controller.ts
diff options
context:
space:
mode:
authorKitson Kelly <me@kitsonkelly.com>2020-05-01 00:40:10 +1000
committerGitHub <noreply@github.com>2020-04-30 10:40:10 -0400
commit81c75332fbf2635f5275cc85053dc244f211471d (patch)
treeb0142c4dddf6f2ef2d6c48c0470e5bdfe822b499 /cli/js/web/streams/writable_stream_default_controller.ts
parent84d687e958ab93afb161def4a8ab47f8994307c9 (diff)
feat: Add WritableStreams (and enable ReadableStreams piping) (#4980)
Diffstat (limited to 'cli/js/web/streams/writable_stream_default_controller.ts')
-rw-r--r--cli/js/web/streams/writable_stream_default_controller.ts68
1 files changed, 68 insertions, 0 deletions
diff --git a/cli/js/web/streams/writable_stream_default_controller.ts b/cli/js/web/streams/writable_stream_default_controller.ts
new file mode 100644
index 000000000..040d0eefc
--- /dev/null
+++ b/cli/js/web/streams/writable_stream_default_controller.ts
@@ -0,0 +1,68 @@
+// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
+
+import {
+ AbortAlgorithm,
+ CloseAlgorithm,
+ isWritableStreamDefaultController,
+ Pair,
+ resetQueue,
+ setFunctionName,
+ SizeAlgorithm,
+ WriteAlgorithm,
+ writableStreamDefaultControllerClearAlgorithms,
+ writableStreamDefaultControllerError,
+} from "./internals.ts";
+import * as sym from "./symbols.ts";
+import { WritableStreamImpl } from "./writable_stream.ts";
+import { customInspect } from "../console.ts";
+
+export class WritableStreamDefaultControllerImpl<W>
+ implements WritableStreamDefaultController {
+ [sym.abortAlgorithm]: AbortAlgorithm;
+ [sym.closeAlgorithm]: CloseAlgorithm;
+ [sym.controlledWritableStream]: WritableStreamImpl;
+ [sym.queue]: Array<Pair<{ chunk: W } | "close">>;
+ [sym.queueTotalSize]: number;
+ [sym.started]: boolean;
+ [sym.strategyHWM]: number;
+ [sym.strategySizeAlgorithm]: SizeAlgorithm<W>;
+ [sym.writeAlgorithm]: WriteAlgorithm<W>;
+
+ private constructor() {
+ throw new TypeError(
+ "WritableStreamDefaultController's constructor cannot be called."
+ );
+ }
+
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ error(e: any): void {
+ if (!isWritableStreamDefaultController(this)) {
+ throw new TypeError("Invalid WritableStreamDefaultController.");
+ }
+ const state = this[sym.controlledWritableStream][sym.state];
+ if (state !== "writable") {
+ return;
+ }
+ writableStreamDefaultControllerError(this, e);
+ }
+
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ [sym.abortSteps](reason: any): PromiseLike<void> {
+ const result = this[sym.abortAlgorithm](reason);
+ writableStreamDefaultControllerClearAlgorithms(this);
+ return result;
+ }
+
+ [sym.errorSteps](): void {
+ resetQueue(this);
+ }
+
+ [customInspect](): string {
+ return `${this.constructor.name} { }`;
+ }
+}
+
+setFunctionName(
+ WritableStreamDefaultControllerImpl,
+ "WritableStreamDefaultController"
+);