Cancellation Token With Linked Sources

A cooperative cancellation primitive with reasons, unsubscribe callbacks, throwIfCancelled and linked token sources that cascade cancellation.

JavascriptResilience

Code

javascriptsnippet
70 lines
export class CancellationError extends Error {
  constructor(message = "Operation cancelled") {
    super(message);
    this.name = "CancellationError";
  }
}

export class CancellationToken {
  #cancelled = false;
  #reason;
  #listeners = new Set();

  get isCancelled() {
    return this.#cancelled;
  }

  get reason() {
    return this.#reason;
  }

  throwIfCancelled() {
    if (!this.#cancelled) return;
    if (this.#reason instanceof Error) throw this.#reason;
    throw new CancellationError(String(this.#reason));
  }

  onCancel(listener) {
    if (typeof listener !== "function") {
      throw new TypeError("listener must be a function");
    }
    if (this.#cancelled) {
      queueMicrotask(() => listener(this.#reason));
      return () => {};
    }
    this.#listeners.add(listener);
    return () => this.#listeners.delete(listener);
  }

  _cancel(reason) {
    if (this.#cancelled) return false;
    this.#cancelled = true;
    this.#reason = reason ?? new CancellationError();
    for (const listener of [...this.#listeners]) listener(this.#reason);
    this.#listeners.clear();
    return true;
  }
}

export class CancellationTokenSource {
  #token = new CancellationToken();

  get token() {
    return this.#token;
  }

  cancel(reason) {
    return this.#token._cancel(reason);
  }

  static link(...sources) {
    const controller = new CancellationTokenSource();
    const unsubscribers = sources.map((source) =>
      source.token.onCancel((reason) => controller.cancel(reason)),
    );
    controller.token.onCancel(() => {
      for (const unsubscribe of unsubscribers) unsubscribe();
    });
    return controller;
  }
}

What the code does

Cancellation Token With Linked Sources keeps everything in the classes CancellationError, CancellationToken and CancellationTokenSource.

The structure is a loop within a loop: for (const listener of [...this.#listeners]) listener(this.#reason); outside, for (const unsubscribe of unsubscribers) unsubscribe(); inside. Failure is loud: CancellationError and TypeError aborts the current operation instead of degrading it.

What to watch for

  • The guard !this.#cancelled short-circuits the flow with an early return when its condition holds.

Using it

CancellationError is what another module would consume; nothing else here is load-bearing for callers.

Key statements

return () => this.#listeners.delete(listener);
queueMicrotask(() => listener(this.#reason));
return () => {};

Interactive Tools for This Topic

FAQ

How does the snippet fail?
`CancellationError` and `TypeError` is raised at the guard (`!this.#cancelled`), so invalid state never reaches the main logic.
Are there third-party dependencies?
No. The dependency list is empty; the code stays inside the standard library.