0.3.2 • Published 4 years ago

rxjs-extension v0.3.2

Weekly downloads
4
License
MIT
Repository
github
Last release
4 years ago

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
 */
0.3.2

4 years ago

0.3.1

4 years ago

0.3.0

4 years ago

0.2.18

4 years ago

0.2.17

4 years ago

0.2.16

4 years ago

0.2.15

4 years ago

0.2.14

4 years ago

0.2.13

4 years ago

0.2.12

4 years ago

0.2.11

4 years ago

0.2.10

4 years ago

0.2.9

4 years ago

0.2.8

4 years ago

0.2.7

4 years ago

0.2.6

4 years ago

0.2.5

4 years ago

0.2.4

4 years ago

0.2.1

4 years ago

0.2.3

4 years ago

0.2.2

4 years ago

0.2.0

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago