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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@pastabolo/rxjs-utils

v0.1.1

Published

Utilities that enhance the power of RxJS 🚀

Downloads

5

Readme

RxJS utils

This library provides some operators to enhance the power of RxJS 🚀

Table of contents

Installation

npm install @pastabolo/rxjs-utils

or

yarn add @pastabolo/rxjs-utils

Operators

▶️ repeatSwitchMap

⚠️ The problem

Sometimes, you want to reset the subscription to an observable by reacting to a list of triggers and then switchMap to the source.

  • It might be difficult to identify what is the source at first glance
  • You waste time and effort to understand what is going on (it may not be clear that the subscription to the source has to be reset each time a trigger emits)
  • You have to initiate the subscription manually (by using the startWith operator or by having a resetter which is a BehaviorSubject instead of a Subject)
merge(
  interval(5000),
  resetter1$,
  resetter2$
).pipe(
  startWith(''),
  switchMap(() => source$) // source$ is declared here
).subscribe();

✔️ Solution

By using the repeatSwitchMap operator, you can do this in a cleaner way :

  • Declare the source at the first line so you can identify it easily
  • The name of this operator is meaningful so you can easily understand what is intended to be done
  • You don't have to initiate the subscription manually with a startWith and keep your resetters as Subject which could be a more appropriate object than a BehaviorSubject because they might not be supposed to emit at initialization
source$.pipe( // source is declared here
  repeatSwitchMap(
    merge(
      interval(5000),
      resetter1$,
      resetter2$
    )
  )
).subscribe();

📝 This operator and the existing repeatWhen operator are different because repeatWhen waits for the source to complete before subscribing to the inner observable that triggers new subscription to this source.

Description

Returns an Observable that mirrors the source Observable. This method will resubscribe to the source Observable each time the notifier emits. For each emission, the previous subscription is completed. It completes if the source and the repeat observable complete. It emits error if the source or the repeat observable emits error.

Examples

Repeat a message stream on click

const source$ = of('Repeat message');
const documentClick$ = fromEvent(document, 'click');

source$.pipe(repeatSwitchMap(documentClick$)).subscribe(console.log);

Parameters

  • notifier: Observable Observable that repeats the subscription to the source each time it emits.

▶️ cache

⚠️ The problem

When you want to cache values, you can store them in variables or specific RxJS objects like ReplaySubject, BehaviorSubject or even use the shareReplay operator. But, with these methods, you don't have the notions of expiration, refreshment and automatic refresh.

✔️ Solution

You can extend your observable with the cache operator and specify :

  • an exipration time
  • a refresher which is an observable that refresh the cached source when it emits
  • a flag to automatically refresh the cache when it expires

Description

Keep the last emitted values in cache for the expirationTime provided and share them with all its subscribers. If an observer subscribes when values are still in cache, it will get them directly. Otherwise, if automaticRefresh is set to false, a new subscription is made to the source and the new values are stored in cache and emitted to all the subscribers.

Examples

Keep a value in cache for 10s

const source$ = of('value');

source$.pipe(cache({ expirationTime: 10000 })).subscribe(console.log);

Keep a value in cache for 10s and refresh it automatically by resubscribing to the source when the cache expires

const source$ = of('value');

source$.pipe(cache({ expirationTime: 10000, automaticRefresh: true })).subscribe(console.log);

Keep a value in cache for 10s and refresh on click

const source$ = of('value');
const documentClick$ = fromEvent(document, 'click');

source$.pipe(cache({ expirationTime: 10000, refresher: documentClick$  })).subscribe(console.log);

Keep 3 values in cache (10s for each value)

const source$ = of(1, 2, 3);

source$.pipe(cache({ expirationTime: 10000, bufferSize: 3 })).subscribe(console.log);

Parameters

  • expirationTime: number Maximum time length to keep the value in cache in milliseconds.
  • refresher: Observable Notifier that resubscribes to the source each time it emits to refresh the cache.
  • automaticRefresh: boolean Refresh the value in cache when it expires.
  • bufferSize: number Maximum element count of the cache buffer (default 1).

Updates

v0.1.0

  • repeatSwitchMap operator refactoring : does not accept a function anymore but an observable. The repeat observable does not have to react to source completion to not encroach upon existing repeatWhen operator

v0.0.1

  • add repeatSwitchMap and cache operators

This library was generated with Angular CLI version 11.0.5.