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

rx-singleton-lock

v1.3.0

Published

A small util that provides singleton locks for rxjs streams.

Readme

rx-singleton-lock

Build Status

Install

npm install rx-singleton-lock --save
yarn add rx-singleton-lock

Note: requires rxjs@^6.0.0.

Usage

import { concat, of, pipe } from "rxjs";
import { delay, tap } from "rxjs/operators";
import RxSingletonLock from "rx-singleton-lock";

const lock = new RxSingletonLock({
  traceErr: console.error.bind(console),
  traceLog: console.log.bind(console),
});

const dummyRequest$ = (str) => of(str).pipe(tap((v) => console.log(v)));
const dummyLock$ = () => of("locked").pipe(delay(3000));

concat(
  lock.sync(dummyRequest$("request0")),
  lock.sync(dummyRequest$("request1")),
  lock.singleton(dummyLock$()),
  lock.sync(dummyRequest$("request2"))
).subscribe();

Notice in the console that request2 will be delayed until the lock has completed (after 3 seconds).

API

constructor(options)

new RxSingletonLock({
  scheduler?: SchedulerLike;
  traceLog?: (message: string) => any;
  traceErr?: (message: string, e: Error) => any;
})

scheduler: SchedulerLike (optional) Specifies the Rx scheduler to use (mostly for unit testing purposes).

traceLog: (message: string) => any (optional) If specified, it will be used in a tap that is attached inside sync and singleton to provide debug information.

traceErr: (message: string, e: Error) => any (optional) If specified, it will be used in a tap that is attached inside sync and singleton to provide debug information.

sync(stream$)

sync(stream$: Observable<T>): Observable<T>

If the lock is unlocked: returns the stream$ with a trace-tap.

If the lock is locked: returns a new stream that will wait for the lock to unlock. Once unlocked it will switchMap to the original stream$ with a trace-tap.

singleton(lock$)

singleton(lock$: Observable<T>): Observable<T>

If the lock is unlocked: returns a shared lock$ with an attached tap for logging (see constructor arguments). The lock is now considered locked until lock$ is completed.

If the lock is locked: returns the shared lock$ that is currently holding the lock, with an attached tap for logging (see constructor arguments). The given lock$ argument will be discarded and never subscribed to.

Description

There is an example React app in ./example that demonstrates the usage more in practice.

This library was created to solve a specific issue. There was a project where all API requests were AWS lambda invokes, where the Cognito credentials could expire at any time. Every single API request went through a central invoke function, which used rxjs/ajax internally. If an API call failed due to an expired token, it should start the refresh-token flow. To avoid starting multiple refresh-flows due to multiple API calls failing simultaneously, the need for a singleton-lock emerged.

The naming sync and singleton comes from the idea of making calls synchronized according to the locking mechanism, and the definition of singleton as "a single person or thing of the kind under consideration.", since only the first lock$ stream is considered in the singleton() function while all others are discarded.

License

MIT