Skip to content

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

License

Notifications You must be signed in to change notification settings

alexanderharding/subject

Repository files navigation

@xan/subject

A set of tooling that encapsulates an object that acts as both an Observer and Observable.

Build

Automated by JSR.

Publishing

Automated by .github\workflows\publish.yml.

Running unit tests

Run deno task test or deno task test:ci to execute the unit tests via Deno.

Example

import { Subject } from "@xan/subject";
import { from } from "@xan/observable";

class Authenticator {
  readonly #events = new Subject<Event>();
  // Hide the Observer from the public API by exposing the Subject as an Observable.
  readonly events = from(this.#events);

  [Symbol.dispose]() {
    this.#events.return(); // Cleanup resources.
  }

  login() {
    // Execute some login logic...
    this.#events.next(new Event("login"));
  }

  logout() {
    // Execute some logout logic...
    this.#events.next(new Event("logout"));
  }
}

Glossary And Semantics

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published