Skip to content

Disposal & Lifetime

The Course
•
20 min study

Unsubscribing removes one registration. Disposing ends the lifetime of an entire channel. Our contract makes dispose idempotent, clears future listeners, returns false from later publish calls, and rejects later subscriptions with an Error.

These operations share state through a closure, not through a type alias. A Channel<T> declaration can describe the methods and their return values, but runtime guards implement the policy. A boolean publish result distinguishes an accepted publish from one rejected because its channel is disposed.

A snapshot already captured by a current publish is allowed to finish even if a listener disposes the channel. This choice keeps one dispatch internally consistent. New publish calls see the disposed flag. A stricter “stop the current snapshot” rule would be a different contract and would need different tests.

Nested publish is reentrant: a callback can publish again before its outer call completes. The implementation permits it, so callers must avoid unbounded recursion. Disposal does not magically cancel async work started elsewhere; our supported listeners are synchronous.

Predict, check, and run

Write the output before running. Use Check types first, then Check & run. Types disappear at runtime: explain both what the compiler checks and what the code actually does. Each editor is an independent ES module; await asynchronous work explicitly.

Predict, then runTypeScript
let disposed = false;

function publish(value: string): boolean {
  if (disposed) {
    return false;
  }

  console.log(value);
  return true;
}

function dispose(): void {
  disposed = true;
}

console.log(publish("Types"));
dispose();
dispose();
console.log(publish("hidden"));

Enable JavaScript to edit and type-check this example.

Check your prediction
Text
Types
true
false

Check the current snapshot and the next call separately

Disposal ends future use of the channel; it does not necessarily interrupt a dispatch already holding a snapshot. The chosen policy allows captured listeners to finish. A callback disposing the channel therefore creates two observable questions: what happens now and what happens on the next publish?

Treat repeated disposal and old cleanup handles as normal lifecycle interactions. Both should be harmless under this contract. New subscriptions after disposal must still be rejected so callers cannot accidentally revive a finished channel.

Check the current snapshot and the next call separately: traceTypeScript
const events: string[] = [];
let disposed = false;
const listeners = [
  () => {
    events.push("dispose");
    disposed = true;
  },
  () => {
    events.push("captured");
  }
];

function publish(): boolean {
  if (disposed) {
    return false;
  }

  for (const listener of [...listeners]) {
    listener();
  }

  return true;
}

console.log(publish());
console.log(events.join(","));
console.log(publish());

Enable JavaScript to edit and type-check this example.

Check your prediction
Text
true
dispose,captured
false

The first dispatch was accepted and completes its captured work. The second sees the disposed flag before starting. Stopping halfway through the first would be another valid policy, but not this one.

Check the current snapshot and the next call separately: contrastTypeScript
let disposed = false;
let releases = 0;

function dispose(): void {
  if (!disposed) {
    disposed = true;
    releases++;
  }
}

dispose();
dispose();
console.log(releases);

Enable JavaScript to edit and type-check this example.

Check your prediction
Text
1

The counter represents releasing an owned resource once. Idempotence is runtime behavior; a dispose:()=>void annotation alone cannot provide it.

Try the rule on new inputs

Test disposal before subscribe, before publish and inside the first listener. Verify the exact captured-listener order and the result of later calls. Keep cleanup safe even when its original token was already removed.

Going further: using and Symbol.dispose

TypeScript 5.2 added the standard form of this pattern, explicit resource management. An object that has a [Symbol.dispose]() method is Disposable, and a using declaration calls that method automatically when the block ends, even if the block throws:

TypeScript
function open(name: string) {
  console.log(`open ${name}`);
  return {
    [Symbol.dispose]() {
      console.log(`close ${name}`);
    }
  };
}

{
  using connection = open("feed");
  console.log("working");
}
console.log("after");

Run locally, this prints open feed, working, close feed, after. It does not run in this course's playground: the playground checks against the ES2022 library, so the check fails with TS2318 "Cannot find global type 'Disposable'". Locally, add "esnext.disposable" (or "esnext") to lib in tsconfig. At runtime, Node 22 has Symbol.dispose but cannot parse using itself (SyntaxError); with a target of ES2022, tsc rewrites using into a try/finally, which Node 22 runs. Newer runtimes, such as Node 24, support using natively.

using only decides when dispose is called. The rules in this lesson still belong to the dispose method: calling it twice must be harmless, and a disposed channel must not deliver again. A channel's dispose could be exposed as [Symbol.dispose] too, so callers can choose either style.

Trace the contract before building

Draw the lifetime as active followed by disposed, with no transition back. Identify which methods may still be called after that transition and the promised result of each. Repeated cleanup should follow the same policy without accidentally restoring a listener or throwing from an old handle.

Now separate a publish already in progress from a future publish. This course lets the captured listener snapshot finish, even if a callback disposes the channel. Test both observations explicitly. The type declaration can describe dispose and publish, but the disposed flag, listener collection, and guards implement their lifetime agreement. Disposal also does not cancel unrelated work started by a listener.

Build it yourself

Exercise

Disposal & Lifetime: practice

Implement a generic channel with a disposed flag, synchronous undefined-returning listeners, cleanup and publish returning boolean. Subscribe a logger, dispose twice, and print false for a later publish. Assert subscribing afterward throws.

Your workspaceTypeScript
// Start from the stated contract. Add a valid call and an edge case.

Enable JavaScript to edit and type-check this example.

Hint 1: the idea

Check the lifetime state at the start of subscribe and publish.

Hint 2: a first step

Dispose sets the flag and clears the Set; cleanup may still harmlessly delete its old token.

Show one solution
One possible solutionTypeScript
type Listener<T> = (value: T) => undefined;

function createChannel<T>() {
  const records = new Set<{ listener: Listener<T> }>();
  let disposed = false;

  return {
    subscribe(listener: Listener<T>): () => void {
      if (disposed) {
        throw new Error("Disposed");
      }

      const token = { listener };

      records.add(token);
      return () => {
        records.delete(token);
      };
    },
    publish(value: T): boolean {
      if (disposed) {
        return false;
      }

      for (const token of [...records]) {
        token.listener(value);
      }

      return true;
    },
    dispose(): void {
      disposed = true;
      records.clear();
    }
  };
}

const channel = createChannel<string>();
const remove = channel.subscribe(value => {
  console.log(value);
});

channel.dispose();
channel.dispose();
remove();
console.log(channel.publish("hidden"));

let rejected = false;

try {
  channel.subscribe(() => {});
} catch {
  rejected = true;
}

if (!rejected) {
  throw new Error("Disposed channel accepted subscription");
}

Enable JavaScript to edit and type-check this example.

Text
false

The disposed flag is the lifetime: once set, publish returns false and subscribe throws, and the second dispose call simply sets the flag again. records.clear() releases the listeners, and the old remove() still works because deleting a missing record is harmless. Snapshot iteration means a listener that disposes the channel during publish does not stop listeners already captured.

Change it and test it

Dispose during the first callback and assert a second already captured callback still runs. Then publish again and assert no callback runs. Make both expectations explicit in a regression test.

Was this article helpful?

Your response is saved on this device.