Skip to content

RxJS (TypeScript) ​

The reference push-based reactive-streams library for JavaScript: a lazy Observable of zero-to-infinite values, ~115 pipeable operators, pluggable Schedulers with a virtual clock, and a disposable Subscription tree, now being rebuilt in 9.0 on top of the web-platform Observable with AbortSignal cancellation.

FieldValue
LanguageTypeScript (ships JavaScript; 7.x as CJS + ESM, 9.x ESM-only)
LicenseApache-2.0 (LICENSE.txt, package.json)
RepositoryReactiveX/rxjs
Documentationrxjs.dev, rendered from docs_app/content/guide/ at 7.8.2; 9.x docs live beside each package (README.md)
Categorypush-based reactive streams
Versions surveyed7.8.2 (stable latest, commit e5351d02e225, 2025-02-21) and 9.0.0-beta.0 (master, commit 60d632c3878d, 2026-11-18; packages/rxjs/package.json)
Core abstractionObservable<T>: a lazy function from an Observer to a teardown; 7.x returns a Subscription, 9.x returns undefined and is cancelled through an AbortSignal

NOTE

Everything below describes 7.8.2 unless marked v9. The 9.0 line is a different implementation, not an evolution of the 7.x source tree: the v9 README states the beta "has not been published to npm yet" at the surveyed commit. v9 claims are verified against master source; where a claim rests only on v9 design prose it is marked (design doc), and where it could not be verified it is marked (unverified).


Overview ​

What it solves ​

RxJS gives JavaScript a value for "a sequence of events over time" that composes like an array. The guide places it in the missing cell of a two-by-two table (observable.md):

"Observables are lazy Push collections of multiple values."

and, decisively for a library design, insists that the type is not inherently asynchronous:

"An Observable is a lazily evaluated computation that can synchronously or asynchronously return zero to (potentially) infinite values from the time it's invoked onwards."

Its sweet spot is exactly the sparkles first-consumer shape: UI events (keystrokes, toggles) fed through time operators (debounceTime) into cancellable async work (switchMap) and rendered downstream.

Design philosophy ​

  1. An Observable is a function, a Subscription is its return value. subscribe runs the producer function once per subscriber (observable.md: "Each call to observable.subscribe triggers its own independent setup for that given subscriber."). Sharing is opt-in via a Subject.
  2. Operators are pure functions of Observables. core-semantics.md makes this normative for v8+: an operator "MUST be a function that returns an operator function. That is (source: Observable<In>) => Observable<Out>" and the returned function "MUST be referentially transparent".
  3. Concurrency is a parameter, not an ambient property. Every time-based operator takes an optional SchedulerLike and picks a default by "the principle of least concurrency" (scheduler.md).
  4. v9: extend the platform, do not compete with it. DECISIONS.md D-001: "RxJS should extend the platform primitive rather than ship a competing Observable identity." Operators become unique symbol keys installed on Observable.prototype (source[map](f)), schedulers disappear, and cancellation is AbortSignal.

Within this survey RxJS is the canonical push, operator-algebra data point. Contrast it with Effect-TS (pull-based Stream over a fiber runtime with typed errors) and Solid signals (glitch-free synchronous dependency graph); vocabulary is in Concepts, the cross-cut in Comparison.


How it works ​

The four objects ​

ConceptType (7.8.2)Role
ProducerObservable<T> (source)Holds a _subscribe function (or a source + operator pair after lift)
ConsumerObserver<T>Plain { next, error, complete } record
GlueSubscriber<T> (source)Wraps an Observer; is itself a Subscription; enforces the "no notifications after a terminal" rule via isStopped
DisposableSubscription (source)A tree node: closed, _parentage, _finalizers; unsubscribe() is idempotent
Multicast hubSubject<T> (source)Both Observable and Observer; keeps an observers array
Clock + queueSchedulerLike (source)now() and schedule(work, delay, state) returning a Subscription

Subscribing builds a chain of Subscribers ​

Observable.subscribe (Observable.ts) wraps the observer in a SafeSubscriber, then calls operator.call(subscriber, source) for a lifted Observable or _trySubscribe for a raw one, and adds whatever teardown comes back to the subscriber. Every operator is written with operate (util/lift.ts), which hands the operator the downstream subscriber and lets it subscribe upstream with an OperatorSubscriber.

The single structural trick is in the Subscriber constructor (Subscriber.ts):

ts
// src/internal/Subscriber.ts (7.8.2)
constructor(destination?: Subscriber<any> | Observer<any>) {
  super();
  if (destination) {
    this.destination = destination;
    // Automatically chain subscriptions together here.
    // if destination is a Subscription, then it is a Subscriber.
    if (isSubscription(destination)) {
      destination.add(this);
    }
  }
  // ...
}

So each upstream OperatorSubscriber is a child finalizer of its downstream Subscriber: the Subscription tree mirrors the operator chain, and unsubscribing the last subscriber walks it downstream → upstream. Subscription.unsubscribe (Subscription.ts) marks closed, detaches from parents, runs initialTeardown, then each _finalizers entry in insertion order, collecting thrown errors.

Operators are closures over mutable state ​

OperatorSubscriber (OperatorSubscriber.ts) wraps the operator's onNext/onComplete/onError so that a throw becomes destination.error(err), and runs an onFinalize hook once on unsubscribe. An operator's state (a pending value, an inner Subscriber, a counter) lives in let bindings inside the operate callback — one instance per subscription, which is what makes the operator function itself referentially transparent.

v9: the platform Observable ​

In v9 the producer type is the active realm's Observable (native, or the conforming fallback in packages/observable-polyfill/src/index.ts). subscribe() returns undefined; the caller passes { signal } and calls controller.abort(); a producer registers cleanup with subscriber.addTeardown() (MIGRATION.md). Operators are Symbols:

ts
// packages/rxjs/src/switch-map.ts (v9, abridged)
export const switchMap: unique symbol = Symbol('switchMap');
Observable.prototype[switchMap] = switchMapOperator;
// call site: source[switchMap]((q) => hash(q))

The platform surface itself already provides takeUntil, map, filter, take, drop, flatMap, switchMap, inspect, catch, finally and Promise-returning forEach/first/reduce/… (ARCHITECTURE.md, "Platform-shaped fallback surface").


Evaluation model ​

Axis7.8.2v9
Push / pullPush. The producer calls next; there is no demand signal and no backpressurePush; same
Lazy / eagerLazy: nothing runs until subscribeLazy: first observer activates the producer
Cold / hotPlain Observable is cold (producer per subscription); Subject is hotPlatform Observable is ref-counted shared: concurrent observers join one producer; ColdObservable restores per-subscription
Unicast / multicastUnicast by default; multicast only through a Subject (subject.md)Multicast by default for concurrent observers
Emission timingSynchronous unless a scheduler or host callback intervenesSame
GlitchesYes — no topological ordering across branchesSame

Cold by default (7.x). subject.md: "plain Observables are unicast (each subscribed Observer owns an independent execution of the Observable), Subjects are multicast." Two subscribe calls on an HTTP Observable issue two requests.

Shared by default (v9). ARCHITECTURE.md defines the platform lifecycle: "A first observer starts producer work. Additional observers join that active subscriber. Aborting an observer removes it; when the last observer leaves, the subscriber closes and producer teardown runs. A later observer can start a new producer subscription." — i.e. every v9 Observable behaves like a 7.x share(). The document refuses the hot/cold label for it and redefines the terms by producer-creation timing only (D-035).

Synchronous emission. observable.md: "Some people claim that Observables are asynchronous. That is not true." of(1, 2, 3).subscribe(f) calls f three times before subscribe returns, which is why Subscriber must check isStopped on every call and why share wires its refcount finalizer "before the subscription to the source occurs ... so that the assignment to the source connection's closed property will be seen by synchronous firehose sources" (share.ts).

Glitches. combineLatest (observable/combineLatest.ts) emits values.slice() on every input next once each input has produced a first value. In a diamond a$ → (b$, c$) → combineLatest([b$, c$]), one a$ emission produces two outputs, the first pairing the new b with the stale c. Nothing in the library orders propagation by dependency depth; this is the defining difference from Solid signals.


Composition ​

pipe and the operator catalogue. 7.x composes with source.pipe(op1, op2, …); src/operators/index.ts exports about 115 names (including deprecated aliases such as flatMap, mapTo, pluck) backed by 117 files under src/internal/operators/, plus ~30 creation functions under src/internal/observable/. v9 drops .pipe and the OperatorFunction type family (UNSUPPORTED_RXJS_7_SURFACES.md: "RxJS Next does not publish the RxJS 7 pipeable-function or string-named .pipe surface."), keeping a rxjs/pipe Symbol for deliberate composition.

The higher-order mapping family is where the design sense lives — the four operators differ only in what happens to an inner Observable when a new outer value arrives (higher-order-observables.md):

OperatorPolicy for a new outer value while an inner is live7.8.2 implementation
switchMapUnsubscribe the old inner, subscribe the new one (latest wins)Own loop: innerSubscriber?.unsubscribe() then project(value) (source)
mergeMapRun concurrently, up to concurrent (default Infinity)mergeInternals: active < concurrent ? doInnerSub(value) : buffer.push(value) (source)
concatMapQueue; run one at a timeLiterally mergeMap(project, 1) (source)
exhaustMapIgnore the new value until the current inner completesif (!innerSub) { … } (source)

Two consequences for a D design: concatMap's queue is an unbounded T[] (buffer.push) — the push model has nowhere else to put excess values; and v9 collapses the family further by giving switchMap a concurrent option (switch-map.ts: const { concurrent = 1 } = options ?? {};, evicting the oldest AbortController when full) and mergeMap the same option (merge-map.ts). There is no concat-map.ts in v9; the platform flatMap is the sequential flattener (the exact migration target for concatMap is unverified).

Derived values. RxJS has no "computed" primitive. A derived value is an operator chain (combineLatest, withLatestFrom, scan, map) and the "current value" of a derivation exists only if a BehaviorSubject or shareReplay(1) is placed at the end. Operator sprawl is the price of this: overlapping members (debounce/debounceTime, audit/auditTime, throttle/throttleTime, buffer*/window* pairs) exist because each scheduling variant is a separate operator. v9 starts to merge them — numeric and selector debounce share one Symbol, count and delay windows share buffer (D-015).


Time and scheduling ​

A scheduler is, per scheduler.md, three things at once: "A Scheduler is a data structure ... A Scheduler is an execution context ... A Scheduler has a (virtual) clock. It provides a notion of "time" by a getter method now() on the scheduler."

SchedulerMechanism (7.8.2 source)
none (null)Synchronous, recursive delivery
queueSchedulerTrampoline: delay 0 runs via scheduler.flush(this) in the current frame; delay > 0 falls back to AsyncAction (QueueAction.ts)
asapSchedulerMicrotask: immediateProvider.setImmediate, implemented on Promise.resolve().then (AsapAction.ts, Immediate.ts)
asyncSchedulerMacrotask: intervalProvider.setInterval — "we use setInterval to schedule single and repeat actions" (AsyncAction.ts)
animationFrameSchedulerrequestAnimationFrame via animationFrameProvider (AnimationFrameScheduler.ts)

now() is Scheduler.now = dateTimestampProvider.now, i.e. (dateTimestampProvider.delegate || Date).now() (Scheduler.ts, dateTimestampProvider.ts). The delegate slot is the seam TestScheduler.run overwrites to install virtual time.

AsyncAction recycles its interval. schedule() keeps the existing timer handle if the action is rescheduled from inside its own callback with the same delay; otherwise recycleAsyncId clears it (AsyncAction.ts):

ts
// src/internal/scheduler/AsyncAction.ts (7.8.2)
protected recycleAsyncId(_scheduler: AsyncScheduler, id?: TimerHandle, delay: number | null = 0) {
  // If this action is rescheduled with the same delay time, don't clear the interval id.
  if (delay != null && this.delay === delay && this.pending === false) {
    return id;
  }
  // ...
  if (id != null) {
    intervalProvider.clearInterval(id);
  }
  return undefined;
}

debounceTime does not re-arm a timer per value. The 7.8.2 implementation (debounceTime.ts) stores lastValue and lastTime = scheduler.now() on every next, but schedules only "if it's not already up":

ts
// src/internal/operators/debounceTime.ts (7.8.2)
(value: T) => {
  lastValue = value;
  lastTime = scheduler.now();
  // Only set up a task if it's not already up
  if (!activeTask) {
    activeTask = scheduler.schedule(emitWhenIdle, dueTime);
    subscriber.add(activeTask);
  }
},

When the task fires, emitWhenIdle computes targetTime = lastTime + dueTime; if now < targetTime it reschedules itself for the remainder (this.schedule(undefined, targetTime - now)), otherwise it emits. A burst of k keystrokes inside one window therefore costs k clock reads and at most one reschedule per elapsed window, not k clearTimeout/setTimeout pairs. (Because the remainder differs from dueTime, that one reschedule does clear and re-request the interval.) Source completion flushes the pending value synchronously before complete.

v9 reverses this. v9 has no debounceTime; the numeric form of debounce (v9 debounce.ts) aborts the previous AbortController and calls globalThis.setTimeout(emitPendingValue, delay) on every value — the re-arm-per-value design. D-019 (DECISIONS.md) makes it normative: "each source value replaces the preceding host timer." v9 also has no scheduler argument anywhere; D-034 requires late globalThis.* resolution so @rxjs/test can patch the host.

debounce(durationSelector) (debounce.ts) generalises the window to any Observable: on each value it unsubscribes the previous duration subscriber and subscribes innerFrom(durationSelector(value)); the first next of the duration emits the pending value. This is the hook for a runtime-adjustable delay: debounce(() => timer(delay$.getValue())) reads the current delay per value.


Concurrency and cancellation ​

Cancellation is synchronous unsubscription. subscription.unsubscribe() runs finalizers inline, on the caller's stack (subscription.md: "A Subscription is an object that represents a disposable resource, usually the execution of an Observable."). There is no cancellation request and no acknowledgement: once unsubscribe returns, the consumer is detached; whatever the producer had in flight (a fetch, a worker job) must be stopped by the teardown function the producer returned, and any late result is dropped because Subscriber.isStopped is set.

switchMap unsubscribes the old inner before creating the new one. From switchMap.ts:

ts
// src/internal/operators/switchMap.ts (7.8.2)
(value) => {
  // Cancel the previous inner subscription if there was one
  innerSubscriber?.unsubscribe();
  let innerIndex = 0;
  const outerIndex = index++;
  // Start the next inner subscription
  innerFrom(project(value, outerIndex)).subscribe(
    (innerSubscriber = createOperatorSubscriber(subscriber, /* next */ …, /* complete */ () => {
      innerSubscriber = null!;
      checkComplete();
    }))
  );
},

The order is: old inner's teardown runs to completion → project(value) is called → new inner subscribes. A producer that posts "cancel job n" in its teardown therefore posts it before the mapper posts "start job n+1". The result completes only when the outer is complete and no inner is live (checkComplete). v9 keeps the order — active.shift()!.abort() precedes mapper(value, index++) (v9 switch-map.ts) — but the cancellation travels as an abort event on the inner's AbortSignal.

No preemption. Every next runs to completion on the JavaScript thread. A synchronous map(hashEverything) blocks the UI and cannot be interrupted by switchMap: the next keystroke is not even dequeued until it returns. RxJS has no thread pool and no Worker integration; offloading means wrapping a Worker in new Observable(...) by hand, and cooperative cancellation is the worker's job — it sees the cancel message only when it returns to its own event loop (or polls a SharedArrayBuffer flag).

Schedulers are not concurrency. observeOn/subscribeOn move delivery to another queue on the same thread; they buy fairness between macrotasks, not parallelism.

v9: AbortSignal all the way down. subscribe() returns undefined (MIGRATION.md: "Own cancellation with an AbortController, and pass its signal in the subscription options."); teardowns "run in reverse insertion order" — the opposite of 7.x's insertion-order finalizers. The polyfill must patch AbortController.prototype.abort because "JavaScript exposes abort events but not the DOM-standard abort-algorithm hook that must run before those events" (ARCHITECTURE.md).


Errors and completion ​

The grammar is next* (error | complete)?. An error is terminal and untyped (err: any); after it the Subscriber sets isStopped and unsubscribes (Subscriber.ts). A throw from a producer function is routed to sink.error(err) by _trySubscribe (Observable.ts); a throw from an operator callback becomes destination.error(err) via OperatorSubscriber.

Recovery replaces the stream. catchError(selector) (catchError.ts) unsubscribes the failed source and subscribes the Observable the selector returns — the failed source is gone, so a catchError placed after switchMap kills the whole pipeline on the first failed hash. retry({ count, delay, resetOnSuccess }) (retry.ts) re-subscribes the source up to count (default Infinity) times.

Isolate errors inside the inner. The idiom for "one failed job must not end the stream" is to catch inside the higher-order mapper — switchMap(q => hash(q).pipe(catchError(e => of(failed(q, e))))) — turning the error into a value. This is the Expected-as-value discipline event-horizon already uses, applied by convention rather than by type.

Consumer errors never reach the producer. A SafeSubscriber's ConsumerObserver wraps each user callback; a throw goes to handleUnhandledError → reportUnhandledError, which rethrows on a fresh setTimeout (reportUnhandledError.ts). One throwing observer of a Subject therefore does not stop delivery to the others.

UnsubscriptionError. Subscription.unsubscribe runs every finalizer even if some throw, then throws one aggregate (UnsubscriptionError.ts): "${errors.length} errors occurred during unsubscription:". v9 removes it with the rest of the Subscription type (UNSUPPORTED_RXJS_7_SURFACES.md: "These errors belong to removed RxJS 7 Subscription and closed-Subject internals.").


State and sharing ​

TypeBehaviour (7.8.2)
SubjectSnapshot-iterates observers on next (currentObservers = Array.from(this.observers)); late subscribers to a stopped Subject get the terminal only (source)
BehaviorSubjectHolds _value; replays it synchronously on subscribe; getValue() throws the stored error if errored (source)
ReplaySubjectRing of bufferSize values, trimmed by windowTime against a TimestampProvider (source)
share(config)Refcounted Subject connector; resetOnError/resetOnComplete/resetOnRefCountZero all default true (source)
shareReplay(n)share with a ReplaySubject, resetOnComplete: false, and resetOnRefCountZero: refCount — default false (source)

BehaviorSubject is the only built-in synchronous read of "current state":

ts
// src/internal/BehaviorSubject.ts (7.8.2)
getValue(): T {
  const { hasError, thrownError, _value } = this;
  if (hasError) {
    throw thrownError;
  }
  this._throwIfClosed();
  return _value;
}

v9 has no getValue. The v9 BehaviorSubject (behavior-subject.ts, 29 lines) keeps a private #currentValue, replays it on subscribe, and exposes only the lowercase factory behaviorSubject(value); there is no synchronous accessor in the source. The dynamic-debounce variant that re-reads the delay per value must hold the number in a plain variable instead.

Hot/cold pitfalls.

  • shareReplay(1) without { refCount: true } keeps its source subscribed after the last subscriber leaves — a timer or socket stays alive for the life of the program.
  • A cold source used twice (results$ rendered and logged) runs the producer twice: two worker jobs per query. In 7.x the fix is share(); in v9 this is the default behaviour and per-subscription work must be requested with ColdObservable.
  • Resubscribing to a Subject loses values emitted in between; resubscribing to a BehaviorSubject replays the latest. This difference decides whether the worked pipeline below loses a keystroke when the delay changes.

Lifetime and ownership ​

The subscriber owns the subscription. The top-level subscribe call returns the root of the Subscription tree; nothing else holds it. Every leak in RxJS code is a root nobody unsubscribed: an interval in a component that was destroyed, a fromEvent listener left registered. No scope encloses a subscription automatically.

takeUntil(destroy$) is the idiom that ties a chain to an owner's lifetime (takeUntil.ts): it subscribes the notifier first (as core-semantics.md requires: "Notifiers provided directly to the operator MUST be subscribed to before the source is subscribed to.") and completes the result on the notifier's first next. It must be the last operator: placed before a switchMap, it completes the outer, but switchMap's checkComplete waits for the live inner, which keeps running.

finalize(callback) (finalize.ts) adds callback as a finalizer of the downstream subscriber, so it runs on error, complete and unsubscribe — the scope(exit) of RxJS.

v9 inverts ownership into the caller's AbortController: a component holds one controller and passes its signal to every subscribe; controller.abort() is the scope exit. This is structurally the event-horizon model (a cancellation token owned by an enclosing scope), though the tree is still built by hand.


Testing ​

TestScheduler virtualises time by swapping every provider delegate inside run() (TestScheduler.ts):

ts
// src/internal/testing/TestScheduler.ts — run() (7.8.2, abridged)
TestScheduler.frameTimeFactor = 1;
this.maxFrames = Infinity;
this.runMode = true;
animationFrameProvider.delegate = animator.delegate;
dateTimestampProvider.delegate = this;
immediateProvider.delegate = delegates.immediate;
intervalProvider.delegate = delegates.interval;
timeoutProvider.delegate = delegates.timeout;
performanceTimestampProvider.delegate = this;

Marble strings describe a timeline (marble-testing.md): - is one frame (1 virtual ms inside run), a a value, | complete, # error, ^/! subscribe/unsubscribe points, (ab) a synchronous group, and 10ms a time progression. A debounce test reads:

ts
testScheduler.run(({ cold, expectObservable }) => {
  const q = cold('a 50ms b 300ms c|');
  expectObservable(q.pipe(debounceTime(100))).toBe('151ms b 201ms (c|)');
});

Documented known issues (marble-testing.md, "Known issues"):

  • "If you have RxJS code that uses asynchronous scheduling - e.g. Promises, etc. - you can't reliably use marble diagrams for that particular code." — microtasks are not virtualised.
  • "you also can't currently assert delays of zero, even with AsyncScheduler".
  • Sync groups advance time by their character count: "'(abc)' will emit the values of a, b, and c synchronously in the same frame and then advance virtual time by 5 frames" — called "a known pain point in real-world testing".
  • Outside run(), frames are 10 ms, maxFrames = 750, operators do not pick up the test scheduler, and flushing is manual.

v9 replaces TestScheduler with @rxjs/test's rxTest, which "virtualizes the supported host timing APIs" and returns a Promise (D-012, DECISIONS.md; packages/test/README.md). Because v9 has no scheduler seam, virtual time is achieved by patching globalThis timers; D-034 admits that "References captured by application code before rxTest patches the realm remain outside the supported virtual-host boundary."


The worked pipeline ​

The first consumer — query field → debounce N ms (N adjustable) → hash a file set on a worker with latest-wins cancellation → render; a toggle enables or disables it — in RxJS 7.8.2:

ts
import { BehaviorSubject, EMPTY, Observable, Subject, of } from 'rxjs';
import {
  catchError,
  debounceTime,
  distinctUntilChanged,
  switchMap,
  takeUntil,
} from 'rxjs/operators';

type Result =
  | { kind: 'ok'; query: string; digest: string }
  | { kind: 'err'; query: string; error: unknown };

const query$ = new BehaviorSubject<string>(''); // text field: next() on input
const delayMs$ = new BehaviorSubject<number>(250); // N, adjustable at runtime
const enabled$ = new BehaviorSubject<boolean>(true); // the toggle
const destroy$ = new Subject<void>(); // the view's lifetime

// One worker job per subscription; teardown posts a cancel if the job is still running.
function hashOnWorker(worker: Worker, query: string): Observable<Result> {
  return new Observable<Result>(subscriber => {
    const id = crypto.randomUUID();
    let done = false;
    const onMessage = (e: MessageEvent) => {
      if (e.data.id !== id) return; // a stale job's reply
      done = true;
      subscriber.next(
        e.data.ok
          ? { kind: 'ok', query, digest: e.data.digest }
          : { kind: 'err', query, error: e.data.error },
      );
      subscriber.complete();
    };
    worker.addEventListener('message', onMessage);
    worker.postMessage({ type: 'hash', id, query });
    return () => {
      // runs on complete, error, or unsubscribe
      worker.removeEventListener('message', onMessage);
      if (!done) worker.postMessage({ type: 'cancel', id });
    };
  });
}

const results$ = enabled$.pipe(
  distinctUntilChanged(),
  switchMap(on =>
    on
      ? delayMs$.pipe(
          distinctUntilChanged(),
          switchMap(ms => query$.pipe(debounceTime(ms))), // a new N restarts the window
          distinctUntilChanged(), // same query after a restart: no re-hash
          switchMap(q =>
            hashOnWorker(worker, q).pipe(
              catchError(error => of<Result>({ kind: 'err', query: q, error })),
            ),
          ),
        )
      : EMPTY,
  ),
  takeUntil(destroy$), // last, so it also ends live inners
);

results$.subscribe(render);

What happens, event by event (times in ms; N = 250):

tEventPrecisely what runs
0subscribe(render)Synchronously: enabled$ replays true → outer switchMap subscribes the branch → delayMs$ replays 250 → query$ replays '' → debounceTime stores lastTime = 0 and schedules one AsyncAction (a setInterval(…, 250)). subscribe returns.
100user types aquery$.next('a') → debounceTime stores lastValue = 'a', lastTime = 100; a task is already up, so no timer call.
250timer firesemitWhenIdle: targetTime = 350 > now, reschedules for 100 ms (different delay → clearInterval + new interval).
350timer firesEmits 'a' → distinctUntilChanged passes it (first value; '' was superseded before emission) → switchMap has no inner → hashOnWorker('a') posts hash#1.
610'ab' (typed at 360) emittedswitchMap calls innerSubscriber.unsubscribe() first: job 1's teardown removes its listener and posts cancel#1; then project('ab') posts hash#2. The worker receives cancel#1 before hash#2 (postMessage is FIFO) but processes it only when its own loop yields between files. A late reply for job 1 is ignored: its listener is gone.
900worker replies for job 2done = true, render({ kind: 'ok', query: 'ab', … }), inner completes; its teardown sees done and posts nothing. The result stays open (outer not complete).
1000delayMs$.next(500)Middle switchMap unsubscribes query$.pipe(debounceTime(250)) (cancels any pending window, discards lastValue) and subscribes debounceTime(500); query$ replays 'ab' so nothing typed is lost. At 1500 'ab' is emitted and suppressed by distinctUntilChanged; no re-hash; job 2's result stays rendered.
1600toggle offOuter switchMap unsubscribes the whole branch, depth-first through the Subscription tree: any in-flight job's teardown posts its cancel. project(false) returns EMPTY, which completes at once; render is not called, so the last result stays on screen — return of(disabledMarker) instead to clear it.
1700toggle onA fresh branch: all three BehaviorSubjects replay, the inner distinctUntilChanged is new, so the current query is debounced with N = 500 and re-hashed once.
enddestroy$.next()takeUntil completes the result; SafeSubscriber unsubscribes, which tears the chain down exactly as for "toggle off".

Variant: re-read N per value. debounce(() => timer(delayMs$.getValue())) replaces the middle switchMap. A new N then applies from the next keystroke rather than restarting the current window, and debounce re-subscribes a timer per value (one setInterval + clearInterval pair per keystroke) where debounceTime reuses one action.

What the user code had to supply that a D library should provide: the job identity and stale-reply guard, the done flag that stops a cancel being sent for a finished job, the ordering argument that makes cancel#n precede hash#n+1, the catchError placement that keeps one failure from killing the stream, and the takeUntil-must-be-last rule. None is checked by the type system.


Strengths ​

  • One small algebra covers UI events, timers and async work. switchMap + debounceTime express latest-wins search in two operators; the four mapping policies make concurrency choices explicit and named.
  • Deterministic, synchronous teardown. unsubscribe runs the whole finalizer tree before it returns; switchMap cancels the old inner strictly before starting the new one.
  • Virtual time is a first-class seam (7.x). Every time operator takes a SchedulerLike; TestScheduler.run turns a debounce test into a one-line marble assertion.
  • Allocation-aware internals. debounceTime reuses a single scheduled action across a burst; AsyncAction keeps its interval when rescheduled with the same delay.
  • Normative operator semantics. core-semantics.md states subscription order, notifier rules and finalization timing as MUST/SHOULD rules.
  • v9 aligns with the platform. Native Observable, AbortSignal cancellation, shared-by-default activation and Symbol-keyed extensions remove the library's private identity and its Subscription type.

Weaknesses ​

  • No backpressure. Push without demand means excess goes into unbounded buffers (mergeInternals' buffer: T[]) or is dropped by a lossy operator the user must choose.
  • Glitches. combineLatest emits intermediate inconsistent tuples in diamonds; there is no topological propagation.
  • Untyped, terminal errors. err: any and stream-killing error push every robust pipeline toward catching inside inners by convention.
  • Ownership is manual. No enclosing scope; leaks come from forgotten roots, shareReplay without refCount, and takeUntil in the wrong position.
  • Hot/cold is invisible in types. The same Observable<T> type may start a worker per subscriber or share one; v9 flips the default, so the same code changes meaning across major versions.
  • No parallelism or preemption. Synchronous work blocks every stream; workers are hand-wrapped with hand-rolled cancellation protocols.
  • Operator sprawl. ~115 exported names with overlapping time/selector pairs and deprecated aliases.
  • v9 regresses testability and debounce cost. Without schedulers, virtual time depends on patching globalThis (with the captured-reference hole D-034 admits), and numeric debounce re-arms a host timer per value.

Key design decisions and trade-offs ​

DecisionRationaleTrade-off
Observable = lazy function; cold by default (7.x)Referential transparency: subscribing twice is calling a function twiceDuplicate side effects unless the user adds share(); hot/cold not visible in the type
Shared, ref-counted activation by default (v9)Match the web-platform Observable lifecycle (D-001, D-035)Per-subscription work needs an explicit ColdObservable; a semantic break for every 7.x pipeline
Pure push, no demand channelSimplest protocol; fits DOM events, which cannot be pausedUnbounded buffering or explicit lossy operators; no flow control to a slow consumer
Synchronous emission allowedNo forced async hop; zero-latency of/map chainsRe-entrancy hazards; share and Subscriber must guard against firehose sources
Subscription tree mirrors the operator chainunsubscribe at the sink cancels everything upstream deterministicallyHand-built tree; leaks when a root is dropped; 7.x insertion-order vs v9 reverse-order teardown
switchMap tears down the old inner before calling projectCancel is observably ordered before the next startTeardown cannot await an acknowledgement; in-flight work must be cancelled cooperatively
debounceTime stores lastTime and reschedules once (7.x)One scheduled action per burst instead of a timer per valueDepends on now() precision and the scheduler; v9 abandoned it for one host timer per value
Injectable SchedulerLike on every time operator (7.x)Least-concurrency defaults; virtual time for testsAPI surface on dozens of operators; removed entirely in v9 in favour of host patching
Errors are terminal and untypedMirrors Promise rejection; a single failure channelRecovery replaces the stream; per-item isolation is a convention (catchError inside inners)
Operators as pipeable functions (7.x) → Symbol-keyed methods (v9)Tree-shaking without patching string names onto a platform type (D-003)Bracket-call syntax source[map](f); no .pipe; a migration tool is required

Sources ​