DeutschGitHub
prototypepackages/reactivity/src

Reactivity

@ralphschuler/reactivity

Owns cold-source subscriptions with independent termination and exactly-once teardown.

Observablesubscriptionoperators

01 · Problem

What is it for?

A source must stop delivery after termination and release resources for each independent subscriber.

02 · Mental model

How should you think about it?

A subscription owns one source execution and its teardown. Operators transform that execution without changing its lifetime.

In this repository

Owns cold-source subscriptions with independent termination and exactly-once teardown. The operator set is deliberately small. Hot multicasting, backpressure and schedulers require additional contracts.

03 · Control flow

What happens in which order?

  1. Create a source and subscribe an observer.
  2. Forward active values through operators.
  3. Terminate on complete or error.
  4. Run teardown exactly once.
  5. Keep independent subscribers isolated.

04 · Building blocks

The important contracts

SimpleObservable / SimpleSubscriptionOwn independent cold-source execution and teardown.
map / filterTransform or filter values while propagating terminal signals.

05 · Build it yourself

Implement a small core

Start with this small independent implementation, then use the steps below to make its contract explicit.

  1. Create a source and subscribe an observer.
  2. Forward active values through operators.
  3. Terminate on complete or error.
  4. Run teardown exactly once.
  5. Keep independent subscribers isolated.
Concept sketch · may reference omitted helpers
type Observer<T> = {
  next(value: T): void;
  error(error: unknown): void;
  complete(): void;
};

class Subscription {
  private closed = false;
  constructor(private teardown: () => void) {}
  unsubscribe() {
    if (this.closed) return;
    this.closed = true;
    this.teardown();
  }
}

06 · Verify

What should you test?

  • Unsubscribing one observer must not affect another.
  • Suppress values after completion or error.
  • Verify operator errors and exactly-once teardown.

Try it yourself

Turn the first verification claim into a failing test, implement the smallest fix, then add a boundary case.

Unsubscribing one observer must not affect another.

Compare your approach

Your check should fail when the stated invariant is deliberately broken, and pass again after the fix. Keep setup deterministic and assert an observable result.

07 · Limits

Trade-offs and failure modes

  • Cold and hot sources have different ownership.
  • Synchronous completion can occur before setup returns.
  • Operator types should preserve value types.
Important

The operator set is deliberately small. Hot multicasting, backpressure and schedulers require additional contracts.

08 · Keep exploring

Source code and related ideas

yarn tsx packages/reactivity/examples/basic.ts