Skip to main content
Home
This package has been archived, and as such it is read-only.

Built and signed on GitHub Actions

Works with
This package works with Node.js, BrowsersIt is unknown whether this package works with Cloudflare Workers, Bun
It is unknown whether this package works with Cloudflare Workers
This package works with Node.js
It is unknown whether this package works with Bun
This package works with Browsers
JSR Score100%
Publisheda month ago (0.1.0)

A lightweight, RxJS-inspired library implementing the Observer pattern in JavaScript. Features BroadcastSubjects with AbortController-based unsubscription, supporting both synchronous and asynchronous producers.

import type { BroadcastSubject } from "./broadcast-subject.ts"; /** * Object interface for an {@linkcode BroadcastSubject} factory. */ export interface BroadcastSubjectConstructor { /** * Creates and returns a variant of [`Subject`](https://jsr.io/@xan/subject/doc/~/Subject). When values * are [`nexted`]((https://jsr.io/@xan/observer/doc/~/Observer.next)), they are {@linkcode structuredClone|structured cloned} and sent * only to [`consumers`](https://jsr.io/@xan/observer#consumer) of _other_ {@linkcode BroadcastSubject} instances with the same {@linkcode name} * even if they are in different browsing contexts (e.g. browser tabs). Logically, [`consumers`](https://jsr.io/@xan/observer#consumer) * of the {@linkcode BroadcastSubject} do not receive it's _own_ [`nexted`](https://jsr.io/@xan/observer/doc/~/Observer.next) values. * @example * ```ts * import { BroadcastSubject } from "@xan/broadcast-subject"; * * // Setup subjects * const name = "test"; * const controller = new AbortController(); * const subject1 = new BroadcastSubject<number>(name); * const subject2 = new BroadcastSubject<number>(name); * * // Subscribe to subjects * subject1.subscribe({ * signal: controller.signal, * next: (value) => console.log("subject1 received", value, "from subject1"), * return: () => console.log("subject1 returned"), * throw: (value) => console.log("subject1 threw", value), * }); * subject2.subscribe({ * signal: controller.signal, * next: (value) => console.log("subject2 received", value, "from subject2"), * return: () => console.log("subject2 returned"), * throw: (value) => console.log("subject2 threw", value), * }); * * subject1.next(1); // subject2 received 1 from subject1 * subject2.next(2); // subject1 received 2 from subject2 * subject2.return(); // subject2 returned * subject1.next(3); // No console output since subject2 is already returned * ``` */ new (name: string): BroadcastSubject; new <Value>(name: string): BroadcastSubject<Value>; readonly prototype: BroadcastSubject; }