Combine RxJS operators into new operator using TypeScript

Peter Albert

I frequently find my self adding the same sequence of operators to observables, e.g.

observable$
  .do(x => console.log('some text', x))
  .publishReplay()
  .refCount();

I'm looking for a way to combine these 3 operators in a small reusable operator (e.g. .cache('some text')) that I can chain to any observable. How can I define this in Typescript, so that I could import rxjs/Observable and this operator, like I do with rxjs operators?

cartant

To implement the operator you have described, create a cache.ts file with the following content:

import { Observable } from "rxjs/Observable";
import "rxjs/add/operator/do";
import "rxjs/add/operator/publishReplay";

// Compose the operator:

function cache<T>(this: Observable<T>, text: string): Observable<T> {
  return this
    .do(x => console.log(text, x))
    .publishReplay()
    .refCount();
}

// Add the operator to the Observable prototype:

Observable.prototype.cache = cache;

// Extend the TypeScript interface for Observable to include the operator:

declare module "rxjs/Observable" {
  interface Observable<T> {
    cache: typeof cache;
  }
}

And consume it like this:

import { Observable } from "rxjs/Observable";
import "rxjs/add/observable/of";
import "./cache";

let cached = Observable.of(1).cache("some text");
cached.subscribe(x => console.log(x));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Specify signature of a custom RxJS operator in Typescript

RxJs: How to combine two observable into a new observable with different type

using spread operator in typescript

What is the difference between 'rxjs/operators' and 'rxjs/add/operator/'?

RxJS - which operator can combine history of last N events?

Typescript Type guards: Using the in operator

Using React useEffect hook with rxjs mergeMap operator

Looking for RxJs Operator that returns early, skipping operators below, not filter() or skip()

RxJs: Can you spread operators as arguments into pipe operator

Cache Http requests using only RxJS operators

Using Combine operators to transform Future into Publisher

Custom change operator using Combine

How to make recursive HTTP calls using RxJs operators?

rxjs conditional startWith , endWith using iif operator

Combine Lambda Expressions using custom operator

How to use rxjs operators inside a TypeScript app

Investigate activatedRoute observables using rxjs operators

Angular Rxjs: syntax error using pipeable operators

Using forkJoin as lettable operator in RxJS 6 pipe

How can I Combine using Spread Operator in TypeScript for an Object

Using RxJS partition operator with angular cause an error

Using the >>= and =<< operators to combine IO in Haskell

Angular using rxjs operator to change boolean flag

Angular combine and transform multiple http requests with forJoin Rxjs operator

combine the array inside object using the spread operator

Using the + operator to combine arrays in PHP

RxJS- Conditionally using RetryWhen operator

RxJS Custom operator with typescript Union type

Typescript using & operator on string type