summaryrefslogtreecommitdiff
path: root/cli/js/web/streams/writable_stream_default_controller.ts
blob: b957c2c8f4a4787814723e07de9234e3b2d1440e (plain)
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
// 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 type { 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"
);