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-extension

v0.3.2

Published

rxjs extensions

Downloads

78

Readme

rxjs-extension

Usage

TimeSliceSubject<T>

In each time slice, only the latest value will be published

import { interval } from 'rxjs';
import { TimeSliceSubject } from 'rxjs-extension';

const subject = new TimeSliceSubject<any>(5000);

subject.subscribe(console.log);

interval(1000).subscribe(val => subject.next(val + 1));

/** log:
 *  5
 *  10
 *  15
 *  20
 *  ...
 */

timeSlice | @timeSlice

Wrap a function T

T extends (...args: any[]) => Observable<any> | Promise<any>

Then return a time sliced function.

Example

import { interval } from 'rxjs';
import { timeSlice } from 'rxjs-extension';

const save = (n: number) => {
  console.log('save called');
  of(`${n} saved!`);
};

const timeSlicedSave = timeSlice(
  save,
  false /*if this function returns a PromiseLike result*/,
  5000
);

interval(1000)
  .pipe(take(5))
  .subscribe(val => timeSlicedSave(val + 1).subscribe(console.log));

/* output:

save called

5 saved! ​​​​​

5 saved! ​​​​​

5 saved! ​​​​​

5 saved! ​​​​​

5 saved! ​​​

*/

Example for decorator

import { interval } from 'rxjs';
import { timeSlice } from 'rxjs-extension/decorators';

class Test {
  @timeSlice(500)
  save(n: number) {
    return of(`${n} saved!`);
  }
}

const test = new Test();

test.save(1).then(console.log);
test.save(2).then(console.log);
test.save(3).then(console.log);

/* output:
 *
 * 3 saved! ​​​​​
 *
 * 3 saved!
 *  ​​​​​
 * 3 saved! ​​​​​
 *
 */

If you use decorators without typescript or the decorator cannot determin the return type, you need to specify the return type manually.

import { ResultType } from 'rxjs-extension/decorators';

class Test {
  @timeSlice(
    500,
    ResultType.Promise | ResultType.Observable | ResultType.Detect
  ) // default is detect
  save(n: number) {
    return of(`${n} saved!`);
  }
}

cacheable | @cacheable

This function will Wrap a function T

T extends (...args: any[]) => Observable<any> | Promise<any>

Then return a function can cache result

For example: we send a request and wait for respone, before request finished, we have a same request again, now we want to just use the last request's result

Example

const get = () => {
  return from(
    new Promise(resolve => {
      setTimeout(() => resolve(new Date()), 2000);
    })
  );
};

const cachedGet = cacheable(
  get,
  false /*if this function returns a PromiseLike result*/
);

all the request will be reused unless the first request finished

you call also pass a timeout:

const cachedGet = cacheable(
  get,
  false /*if this function returns a PromiseLike result*/,
  2000
);

the result will be cached for 2s

Example for decorator

import { interval } from 'rxjs';
import { cacheable } from 'rxjs-extension/decorators';

class Test {
  @cacheable()
  async save(n: number) {
    // await something...
    console.log(`${n} saved`);
    return `${n} succeed`;
  }
}

const test = new Test();

test.save(1).then(console.log);
test.save(1).then(console.log);
test.save(3).then(console.log);

/** logs:
 *  1 saved         <---- save only called once, the second call resued before if last call is pending
 *  1 succeed
 *  1 succeed
 *  3 saved
 *  3 succeed
 */