项目作者: willin

项目描述 :
Useful Rxjs Operators
高级语言: TypeScript
项目地址: git://github.com/willin/v0.git
创建时间: 2020-07-24T01:29:22Z
项目社区:https://github.com/willin/v0

开源协议:Apache License 2.0

下载


v0

Useful Rxjs Operators & Utils.

github npm npm npm Maintainability Test Coverage Build Status

你的关注是我最大的动力。 Your Star is the best gift.

Install

  1. npm i --save rxjs v0
  2. # or
  3. yarn add rxjs v0

Usage

中文文档参考: rx.js.cool 中的【进阶(Advanced)】章节系列文章

Operators

delayRetry

  1. {
  2. maxAttempts?: number;
  3. duration?: number;
  4. }

Defaults:

  1. {
  2. maxAttempts = 3,
  3. duration = 1000
  4. }

Usage:

  1. import { delayRetry } from 'v0';
  2. // import { delayRetry } from 'v0/operators';
  3. source$.pipe(
  4. // ...
  5. // Retry all options in current pipe
  6. delayRetry({
  7. maxAttempts: 2,
  8. duration: 200
  9. }),
  10. // catchError is needed
  11. catchError((error) => of(error))
  12. );

tapAsync

Just like tap, support async/await (promise) function.

Usage:

  1. import { tapAsync } from 'v0';
  2. // import { tapAsync } from 'v0/operators';
  3. source$.pipe(
  4. tapAsync(async (val) => {
  5. await SomeFn(val);
  6. })
  7. );

Decorators

@Cacheable(timeout = 0, mode = ReturnType.Detect)

Return Type:

  1. enum ResultType {
  2. Promise = 'Promise',
  3. Observable = 'Observable',
  4. Detect = 'Detect'
  5. }

Usage:

  1. import { interval } from 'rxjs';
  2. import { Cacheable, ReturnType } from 'v0';
  3. // import { Cacheable } from 'v0/decorators';
  4. class Demo {
  5. @Cacheable(300, ReturnType.Promise)
  6. async save(n: number) {
  7. // await something...
  8. console.log(`${n} saved`);
  9. return `${n} succeed`;
  10. }
  11. }
  12. const demo = new Demo();
  13. demo.save(1).then(console.log);
  14. demo.save(1).then(console.log);
  15. demo.save(3).then(console.log);
  16. /** logs:
  17. * 1 saved <---- save only called once, the second call resued before if last call is pending
  18. * 1 succeed
  19. * 1 succeed
  20. * 3 saved
  21. * 3 succeed
  22. */

Utils

cacheable(FN, timeout = 0, isPromise = false)

  1. import { cacheable } from 'v0';
  2. // import { cacheable } from 'v0/utils';
  3. const get = () => {
  4. return from(
  5. new Promise((resolve) => {
  6. setTimeout(() => resolve(new Date()), 2000);
  7. })
  8. );
  9. };
  10. const cachedGet = cacheable(get, false /*if this function returns a PromiseLike result*/);

RxPromise

  1. import { RxPromise } from 'v0';
  2. // import { RxPromise } from 'v0/utils';
  3. // RxPromise<T, R = Error> extends Observable<T>
  4. const mockedPromise = new RxPromise(resolver);

Type of resolver:

  1. (resolve: (r: T) => void, reject: (r: R) => void) => void

Example,transform mongoose exec to observable:

  1. import mongoose from 'mongoose';
  2. import { RxPromise } from 'v0';
  3. mongoose.Promise = RxPromise;
  4. mongoose.connect('mongodb://localhost/test', { useNewUrlParser: true, useUnifiedTopology: true });
  5. const kittySchema = new mongoose.Schema({
  6. name: String
  7. });
  8. const Kitten = mongoose.model('Kitten', kittySchema);
  9. const s$ = <Observable<Record<string, unknown>[]>>(<any>Kitten.find().exec());
  10. // or
  11. // const s$ = (Kitten.find().exec()) as any) as Observable<Record<string, unknown>[]>;
  12. s$.subscribe({
  13. next(v) {
  14. console.log(v);
  15. },
  16. complete() {
  17. console.log('ended');
  18. }
  19. });

Contribute

  1. Add your own operator
  2. Update Readme Doc (List / Usage)
  3. Make a PR

License

Apache 2.0

通过支付宝捐赠:

qr