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

rxjs-overlap-map

v0.0.2

Published

Alternative operator to RxJS switchMap

Downloads

2

Readme

RxJS overlapMap

Install

You can install rxjs-overlap-map by either using PNPM, NPM or Yarn:

# PNPM
$ pnpm install --save rxjs-overlap-map

# NPM
$ npm install --save rxjs-overlap-map

# Yarn
$ yarn add rxjs-overlap-map

Motivation

RxJS switchMap operator switch to new inner Observable everytime it receives new value from source Observable. Meanwhile, when making multiple HTTP requests for searching or fetching data, we usually want to switch to the latest emitted inner Observable than waiting for the latest inner Observable to emit. overlapMap is the same as exhaustMap with the reversed effects:

clicks.pipe(overlapMap(() => ajax.get('/api')))

Example

Given the following input Observable:

const input$ = of(3, 1, 5).pipe(
  map(val => of(val).pipe(delay(val * 1000)))
)
// Expected output: 1, 5

The given Observable contains three inner Observables. The first emits 3 after 3ms, the second emits 1 after 1ms and the third emits 5 after 5ms. Our expected behavior is to make the application output 1 and 5.

Using switchMap the output will be 5 only:

input$
  .pipe(switchMap(stream => stream))
  .subscribe(val => console.log(val))
// Output:
//  5s -> 5

It means that even 1 and 3 has been emitted, we still have to wait until the last stream emits 5 to receive the output.

If we use mergeMap, the output is mandatory:

input$
  .pipe(mergeMap(stream => stream))
  .subscribe(val => console.log(val))
// Output: 1, 3, 5
//  1s -> 1
//  3s -> 3 <- Late response is not omitted
//  5s -> 5

But late response is not omitted so that the order is not preserved, the data may become stale.

Using concatMap we can preserve the stream order:

input$
  .pipe(concatMap(stream => stream))
  .subscribe(val => console.log(val))
// Output: 3, 1, 5
//  3s -> 3
//  4s -> 1
//  9s -> 5

But a new stream always have to wait for the previous stream to finish to move to the next stream.

Using overlapMap, we will get expected output:

input$
  .pipe(overlapMap(stream => stream))
  .subscribe(val => console.log(val))
// Output:
//  1s -> 1
//  5s -> 5

API Reference

overlap

Flattens multiple Observables, previous values that being overlapped by the the next Observable will be ignored.

declare function overlap<R>(
  ...observables: Array<Observable<any>>
): Observable<R>

overlapMap

Overlap previous streams with new coming stream.

declare function overlap<T, R>(
  project: (value: T, index: number) => Observable<R>
): OperatorFunction<T, R>

clicks.pipe(overlapMap(() => interval(1000)))

sequentialMap

Give a full control of new coming stream over previous projected streams.

declare function sequentialMap<T extends Observable<any>, R>(
  project: (
    acc: Observable<R>,
    next: T,
    index: number
  ) => Observable<R>
): OperatorFunction<T, R>

const overlapMap = callback => source =>
  source.pipe(
    map(callback),
    sequentialMap((prev, next) => overlap(prev, next))
  )

const mergeMap = callback => source =>
  source.pipe(
    map(callback),
    sequentialMap((prev, next) => merge(prev, next))
  )

const concatMap = callback => source =>
  source.pipe(
    map(callback),
    sequentialMap((prev, next) => prev.pipe(concat(next)))
  )