npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

rxjs-worker-subject

v2.0.1

Published

Reactive Web Worker communication using RxJS — subscribe to worker output as an Observable, send input as an Observer.

Downloads

314

Readme

rxjs-worker-subject

npm CI license

Reactive Web Worker communication using RxJS. Subscribe to worker output as an Observable, send input as an Observer — or do both at once with WorkerSubject.

Three classes cover the full range of use cases — from read-only observation to full-duplex messaging.

Installation

npm install rxjs-worker-subject

Example

worker.ts

self.onmessage = (event: MessageEvent<number>) => {
  self.postMessage(event.data * 2);
};

main.ts

import { filter, map } from 'rxjs';
import { WorkerSubject } from 'rxjs-worker-subject';

const worker = new Worker(new URL('./worker.ts', import.meta.url), { type: 'module' });
const subject = new WorkerSubject<number, number>(worker);

subject
  .pipe(
    filter((n) => n > 10),
    map((n) => `Result: ${n}`)
  )
  .subscribe((result) => console.log(result)); // "Result: 84"

subject.next(42);
// call complete(true) after receiving the final response, or in a teardown hook
subject.complete(true); // clears handlers and terminates the worker

Classes

WorkerSubject<Input, Output> — full duplex

Send messages to the worker and subscribe to its responses through a single handle.

import { WorkerSubject } from 'rxjs-worker-subject';

const worker = new Worker(new URL('./worker.ts', import.meta.url), { type: 'module' });
const subject = new WorkerSubject<Command, Result>(worker);

subject.subscribe((result) => console.log(result));
subject.next({ type: 'ping' });

subject.complete(true); // true = terminate the worker

WorkerObservable<T> — read only

Subscribe to worker output without caring about the input side.

import { WorkerObservable } from 'rxjs-worker-subject';

const obs = new WorkerObservable<Result>(worker);
obs.pipe(filter((r) => r.type === 'pong')).subscribe(console.log);

WorkerObserver<T> — write only

Send messages to a worker. Implements the RxJS Observer interface, so it can be passed directly to observable.subscribe().

import { WorkerObserver } from 'rxjs-worker-subject';

const observer = new WorkerObserver<Command>(worker);
observer.next({ type: 'ping' });

// pipe an observable into a worker
commands$.subscribe(observer);

API

WorkerSubject<Input, Output>

Extends WorkerObservable<Output> and implements Observer<Input>.

| Member | Description | |--------|-------------| | constructor(worker, options?) | Wraps the given Worker. | | next(input: Input) | Sends a message to the worker via postMessage. | | complete(terminate?: boolean) | Completes the observable and clears event handlers. Terminates the worker if terminate is true. | | subscribe(...) | Standard RxJS Observable subscription. |

WorkerObservable<T>

Extends Observable<T>.

| Member | Description | |--------|-------------| | constructor(worker, options?) | Wraps worker output as a multicasted observable. | | complete(terminate?: boolean) | Completes all subscriptions and clears event handlers. | | options.rawResponse | When true, emits the raw MessageEvent instead of event.data. Defaults to false. |

On worker error, the observable propagates the error to subscribers and terminates the worker automatically.

WorkerObserver<T>

Implements Observer<T>.

| Member | Description | |--------|-------------| | constructor(worker) | Wraps worker input. | | next(input: T) | Sends a message to the worker via postMessage. No-op after complete() or error(). | | error(err) | Terminates the worker. Called automatically by RxJS when an upstream observable errors (e.g. source$.subscribe(observer)). | | complete(terminate?: boolean) | Stops accepting messages. Terminates the worker if terminate is true. Defaults to false — when used via source$.subscribe(observer), RxJS calls complete() with no arguments, leaving the worker running. Call complete(true) explicitly to terminate. |

Note: a Worker instance can only be wrapped by one WorkerObservable (or WorkerSubject) at a time. Wrapping the same worker in a second instance will silently replace the onmessage/onerror handlers and break the first.

Contributing

Pull requests are welcome. For major changes, please open an issue first.

License

MIT