Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/okikio/observables. A spec-faithful yet ergonomic TC39-inspired Observable library with detailed ty
Something went wrong. Try again.
observables observable.ts
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195// @filename: observable.ts/** * A **spec-faithful** yet ergonomic TC39-inspired Observable implementation with detailed TSDocs and examples. * * A **push‑based stream abstraction** for events, data, and long‑running * operations. Think of it as a **multi‑value Promise** that keeps sending * values until you tell it to stop. * * Meant to help with juggling many async sources, mouse clicks, HTTP requests, timers, * WebSockets, file watchers. Before Observables you glued those together with a * mish‑mash of callbacks, Promises, `EventTarget`s and async iterators, each * with different rules for cleanup and error handling. **Observables give you * one mental model** for subscription → cancellation → propagation → teardown. * * @example Common Patterns * ```ts * // DOM events → Observable * const clicks = new Observable<Event>(obs => { * const h = (e: Event) => obs.next(e); * button.addEventListener("click", h); * return () => button.removeEventListener("click", h); * }); * * // HTTP polling every 5 s * const poll = new Observable<Response>(obs => { * const id = setInterval(async () => { * try { obs.next(await fetch("/api/data")); } * catch (e) { obs.error(e); } * }, 5000); * return () => clearInterval(id); * }); * * // WebSocket stream with graceful close * const live = new Observable<string>(obs => { * const ws = new WebSocket("wss://example.com"); * ws.onmessage = e => obs.next(e.data); * ws.onerror = e => obs.error(e); * ws.onclose = () => obs.complete(); * return () => ws.close(); * }); * ``` * * That makes Observables a good fit for UI events, timers, socket messages, * file watchers, and any workflow where starting, stopping, and cleaning up * are part of the job. * * By default, each `subscribe()` call starts fresh work for that subscriber. * If you subscribe twice to a fetch-like Observable, you usually get two * separate fetches unless you choose to share the source. * * ```text * source setup -> subscribe() -> next(value) ... -> complete() * | | * | -> error(error) * -> unsubscribe() * ``` * * Cleanup runs once when the subscription completes, fails, or is cancelled, * even if the source finishes right away during setup. * * Read operator pipelines the same way you read array pipelines: * `pipe(source, map(...), filter(...))` is the time-based version of * `array.map(...).filter(...)`. * * @example Turning DOM events into a stream * ```ts * const clicks = new Observable<MouseEvent>((observer) => { * const handler = (event: MouseEvent) => observer.next(event); * button.addEventListener('click', handler); * * return () => button.removeEventListener('click', handler); * }); * * // Auto-cleanup when leaving scope * using subscription = clicks.subscribe(event => console.log('Clicked!')); * ``` * * @example Building an Array-style pipeline for async values * ```ts * const evenTens = pipe( * Observable.of(1, 2, 3, 4), * filter((value) => value % 2 === 0), * map((value) => value * 10), * ); * * evenTens.subscribe(console.log); * // 20 * // 40 * ``` * * @example Pulling values at the consumer's pace * ```ts * const values = Observable.from([1, 2, 3, 4, 5]); * * for await (const value of values.pull({ strategy: { highWaterMark: 2 } })) { * console.log(value); * await new Promise((resolve) => setTimeout(resolve, 100)); * } * ``` * * @module */import type { ObservableProtocol, SpecObservable, SpecSubscription,} from "./_spec.ts";import type { Observer, Subscription } from "./_types.ts";
import { assertObservableError, ObservableError } from "./error.ts";import { Symbol } from "./symbol.ts";
/** * `Teardown` describes how a subscription releases resources. * * The subscriber can return a plain function, an object with * `unsubscribe()`, or a sync/async disposable. They all mean the same thing: * when the subscription closes, clean up once. * * @example Cleaning up a timer * ```ts * new Observable<number>((observer) => { * const timer = setInterval(() => observer.next(Date.now()), 1000); * return () => clearInterval(timer); * }); * ``` * * @example Cleaning up several resources together * ```ts * new Observable<string>((observer) => { * const timer = setInterval(() => observer.next('tick'), 1000); * const socket = new WebSocket(url); * * return () => { * clearInterval(timer); * socket.close(); * }; * }); * ``` */export type Teardown = | (() => void) | SpecSubscription | AsyncDisposable | Disposable | null | undefined | void;
/** * Internal subscription state kept outside the public `Subscription` object. * * Storing this in a `WeakMap` keeps the public handle small while giving close, * cleanup, and abort logic one place to read and update state. */export interface StateMap<T> { /** Set once cancellation, completion, or error closes the subscription. */ closed: boolean;
/** Active observer callbacks. Cleared on close so references can be released. */ observer: Observer<T> | null;
/** Cleanup returned by the subscriber body. */ cleanup: Teardown;
/** Removes the linked `AbortSignal` listener when one was attached. */ removeAbortHandler?: (() => void) | null;}
/** * Central registry of subscription state. * * Using a WeakMap allows us to: * 1. Associate state with subscription objects without extending them * 2. Let the garbage collector automatically clean up entries when subscriptions are no longer referenced * 3. Hide implementation details from users */export const SubscriptionStateMap: WeakMap<Subscription, StateMap<unknown>> = new WeakMap();
/** * Creates a new Subscription object with properly initialized state. * * We validate observer methods early, ensuring type errors are caught * at subscription time rather than during event emission. * * The returned Subscription includes support for: * - Manual cancellation via `unsubscribe()` * - Automatic cleanup via `using` blocks (Symbol.dispose) * - Async cleanup contexts (Symbol.asyncDispose) * * @throws TypeError if observer methods are present but not functions * @internal */export function createSubscription<T>( observer: Observer<T>, opts?: { signal?: AbortSignal } | null,): Subscription { // Observer's methods should be functions if they exist if (observer.next !== undefined && typeof observer.next !== "function") { throw new TypeError("Observer.next must be a function"); } if (observer.error !== undefined && typeof observer.error !== "function") { throw new TypeError("Observer.error must be a function"); } if ( observer.complete !== undefined && typeof observer.complete !== "function" ) { throw new TypeError("Observer.complete must be a function"); }
// Create a local statemap to speed up access during hot-paths const stateMap: StateMap<T> = { closed: false, observer, cleanup: null, removeAbortHandler: null, };
/* ------------------------------------------------------------------- * Create the Subscription facade (spec: CreateSubscription()). * ------------------------------------------------------------------- */ const subscription: Subscription = { get [Symbol.toStringTag](): "Subscription" { return "Subscription" as const; },
/** * Returns whether this subscription is closed. * * A subscription becomes closed after: * - Explicit call to unsubscribe() * - Error notification * - Complete notification * * Once closed, no further events will be delivered to the observer, * and resources associated with the subscription are released. */ get closed() { return stateMap.closed; },
/** * Cancels the subscription and releases resources. * * - Safe to call multiple times (idempotent) * - Synchronously performs cleanup * - Marks subscription as closed * - Prevents further observer notifications * * This is the primary method for consumers to explicitly * terminate a subscription when they no longer need it. */ unsubscribe(): void { closeSubscription(this, stateMap); },
// Let subscriptions work with `using` for automatic cleanup. [Symbol.dispose]() { this.unsubscribe(); },
// Match async disposal APIs too. [Symbol.asyncDispose]() { return Promise.resolve(this.unsubscribe()); }, };
// Adds support for unsubscribing via AbortSignals const abortHandler = () => subscription?.unsubscribe(); const removeAbortHandler = () => opts?.signal?.removeEventListener("abort", abortHandler); stateMap.removeAbortHandler = removeAbortHandler;
// Initialize shared state before any early-close path so subscribe() can // still call observer.start(subscription) with a valid, already-closed // subscription facade when needed. SubscriptionStateMap.set(subscription, stateMap);
if (opts?.signal?.aborted) { closeSubscription(subscription, stateMap); return subscription; } opts?.signal?.addEventListener?.("abort", abortHandler, { once: true }); return subscription;}
/** * Mark subscription as closed and return observer reference. * Does NOT perform cleanup - that happens later. */export function markSubscriptionClosed<T>( state: StateMap<T> | undefined | null, returnObserver: true,): Observer<T> | null;/** * Marks a subscription as closed when the caller does not need the observer. */export function markSubscriptionClosed<T>( state: StateMap<T> | undefined | null, returnObserver: false,): undefined | null;/** * Marks a subscription as closed and optionally returns the detached observer. */export function markSubscriptionClosed<T>( state: StateMap<T> | undefined | null, returnObserver?: boolean,): Observer<T> | undefined | null;export function markSubscriptionClosed<T>( state: StateMap<T> | undefined | null, returnObserver = false,): Observer<T> | null | undefined { if (!state || state.closed) return null;
// Capture observer BEFORE marking closed (for spec compliance) const observer = state.observer;
// Mark as closed (this is what SubscriptionClosed checks) state.closed = true; state.observer = null;
// Return observer if requested (enables spec-compliant error/complete) if (returnObserver) return observer;}
/** * Perform cleanup if available. Safe to call multiple times. */export function performSubscriptionCleanup( subscription: Subscription, state?: StateMap<unknown> | null,): void { if (!state) return;
// Cache cleanup, abort signal and the abort handler before clearing let cleanup = state.cleanup; let removeAbortHandler = state.removeAbortHandler;
// Clear references first state.cleanup = null; state.removeAbortHandler = null;
try { removeAbortHandler?.(); cleanupSubscription(cleanup); } finally { SubscriptionStateMap.delete(subscription); cleanup = null; removeAbortHandler = null; }}
/** * Marks a subscription as closed and schedules necessary cleanup. * * This is the centralized implementation for all subscription termination paths: * - Manual unsubscribe() * - Observer.error() * - Observer.complete() * * The function ensures: * 1. Idempotency (safe to call multiple times) * 2. Cleanup happens exactly once * 3. State is properly cleared to prevent memory leaks * 4. WeakMap entry is removed to aid garbage collection * * @param subscription - The subscription to close * @internal */export function closeSubscription( subscription: Subscription, stateMap?: StateMap<unknown> | undefined | null,): Observer<unknown> | null | void { const state = stateMap ?? SubscriptionStateMap.get(subscription); const closed = markSubscriptionClosed(state!); if (closed === null) return;
// If we have an observer, perform cleanup performSubscriptionCleanup(subscription, state!);}
/** * Handles the actual cleanup process for a subscription. * * The spec allows three different types of cleanup values: * 1. Function: Called directly * 2. Object with unsubscribe method: unsubscribe() is called * 3. (deviate from spec) Object with Symbol.dispose/asyncDispose: dispose() is called * * Any errors during cleanup are reported asynchronously to prevent * them from disrupting the unsubscribe flow. * * @param cleanup - Function or object to perform cleanup * @internal */function cleanupSubscription(cleanup: Teardown) { let temp = cleanup; cleanup = null;
if (!temp) return; try { if (typeof temp === "function") temp(); else if (typeof temp === "object") { if (typeof (temp as SpecSubscription).unsubscribe === "function") { (temp as SpecSubscription).unsubscribe(); } else if ( typeof (temp as AsyncDisposable)[Symbol.asyncDispose] === "function" ) { (temp as AsyncDisposable)[Symbol.asyncDispose](); } else if (typeof (temp as Disposable)[Symbol.dispose] === "function") { (temp as Disposable)[Symbol.dispose](); } } } catch (err) { // Report cleanup errors asynchronously to avoid disrupting the unsubscribe flow queueMicrotask(() => { throw err; }); }
temp = null;}
/** * Wraps an observer with key guarantees required by the Observable specification. * * SubscriptionObserver is a critical component that ensures: * * 1. The observer contract is honored correctly * 2. Notifications stop after a subscription is closed * 3. Error/complete notifications properly terminate the subscription * 4. Observer methods are called with the correct `this` context * 5. Errors are properly propagated according to spec * * This wrapper acts as the intermediary between the Observable producer * and the consumer-provided Observer. * * @typeParam T - The type of values delivered by the parent Observable. */export class SubscriptionObserver<T> { /** Cached state map to improve perf. */ #state?: StateMap<T> | null;
/** Reference to the subscription that created this observer */ #subscription?: Subscription | null = null;
/** * Returns whether this observer's subscription is closed. * * Uses the single source of truth for closed state from SubscriptionStateMap. * This property is used by subscriber functions to check if they should * continue delivering events. * * @example * ```ts * const timer = new Observable(observer => { * const id = setInterval(() => { * if (!observer.closed) { * observer.next(Date.now()); * } * }, 1000); * return () => clearInterval(id); * }); * ``` */ get closed(): boolean { const state = this.#state; if (!state) return true; return state.closed ?? true; }
/** * Creates a new SubscriptionObserver attached to the given subscription. * * @param subscription - The subscription that created this observer */ constructor(subscription?: Subscription | null) { this.#subscription = subscription;
if (subscription) { this.#state = SubscriptionStateMap.get(subscription); if (!this.#state) throw new Error("Subscription state not found"); } }
/** * Delivers the next value to the observer if the subscription is open. * * This is typically the "hot path" in an Observable implementation, * as it's called for every emitted value. Key behaviors: * * 1. Silently returns if subscription is closed (no errors) * 2. Properly preserves observer's `this` context * 3. Catches and handles errors thrown from observer.next * 4. Forwards errors to observer.error when available * * Performance Considerations: * - Minimizes property access chains * - Early returns for closed subscriptions * - Type checking to avoid calling non-functions * * @param value - The value to deliver to the observer * * @example * ```ts * // Inside a subscriber function: * observer.next(42); // Delivers value to consumer * ``` * * > Note: Error-propagation policy * > ───────────────────────────── * > * If the *observer supplies its own `error()` handler*, * > that handler is considered the “catch-block” for the stream. * > ↳ Any exception that happens *inside* the user’s `next()` / * > `complete()` callbacks is forwarded to `error(err)` **once**. * > ↳ If `error()` itself throws, we still delegate to `HostReportErrors` (≈ “unhandled-promise rejection”) * > (i.e. `queueMicrotask`), exactly as the proposal specifies. * > * > * If the observer does **not** implement `error()`, we fall back to the * > spec’s `HostReportErrors` behaviour (queueMicrotask + throw) so the host * > surfaces the error just like an uncaught Promise rejection. * > * > Rationale – Think of `error()` as the moral equivalent of a `.catch()` * > on a Promise. Once a catch exists, the host no longer warns about * > “unhandled” rejections; we mirror that mental model here. * > * > Spec reference – This diverges slightly from stage-1, which still * > invokes HostReportErrors if the *error handler itself* throws. We * > intentionally suppress that extra surfacing for the reasons above. */ next(value: T) { const state = this.#state; if (!state || state.closed) return;
// Fast-path optimization to avoid long request chains const observer = state.observer; if (!observer) return;
const nextFn = observer.next; if (typeof nextFn !== "function") return;
try { nextFn.call(observer, value); } catch (err) { const errorFn = observer.error; if (typeof errorFn === "function") { try { errorFn.call(observer, err); } catch (err) { queueMicrotask(() => { throw err; }); } } // Either a user callback or HostReportErrors emulation (queueMicrotask). else {queueMicrotask(() => { throw err; });} } }
/** * Delivers an error notification to the observer, then closes the subscription. * * Error is a terminal operation - after calling it: * 1. The subscription is immediately marked as closed * 2. Resources are released via unsubscribe() * 3. No further notifications will be delivered * * Error Handling: * - If observer.error exists, the error is delivered there * - If observer.error throws, the error is reported asynchronously * - If no error handler exists, the error is reported asynchronously * * > Note: Even for "silent" errors (no error handler), we still close * the subscription and report the error to the host. * * ## * * @example Important Timing Consideration * When this method is called during the subscriber function execution (before it returns), * there's a potential race condition with cleanup functions. * * Consider: * ```ts * new Observable(observer => { * observer.error(new Error()); // Triggers unsubscribe here * return () => cleanupResources(); // But this hasn't been returned yet! * }); * ``` * * Our implementation handles this by: * 1. Marking the subscription as closed immediately * 2. Scheduling actual cleanup in a microtask to ensure the teardown function * has time to be captured and stored * * This ensures resources are properly cleaned up even when error/complete * is called synchronously during subscription setup. * * @param err - The error to deliver * * @example * ```ts * // Inside a subscriber function: * try { * doRiskyOperation(); * } catch (err) { * observer.error(err); // Terminates the subscription with error * } * ``` * * > Note: {@link SubscriptionObserver.next | Review the error propagation policy in `next()` on how errors propagate, the behaviour is not obvious on first glance.} */ error(err: unknown) { const state = this.#state;
// Mark closed and get observer in one call const observer = markSubscriptionClosed(state, true); if (observer === null) return;
const errorFn = observer?.error; if (typeof errorFn === "function") { try { errorFn.call(observer, err); } catch (innerErr) { queueMicrotask(() => { throw innerErr; }); } } // No error handler, delegate to host else {queueMicrotask(() => { throw err; });}
// Perform cleanup after marking closed performSubscriptionCleanup(this.#subscription!, state);
// Clear reference this.#subscription = null; }
/** * Signals successful completion of the observable sequence. * * Complete is a terminal operation - after calling it: * 1. The subscription is immediately marked as closed * 2. Resources are released via unsubscribe() * 3. No further notifications will be delivered * * If observer.complete throws an error: * - The error is forwarded to observer.error if available * - Otherwise, it's reported asynchronously to the host * * @example * ```ts * // Inside a subscriber function: * observer.next(1); * observer.next(2); * observer.complete(); // Terminates the subscription normally * ``` * * > Note: {@link SubscriptionObserver.next | Review the error propagation policy in `next()` on how errors propagate, the behaviour is not obvious on first glance.} */ complete() { const state = this.#state; if (!state || state.closed) return;
// Mark closed and get observer in one call const observer = markSubscriptionClosed(state, true); if (observer === null) return;
const completeFn = observer?.complete; if (typeof completeFn === "function") { try { completeFn.call(observer); } catch (err) { const errorFn = observer?.error; if (typeof errorFn === "function") { try { errorFn.call(observer, err); } catch (innerErr) { queueMicrotask(() => { throw innerErr; }); } } // Either a user callback or HostReportErrors emulation (queueMicrotask). else {queueMicrotask(() => { throw err; });} } }
// Perform cleanup after marking closed performSubscriptionCleanup(this.#subscription!, state);
// Clear reference this.#subscription = null; }
/** * Returns a standard string tag for the object. * Used by Object.prototype.toString. */ get [Symbol.toStringTag](): "Subscription Observer" { return "Subscription Observer" as const; }}
/** * `Observable` is the main type for values that show up over time. * * A Promise gives you one future value. An Observable can give you many * values, plus a clear way to stop listening and clean up. * * A few rules matter most: * 1. Nothing happens until `subscribe()` is called. * 2. Each subscription runs its own work unless you share the source. * 3. Cleanup runs when the subscription completes, fails, or is cancelled. * 4. Infinite streams need `unsubscribe()` or a `using` block. * * This implementation also lets you consume values with `for await` and clean * up with `Symbol.dispose` or `Symbol.asyncDispose`. * * @typeParam T - Type of values emitted by this Observable */export class Observable<T> implements AsyncIterable<T>, SpecObservable<T>, ObservableProtocol<T> { /** The subscriber function provided when the Observable was created */ #subscribeFn: (obs: SubscriptionObserver<T>) => Teardown;
/** * Creates a new Observable with the given subscriber function. * * Creating an Observable does not start any work. It only stores the * function that should run later when someone subscribes. * * That function gets an observer it can send values to, and it can return * cleanup logic for timers, event listeners, sockets, or other resources. * * @param subscribeFn - Function that implements the Observable's behavior * * The observer lets the source: * - `observer.next(value)` - Emit a value * - `observer.error(err)` - Emit error (terminates) * - `observer.complete()` - Signal completion (terminates) * - `observer.closed` - Check if subscription is still active * * @throws TypeError if subscribeFn is not a function * @throws TypeError if Observable is called without "new" * * @example Timer with cleanup * ```ts * // Timer that emits the current timestamp every second * const timer = new Observable(observer => { * console.log('Subscription started!'); * const id = setInterval(() => { * observer.next(Date.now()); * }, 1000); * * // Return cleanup function * return () => { * console.log('Cleaning up timer'); * clearInterval(id); * }; * }); * ``` * * @example Async operation with error handling * ```ts * const fetch = new Observable(observer => { * const controller = new AbortController(); * * fetch('/api/data', { signal: controller.signal }) * .then(res => res.json()) * .then(data => { * observer.next(data); * observer.complete(); * }) * .catch(err => observer.error(err)); * * return () => controller.abort(); // Cleanup * }); * ``` */ constructor(subscribeFn: (obs: SubscriptionObserver<T>) => Teardown) { if (typeof subscribeFn !== "function") { throw new TypeError("Observable initializer must be a function"); }
// Add check for constructor invocation if (!(this instanceof Observable)) { throw new TypeError("Observable must be called with new"); }
this.#subscribeFn = subscribeFn; }
/** * Returns this Observable from `[Symbol.observable]()`. * * Other libraries use this method to treat the instance as an Observable-like * source. * * @returns This Observable instance */ [Symbol.observable](): Observable<T> { return this; }
/** * Subscribes to this Observable with an observer object. * * This starts the source, sends values to the observer callbacks, and * returns a subscription you can cancel. * * `observer.start()` is for observing the newly-created subscription or * cancelling it before the subscriber body runs. It is not where teardown is * registered. Cleanup is still sourced from the subscriber function's return * value. * * The subscription starts right away. It keeps going until the source * finishes or you cancel it. * * If you provide an `error` callback, stream failures go there. If you do * not, they are reported to the host environment. * * Infinite Observables need manual cleanup. If the source never finishes, * call `unsubscribe()` yourself or use a `using` block. * * For long-lived subscriptions, consider: * 1. Using a `using` block with this subscription * 2. Setting up a timeout or take-until condition * 3. Explicitly calling `unsubscribe()` when no longer needed * * @param observer - Object with next/error/complete callbacks * @param opts.signal - Optional AbortSignal to close subscription * @returns Subscription object that can be used to cancel the subscription * * @example Observer object * ```ts * const subscription = observable.subscribe({ * next(value) { console.log('Received:', value) }, * error(err) { console.error('Error:', err) }, * complete() { console.log('Done!') } * }); * * // Later, to cancel: * subscription.unsubscribe(); * ``` * * @example Two ways to subscribe * ```ts * // Observer object (recommended) * obs.subscribe({ * start(sub) { console.log('Started, can call sub.unsubscribe()'); }, * next(val) { console.log('Value:', val); }, * error(err) { console.error('Error:', err); }, * complete() { console.log('Done'); } * }); * * // Separate functions * obs.subscribe( * val => console.log(val), * err => console.error(err), * () => console.log('done') * ); * ``` */ subscribe( observer: Observer<T>, opts?: { signal?: AbortSignal }, ): Subscription;
/** * Subscribes to this Observable with callback functions. * * Convenience overload that wraps the callbacks in an Observer object. * See the documentation for the observer-based overload for details * on subscription behavior. * * This overload does the same work as the observer-based form: it starts the * source, sends values to your callbacks, and returns a subscription you can * cancel. * * `observer.start()` is only for seeing or cancelling the subscription * before the subscriber body runs. It is not where cleanup is registered. * Cleanup still comes from the value returned by the subscriber function. * * The subscription starts right away. It keeps going until the source * finishes or you cancel it. * * If you provide an `error` callback, stream failures go there. If you do * not, they are reported to the host environment. * * Infinite Observables need manual cleanup. If the source never finishes, * call `unsubscribe()` yourself or use a `using` block. * * For long-lived subscriptions, consider: * 1. Using a `using` block with this subscription * 2. Setting up a timeout or take-until condition * 3. Explicitly calling `unsubscribe()` when no longer needed * * @param next - Function to handle each emitted value * @param error - Optional function to handle errors * @param complete - Optional function to handle completion * @param opts.signal - Optional AbortSignal to close subscription * @returns Subscription object that can be used to cancel the subscription * * @example * ```ts * const subscription = observable.subscribe( * value => console.log('Received:', value), * err => console.error('Error:', err), * () => console.log('Done!') * ); * ``` * * @example Two ways to subscribe * ```ts * // Observer object (recommended) * obs.subscribe({ * start(sub) { console.log('Started, can call sub.unsubscribe()'); }, * next(val) { console.log('Value:', val); }, * error(err) { console.error('Error:', err); }, * complete() { console.log('Done'); } * }); * * // Separate functions * obs.subscribe( * val => console.log(val), * err => console.error(err), * () => console.log('done') * ); * ``` */ subscribe( next: (value: T) => void, error?: (e: unknown) => void, complete?: () => void, opts?: { signal?: AbortSignal }, ): Subscription;
/** * Implementation of subscribe method (handles both overloads). */ subscribe( observerOrNext: Observer<T> | ((value: T) => void), errorOrOpts?: ((e: unknown) => void) | { signal?: AbortSignal }, complete?: () => void, _opts?: { signal?: AbortSignal }, ): Subscription { // Check for invalid this context if (this === null || this === undefined) { throw new TypeError( 'Cannot read property "subscribe" of null or undefined', ); }
/* ------------------------------------------------------------------- * 1. Normalise the observer – mirrors spec step 4. * ------------------------------------------------------------------- */ const observer: Observer<T> | null = ( typeof observerOrNext === "function" ? { next: observerOrNext, error: errorOrOpts as (e: unknown) => void, complete, } : observerOrNext ) ?? {}; // ← spec-compliant fallback for null / primitives
// Additional options to pass along AbortSignal (part of the WCIG Observables Spec., thought to implement it for convinence reasons) const opts = (typeof observerOrNext === "function" ? _opts : errorOrOpts as typeof _opts) ?? {};
/* ------------------------------------------------------------------- * 2. Create the Subscription facade (spec: CreateSubscription()). * ------------------------------------------------------------------- */ const subscription: Subscription = createSubscription(observer, opts);
/* ------------------------------------------------------------------- * 3. Call observer.start(subscription) – (spec step 10). * ------------------------------------------------------------------- */ try { observer.start?.(subscription); if (subscription?.closed) return subscription; // spec step 10.d } catch (err) { // WarnIfAbrupt: report, but return closed subscription // Queue in a micro-task so it surfaces *after* current job, // matching the spec’s “report later” intent. queueMicrotask(() => { // 1. Print to console for visibility console.error(err);
// 2. Re-throw so debuggers break (optional, but common) throw err; });
subscription?.unsubscribe?.(); return subscription; }
/* ------------------------------------------------------------------- * 4. Wrap user observer so we enforce closed-state. * ------------------------------------------------------------------- */ const subObserver = new SubscriptionObserver<T>(subscription);
/* ------------------------------------------------------------------- * 5. Execute the user subscriber and capture its cleanup (spec step 12-16). * ------------------------------------------------------------------- */ try { let cleanup = this.#subscribeFn?.call(undefined, subObserver) ?? null;
// Validate the cleanup value if provided if (cleanup !== undefined && cleanup !== null) { if ( !( typeof cleanup === "function" || typeof (cleanup as SpecSubscription)?.unsubscribe === "function" || typeof (cleanup as Disposable)?.[Symbol.dispose] === "function" || typeof (cleanup as AsyncDisposable)?.[Symbol.asyncDispose] === "function" ) ) { throw new TypeError( "Expected subscriber to return a function, an unsubscribe object, a disposable with a [Symbol.dispose] method, an async-disposable with a [Symbol.asyncDispose] method, or undefined/null", ); } }
// Store the cleanup function in the subscription state const state = SubscriptionStateMap.get(subscription); if (state && cleanup) state.cleanup = cleanup;
/** * Handle the case where complete/error was called synchronously during the subscribe function. * This is a critical edge case that requires special handling - when the observer * calls `error()` or `complete()` before the subscribe function returns, we need to ensure * that any teardown function returned by the subscriber is still executed properly. * * The returned teardown wouldn't have been available when `unsubscribe()` was initially * triggered by error/complete, so we need to handle it manually here. * * @example * ```ts * const errorObservable = new Observable(observer => { * observer.error(new Error("test error")); // Will auto-unsubscribe (but teardown hasn't been defined yet) * log.push("after error"); // This should still run * * // Teardown now defined but now the subscription has been closedn * // but resources being used haven't actually been disposed yet * return () => { * log.push("error teardown"); * }; * }); * ``` * * `observer.error` fires before the teardown function is defined, so we would need to manually cleanup ourselves * by manually running the teardown function */ if (subscription.closed && cleanup) { cleanupSubscription(cleanup); }
cleanup = null; } catch (err) { // 6) If their subscribeFn throws, send that as an error notification subObserver.error(err); }
// 7) Finally, hand back the Subscription so callers can cancel whenever they like return subscription; }
/** * Visits every value in this Observable and resolves when it completes. * * This is the terminal-consumer counterpart to `tap()`. `tap()` stays inside * a pipeline and returns another Observable. `forEach()` consumes the stream, * runs your callback for each value, and returns a Promise that settles when * the stream ends. * * Use this when you want Promise-style completion semantics without the * async-iterator overhead of `pull()` or `for await ... of`. * * If the callback throws, the returned Promise rejects and the subscription * is cancelled immediately. * * Like the standalone helper, this method still tears down correctly when a * foreign Observable-like source emits synchronously before `subscribe()` * returns its subscription object. * * @param callback - Function invoked for each emitted value * @param opts.signal - Optional AbortSignal to cancel traversal * @returns Promise that resolves with `undefined` on completion * * @example Visiting values through the instance method * ```ts * await Observable.of(1, 2, 3).forEach((value, index) => { * console.log(index, value); * }); * ``` */ forEach( callback: (value: T, index: number) => void, opts?: { signal?: AbortSignal }, ): Promise<void> { return forEach(this, callback, opts); }
/** * Enables `for await ... of observable` syntax for direct async iteration. * * This method allows Observables to be used in any context that accepts an AsyncIterable, * implementing the "pull" mode of consuming an Observable. * * Uses default buffer size of 64 items. * * The implementation delegates to the `pull()` function which: * 1. Converts push-based events to pull-based async iteration * 2. Applies backpressure with ReadableStream * 3. Handles proper cleanup on early termination * * @returns An AsyncIterator that yields values from this Observable * * @example * ```ts * const observable = Observable.of(1, 2, 3); * * // Using for-await-of directly on an Observable * for await (const value of observable) { * console.log(value); // Logs 1, 2, 3 * } * ``` */ async *[Symbol.asyncIterator](): AsyncIterator<T> { yield* pull(this); }
/** * Converts this Observable into an AsyncGenerator with backpressure control. * * **Why use this**: Control buffer size to prevent memory issues when producer is faster than consumer. * Uses ReadableStream internally for efficient buffering. * * This method provides more control over async iteration than the default * Symbol.asyncIterator implementation, allowing consumers to: * * 1. Specify a queuing strategy with a custom highWaterMark * 2. Control buffering behavior when the producer is faster than the consumer * 3. Apply backpressure to prevent memory issues with fast producers * * The implementation uses ReadableStream internally to manage buffering * and backpressure, pausing the producer when the buffer fills up. * * **Buffer sizing**: * - Small (1-10): Memory-constrained environments, large data items * - Medium (10-100): Most applications, good balance * - Large (100+): High-throughput scenarios, small items * * **Error handling**: Errors are sent through value channel (not stream errors) to ensure * all buffered values are processed before error is thrown. * * @param options - Configuration options for the pull operation * @param options.strategy.highWaterMark - Max items to buffer before applying backpressure (default: 64) * @returns Async generator that yields values and slows the producer when the * buffer is full. * * @example Memory-efficient processing * ```ts * // Buffer up to 5 items before applying backpressure * for await (const value of observable.pull({ * strategy: { highWaterMark: 5 } * })) { * console.log(value); * // Slow consumer - producer will pause when buffer fills * await new Promise(r => setTimeout(r, 1000)); * } * * // Large items, tiny buffer * for await (const item of largeDataStream.pull({ strategy: { highWaterMark: 1 } })) { * await processLargeItem(item); // Producer pauses when buffer full * } * * // High throughput, large buffer * for await (const event of fastStream.pull({ strategy: { highWaterMark: 1000 } })) { * await processFast(event); * } * ``` */ pull( opts?: Parameters<typeof pull>[1] & { ignoreError?: true }, ): AsyncGenerator<T>; /** * Returns pulled values while preserving `ObservableError` objects in the * yielded value channel. */ pull( opts?: Parameters<typeof pull>[1] & { ignoreError?: false }, ): AsyncGenerator<T | ObservableError>; pull( opts?: Parameters<typeof pull>[1] & { ignoreError?: boolean }, ): AsyncGenerator<T | ObservableError> { return pull(this, opts); }
/** * Turns a Promise, iterable, async iterable, or Observable-like value into * an Observable. * * **Handles**: * - Arrays, Sets, Maps → sync emission * - Async generators → values over time * - Symbol.observable objects → delegates to their implementation * * What happens depends on the input type: * 1. Objects with Symbol.observable - Delegates to their implementation * 2. Synchronous iterables - Emits all values then completes * 3. Asynchronous iterables - Emits values as they arrive then completes * 4. Promise - Emits a single value (the resovled value) then completes * * Unlike `Promise.resolve`, `Observable.from` does not always return the * input unchanged when it already looks Observable-like. That keeps behavior * consistent across different Observable libraries. * * @param input - The object to convert to an Observable * @returns A new Observable that emits values from the input * * @example * ```ts * // From an array * Observable.from([1, 2, 3]).subscribe({ * next: val => console.log(val) // 1, 2, 3 * }); * * // From a Promise * Observable.from(Promise.resolve("result")).subscribe({ * next: val => console.log(val) // "result" * }); * * // From another Observable-like object * const foreign = { * [Symbol.observable]() { * return new Observable(obs => { * obs.next("hello"); * obs.complete(); * }); * } * }; * Observable.from(foreign).subscribe({ * next: val => console.log(val) // "hello" * }); * ``` */ static readonly from: typeof from = from;
/** * Creates an Observable that synchronously emits the given values then completes. * * This is a convenience method for creating simple Observables that: * 1. Emit a fixed set of values synchronously * 2. Complete immediately after emitting all values * 3. Never error * * It's the Observable equivalent of `Promise.resolve()` for single values * or `[].values()` for multiple values. * * @param items - Values to emit * @returns A new Observable that emits the given values then completes * * @example * ```ts * // Create and subscribe * Observable.of(1, 2, 3).subscribe({ * next: val => console.log(val), // Logs 1, 2, 3 * complete: () => console.log('Done!') * }); * * // Output: * // 1 * // 2 * // 3 * // Done! * ``` */ static readonly of: typeof of = of;
/** * Converts a Observable into an AsyncGenerator with backpressure control. * * This method provides more control over async iteration than the default * Symbol.asyncIterator implementation, allowing consumers to: * * 1. Specify a queuing strategy with a custom highWaterMark * 2. Control buffering behavior when the producer is faster than the consumer * 3. Apply backpressure to prevent memory issues with fast producers * * The implementation uses ReadableStream internally to manage buffering * and backpressure, pausing the producer when the buffer fills up. * * @param options - Configuration options for the pull operation * @returns An AsyncGenerator that yields values from this Observable * * @example * ```ts * // Buffer up to 5 items before applying backpressure * for await (const value of observable.pull({ * strategy: { highWaterMark: 5 } * })) { * console.log(value); * // Slow consumer - producer will pause when buffer fills * await new Promise(r => setTimeout(r, 1000)); * } * ``` */ static readonly pull: typeof pull = pull;
/** * Standard string tag for the object. * Used by Object.prototype.toString. */ get [Symbol.toStringTag](): "Observable" { return "Observable"; }}
/** * Cached empty observable handler * @internal */function EMPTY(obs: SubscriptionObserver<unknown>) { obs.complete();}
/** * Creates an Observable that synchronously emits the given values then completes. * * This standalone function implements the Observable.of static method while * properly supporting subclassing. It's the Observable equivalent of: * - `Array.of()` for collections * - `Promise.resolve()` for single values * * Key behaviors: * 1. Emits values synchronously when subscribed * 2. Completes immediately after all values are emitted * 3. Never errors * 4. Respects the constructor it was called on for subclassing * * @param items - Values to emit * @returns A new Observable that emits the given values then completes * * @example * ```ts * // Basic usage * of(1, 2, 3).subscribe({ * next: x => console.log(x), * complete: () => console.log('Done!') * }); * // Output: 1, 2, 3, Done! * * // Subclassing support * class MyObservable extends Observable<number> { * // Custom methods... * } * * // Creates a MyObservable instance * const mine = MyObservable.of(1, 2, 3); * ``` */export function of<T>(this: unknown, ...items: T[]): Observable<T> { const Constructor = typeof this === "function" ? this as typeof Observable<T> : Observable; const len = items.length;
// Pre-defined handlers for common cases to avoid creating new closures switch (len) { case 0: return new Constructor(EMPTY); case 1: return new Constructor((obs) => { obs.next(items[0]); obs.complete(); }); case 2: return new Constructor((obs) => { obs.next(items[0]); obs.next(items[1]); obs.complete(); }); case 3: return new Constructor((obs) => { obs.next(items[0]); obs.next(items[1]); obs.next(items[2]); obs.complete(); }); default: // For arrays > 3 items, balance between code size and performance return new Constructor((obs) => { // Based on benchmarking: // - Arrays < 100: simple loop is fine (method call dominates) // - Arrays >= 100: unrolling provides measurable benefit if (len < 100) { for (let i = 0; i < len; i++) { obs.next(items[i]); } } else { // Unroll by 8 for large arrays (2.8x speedup) let i = 0; const limit = len - (len % 8);
for (; i < limit; i += 8) { obs.next(items[i]); obs.next(items[i + 1]); obs.next(items[i + 2]); obs.next(items[i + 3]); obs.next(items[i + 4]); obs.next(items[i + 5]); obs.next(items[i + 6]); obs.next(items[i + 7]); }
// Handle remainder for (; i < len; i++) { obs.next(items[i]); } }
obs.complete(); }); }}
/** * Converts an Observable-like, sync iterable, or async iterable into an Observable. * * This is the standalone implementation of Observable.from, supporting: * - Objects with Symbol.observable (Observable-like) * - Regular iterables (arrays, Maps, Sets, generators) * - Async iterables (async generators, ReadableStreams) * * Conversion follows these rules: * 1. For Symbol.observable objects: delegates to their implementation * 2. For Promises: resolves and emits the promise's value * 3. For iterables: synchronously emits all values, then completes * 4. For async iterables: emits values as they arrive, then completes * * This function properly supports subclassing, preserving the constructor * it was called on. * * @throws TypeError if input is null, undefined, or not convertible * * @example * ```ts * // From array * from([1, 2, 3]).subscribe(x => console.log(x)); * // Output: 1, 2, 3 * * // From Promise * from(Promise.resolve('done')).subscribe(x => console.log(x)); * // Output: 'done' * * // From Map * from(new Map([['a', 1], ['b', 2]])).subscribe(x => console.log(x)); * // Output: ['a', 1], ['b', 2] * * // From another Observable implementation * const foreign = { * [Symbol.observable]() { * return { subscribe: observer => { * observer.next('hello'); * observer.complete(); * return { unsubscribe() {} }; * }}; * } * }; * from(foreign).subscribe(x => console.log(x)); * // Output: 'hello' * ``` */export function from<T>( this: unknown, input: | SpecObservable<T> | Iterable<T> | AsyncIterable<T> | PromiseLike<T> | ArrayLike<T>, { throwError = true } = {},): Observable<T> { if (input === null || input === undefined) { throw new TypeError("Cannot convert undefined or null to Observable"); }
const Constructor = typeof this === "function" ? this as typeof Observable<T> : Observable;
// Faster implementation of iteration for array-like values const arr = input as ArrayLike<T>; if (Array.isArray(input) || typeof arr.length === "number") { const len = arr.length;
// Optimize for small arrays if (len === 0) return new Constructor(EMPTY); if (len === 1) { return new Constructor((obs) => { if (throwError) assertObservableError(arr[0], obs); obs.next(arr[0]); obs.complete(); }); }
// Type check to ensure it's actually array-like return new Constructor((obs) => { try { // Typed arrays: no bounds checking needed, direct iteration // Small arrays: simple loop with early exit checks if (len < 100 || ArrayBuffer.isView(arr)) { for (let i = 0; i < len; i++) { if (throwError) assertObservableError(arr[i]);
obs.next(arr[i]); if (obs.closed) return; } } else { // Large arrays: unroll with less frequent closed checks let i = 0; const limit = len - (len % 8);
// Check closed once per 8 items (balanced approach) for (; i < limit; i += 8) { if (throwError) { assertObservableError(arr[i]); assertObservableError(arr[i + 1]); assertObservableError(arr[i + 2]); assertObservableError(arr[i + 3]); assertObservableError(arr[i + 4]); assertObservableError(arr[i + 5]); assertObservableError(arr[i + 6]); assertObservableError(arr[i + 7]); }
obs.next(arr[i]); obs.next(arr[i + 1]); obs.next(arr[i + 2]); obs.next(arr[i + 3]); obs.next(arr[i + 4]); obs.next(arr[i + 5]); obs.next(arr[i + 6]); obs.next(arr[i + 7]); if (obs.closed) return; }
// Handle remainder with checks for (; i < len; i++) { if (throwError) assertObservableError(arr[i]); obs.next(arr[i]); if (obs.closed) return; } } } catch (err) { obs.error(err); }
obs.complete(); }); }
// Case 1 – object with @@observable const observableFn = (input as SpecObservable<T>)[Symbol.observable]; if (typeof observableFn === "function") { const observable = observableFn.call(input);
// Validate the result has a subscribe method if (!observable || typeof observable.subscribe !== "function") { throw new TypeError( "Object returned from [Symbol.observable]() does not implement subscribe method", ); }
// Return directly if it's already an instance of the target constructor if (observable instanceof Constructor) return observable as Observable<T>;
// Otherwise, wrap it to ensure consistent behavior return new Constructor((observer) => { const sub = observable.subscribe(observer); return () => sub?.unsubscribe?.(); }); }
// Fast implementation for Set & Maps which are generally optimized // by the runtime when using `for..of` loops if (input instanceof Set || input instanceof Map) { const collection = input as Set<T> | Map<unknown, unknown>; const size = collection.size; if (size === 0) return new Constructor(EMPTY);
return new Constructor((obs) => { // For...of is optimized for Sets in V8 for (const item of collection) { if (throwError) assertObservableError(item, obs);
obs.next(item as T); if (obs.closed) return; }
obs.complete(); }); }
// Case 2 – promise const promise = input as PromiseLike<T>; if (typeof promise.then === "function") { return new Constructor((obs) => { promise.then( (value) => { if (throwError) assertObservableError(value, obs); obs.next(value); obs.complete(); }, // Error during iteration (err) => obs.error(err), ); }); }
// Case 3 – synchronous iterable const iteratorFn = (input as Iterable<T>)[Symbol.iterator]; if (typeof iteratorFn === "function") { return new Constructor((obs) => { const iterator = iteratorFn.call(input);
try { for (let step = iterator.next(); !step.done; step = iterator.next()) { if (throwError) assertObservableError(step.value); obs.next(step.value);
// If subscription was closed during iteration, clean up and exit if (obs.closed) break; }
obs.complete(); } catch (err) { obs.error(err); }
return () => { if (typeof iterator?.return === "function") { try { iterator.return(); // IteratorClose } catch (err) { queueMicrotask(() => { throw err; }); } } }; }); }
// Case 4 – async iterable const asyncIteratorFn = (input as AsyncIterable<T>)[Symbol.asyncIterator]; if (typeof asyncIteratorFn === "function") { return new Constructor((obs) => { const asyncIterator = asyncIteratorFn.call(input);
// Start consuming the async iterable (async () => { try { for ( let step = await asyncIterator.next(); !step.done; step = await asyncIterator.next() ) { if (throwError) assertObservableError(step.value); obs.next(step.value);
// If subscription was closed during iteration, clean up and exit if (obs.closed) break; }
// Normal completion obs.complete(); } catch (err) { // Error during iteration obs.error(err); } })();
return () => { if (typeof asyncIterator?.return === "function") { try { asyncIterator.return(); // IteratorClose } catch (err) { queueMicrotask(() => { throw err; }); } } }; }); }
throw new TypeError( "Input is not Observable, Iterable, AsyncIterable, Promise, or ReadableStream", );}
/** * Converts an Observable into an AsyncGenerator with backpressure control. * * This function bridges the gap between push-based Observables and * pull-based async iteration, allowing consumers to: * 1. Process values at their own pace * 2. Use standard async iteration patterns (for-await-of) * 3. Control buffering behavior to prevent memory issues * * ## How It Works * Observable → ReadableStream (for buffering) → AsyncGenerator * * **Key features**: * - Automatic backpressure when consumer slower than producer * - Configurable buffer size via highWaterMark * - Proper cleanup on early termination * - Errors sent through value channel to preserve buffered items * * Implementation details: * - Uses ReadableStream as the backpressure mechanism * - Connects the Observable to the stream as a source * - Returns an AsyncGenerator that yields values from the stream * - Handles proper cleanup on early termination * * Instead of using ReadableStream's error mechanism, this implementation uses a special * approach to error handling: errors are wrapped in `ObservableError` objects and * sent through the normal value channel. This ensures all values emitted before an error * are properly processed in order before the error is thrown. * * ## Key Benefits * * 1. **Controlled Processing**: Process values at your own pace rather than being overwhelmed * 2. **Proper Backpressure**: When your consumer is slow, the producer automatically slows down * 3. **Complete Error Handling**: Errors don't cause queued values to be lost * 4. **Resource Safety**: Automatically cleans up subscriptions, even with early termination * 5. **Memory Efficiency**: Controls buffer size to prevent memory issues with fast producers * 6. **Iterator Integration**: Natural integration with other async iteration tools * * @param observable - Source Observable to pull values from * @param options - Configuration options for the ReadableStream * @param options.strategy - Queuing strategy that controls how backpressure is applied * @param options.strategy.highWaterMark - Buffer size before backpressure (default: 64) * * @returns An AsyncGenerator that yields values from the Observable at the consumer's pace * * @example Basic usage with for-await-of loop: * ```ts * const numbers = Observable.of(1, 2, 3, 4, 5); * * // Process each value at your own pace * for await (const num of pull(numbers)) { * console.log(`Processing ${num}`); * await someTimeConsumingOperation(num); * } * // Output: * // Processing 1 * // Processing 2 * // Processing 3 * // Processing 4 * // Processing 5 * * const fast = new Observable(obs => { * let count = 0; * const id = setInterval(() => obs.next(count++), 10); // 100/sec * return () => clearInterval(id); * }); * * // Slow consumer with small buffer * for await (const n of pull(fast, { strategy: { highWaterMark: 5 } })) { * console.log(n); * await new Promise(r => setTimeout(r, 1000)); // 1/sec - producer slows down * } * ``` * * @example Handling errors while ensuring all prior values are processed: * ```ts * // Observable that emits values then errors * const source = new Observable(observer => { * observer.next(1); * observer.next(2); * observer.error(new Error("Something went wrong")); * // Even though an error occurred, both 1 and 2 will be processed * }); * * try { * for await (const value of pull(source)) { * console.log(`Got value: ${value}`); * } * } catch (err) { * console.error(`Error caught: ${err.message}`); * } * * // Output: * // Got value: 1 * // Got value: 2 * // Error caught: Something went wrong * ``` * * @example Controlling buffer size for memory efficiency: * ```ts * // Create a producer that emits values rapidly * const fastProducer = new Observable(observer => { * let count = 0; * const interval = setInterval(() => { * observer.next(count++); * if (count > 1000) { * clearInterval(interval); * observer.complete(); * } * }, 1); * return () => clearInterval(interval); * }); * * // Limit buffer to just 5 items to prevent memory issues * for await (const num of pull(fastProducer, { * strategy: { highWaterMark: 5 } * })) { * console.log(`Processing ${num}`); * // Slow consumer - producer will pause when buffer fills * await new Promise(r => setTimeout(r, 100)); * } * ``` */export function pull<T>( this: unknown, observable: SpecObservable<T>, opts?: { strategy?: QueuingStrategy<T | ObservableError>; throwError?: true },): AsyncGenerator<T>;/** * Converts an Observable into an AsyncGenerator that yields both values and * wrapped `ObservableError` objects when `throwError` is disabled. */export function pull<T>( this: unknown, observable: SpecObservable<T>, opts?: { strategy?: QueuingStrategy<T | ObservableError>; throwError?: false; },): AsyncGenerator<T | ObservableError>;export async function* pull<T>( this: unknown, observable: SpecObservable<T>, { strategy = { highWaterMark: 64 }, throwError = true }: { strategy?: QueuingStrategy<T | ObservableError>; throwError?: boolean; } = {},): AsyncGenerator<T | ObservableError> { const obs = observable?.[Symbol.observable]?.(); let sub: SpecSubscription | null = null;
// Create a ReadableStream that will buffer values from the Observable const stream = new ReadableStream<T | ObservableError>({ start: (ctrl) => { // Subscribe to the Observable and connect it to the stream sub = obs?.subscribe({ // Normal values flow directly into the stream next: (v) => ctrl.enqueue(v),
// Errors are wrapped as special values rather than using stream.error() // This ensures values emitted before the error are still processed error: (e) => { ctrl.enqueue(ObservableError.from(e, "observable:pull")); sub = null; },
// Close the stream when the Observable completes complete: () => { ctrl.close(); sub = null; }, }); },
// Clean up the subscription if the stream is cancelled // This happens when the AsyncGenerator is terminated early cancel: () => { sub?.unsubscribe(); sub = null; }, }, strategy);
// Get a reader for the stream and yield values as they become available const reader = stream.getReader();
try { while (true) { // Wait for the next value (with backpressure automatically applied) const { value, done } = await reader.read();
// If we received a wrapped error, unwrap and throw it if (throwError) assertObservableError(value);
// If the stream is done (Observable completed), exit the loop if (done) break;
// Otherwise, yield the value to the consumer yield value as T; } } finally { // Ensure resources are cleaned up even if iteration is terminated early // This guarantees no memory leaks, even with break or thrown exceptions reader.releaseLock(); await stream.cancel(); }}
/** * Shared mutable state for one `forEach()` traversal. * * Some Observable-like sources synchronously call observer methods before * `subscribe()` returns the subscription handle. This object keeps callback * progress, cancellation state, and the eventual subscription in one place so * every exit path can coordinate correctly. */interface ForEachState<T> { /** Function invoked for each emitted value. */ callback: (value: T, index: number) => void; /** Internal signal used to stop the source after callback failure. */ controller: AbortController; /** Deferred promise returned from `forEach()`. */ deferred: PromiseWithResolvers<void>; /** Optional caller-owned cancellation signal. */ externalSignal?: AbortSignal; /** Zero-based callback index. */ index: number; /** Stored so the abort listener can be removed exactly once. */ onExternalAbort: (() => void) | null; /** Prevents multiple resolve or reject attempts. */ settled: boolean; /** Subscription returned by the source, if it has been produced yet. */ subscription: SpecSubscription | null;}
/** * Settles the traversal promise and removes the external abort listener. * * `forEach()` can finish in several ways: normal completion, source error, * callback failure, or external cancellation. This helper makes sure only one * of those paths wins, and that the Promise plus abort-listener cleanup happen * once. */function settleForEach<T>( state: ForEachState<T>, result: { ok: true } | { ok: false; reason: unknown },): void { if (state.settled) return;
state.settled = true;
if (state.onExternalAbort) { state.externalSignal?.removeEventListener?.('abort', state.onExternalAbort); state.onExternalAbort = null; }
if (result.ok) { state.deferred.resolve(); return; }
state.deferred.reject(result.reason);}
/** * Visits every value in an Observable and resolves when the stream completes. * * This is the tree-shakeable terminal-consumer helper for workflows that want * Promise completion semantics without going through async iteration. It is the * consumer-side counterpart to `tap()`: `tap()` keeps the stream alive inside a * pipeline, while `forEach()` consumes the stream and returns a Promise. * * The callback receives the emitted value and a zero-based index. If the * callback throws, the Promise rejects and the subscription is cancelled. * * Some Observable-like sources synchronously emit before their `subscribe()` * call returns. `forEach()` still tears those sources down correctly by routing * callback failures and `AbortSignal` cancellation through one shared teardown * path. * * This helper follows the WICG Observable draft's promise-returning operator * shape more closely than `zen-observable`'s callback-plus-cancel signature. * * @param observable - Observable or Observable-like source to consume * @param callback - Function invoked for each emitted value * @param opts.signal - Optional AbortSignal to cancel traversal * @returns Promise that resolves with `undefined` on completion * * @example Visiting each emitted value with an index * ```ts * await forEach(Observable.of("a", "b"), (value, index) => { * console.log(index, value); * }); * ``` * * @example Cancelling traversal with an AbortSignal * ```ts * const controller = new AbortController(); * * const done = forEach(source, (value) => { * if (value === 10) controller.abort(new Error("stopped early")); * }, { signal: controller.signal }); * ``` */export function forEach<T>( this: unknown, observable: SpecObservable<T>, callback: (value: T, index: number) => void, opts: { signal?: AbortSignal } = {},): Promise<void> { if (typeof callback !== "function") { return Promise.reject(new TypeError("forEach callback must be a function")); }
const obs = observable?.[Symbol.observable]?.(); if (!obs || typeof obs.subscribe !== "function") { return Promise.reject(new TypeError("Expected an Observable-like value")); }
if (opts.signal?.aborted) { return Promise.reject(opts.signal.reason); }
const deferred = Promise.withResolvers<void>();
// `subscribe()` can synchronously emit before it returns. This shared state // lets callback code, abort handling, and final settlement all coordinate // even when the subscription handle arrives last. const state: ForEachState<T> = { callback, controller: new AbortController(), deferred, externalSignal: opts.signal, index: 0, onExternalAbort: null, settled: false, subscription: null, };
// External cancellation should behave the same as callback failure: stop the // source, release the subscription once, and reject with the abort reason. const onExternalAbort = () => { const reason = opts.signal?.reason;
if (!state.controller.signal.aborted) { state.controller.abort(reason); }
state.subscription?.unsubscribe(); state.subscription = null; settleForEach(state, { ok: false, reason }); }; state.onExternalAbort = onExternalAbort; opts.signal?.addEventListener?.('abort', onExternalAbort, { once: true });
try { state.subscription = ( obs as { subscribe( observer: Observer<T>, opts?: { signal?: AbortSignal }, ): SpecSubscription; } ).subscribe({ next(value) { try { state.callback(value, state.index++); } catch (error) { if (!state.controller.signal.aborted) { state.controller.abort(error); }
state.subscription?.unsubscribe(); state.subscription = null; settleForEach(state, { ok: false, reason: error }); } }, error(error) { // The source has already finished with an error, so later teardown // paths should not try to unsubscribe a stale handle. state.subscription = null; settleForEach(state, { ok: false, reason: error }); }, complete() { // Completion uses the same one-shot settlement path as errors so the // Promise and abort listener cannot diverge. state.subscription = null; settleForEach(state, { ok: true }); }, }, { signal: state.controller.signal }); } catch (error) { state.subscription = null; settleForEach(state, { ok: false, reason: error }); return deferred.promise; }
// Some Observable-like sources synchronously call `next()` and only return // the subscription object afterward. If traversal already aborted during that // synchronous work, unsubscribe once here now that the handle exists. if (state.controller.signal.aborted) { state.subscription?.unsubscribe(); state.subscription = null;
if (!state.settled) { settleForEach(state, { ok: false, reason: state.controller.signal.reason, }); } }
return deferred.promise;}
/** * Returns `true` when a value is an instance of this library's `Observable` * class. * * Use it when you need methods that belong to this implementation specifically, * such as `pull()`. * * @example Narrowing to this implementation * ```ts * if (isObservable(source)) { * return source.pull(); * } * ``` */export function isObservable<T = unknown>( value: unknown,): value is Observable<T> { // This is a straightforward instanceof check // Works reliably across module boundaries and handles inheritance correctly return value instanceof Observable;}
/** * Returns `true` when a value implements the Observable protocol via * `[Symbol.observable]()`. * * Use it when compatibility matters more than the concrete class. This is the * right check for adapters and utilities that should work with RxJS, this * package, or any other spec-shaped Observable. * * @example Accepting any Observable-like source * ```ts * if (isSpecObservable(source)) { * return Observable.from(source); * } * ``` */export function isSpecObservable<T = unknown>( value: unknown,): value is SpecObservable<T> { // Early return for non-objects if (value === null || value === undefined || typeof value !== "object") { return false; }
try { // Check if the object has the Symbol.observable method const observableMethod = (value as Record<symbol, unknown>)[Symbol.observable]; if (typeof observableMethod !== "function") { return false; }
// Call the method to get the subscribable object const subscribable = (observableMethod as () => unknown).call(value);
// Verify the result has a subscribe method return ( subscribable !== null && subscribable !== undefined && typeof subscribable === "object" && typeof (subscribable as Record<string, unknown>).subscribe === "function" ); } catch { // If any step throws, it's not a valid Observable return false; }}