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

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

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

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

How to use rxjs operators inside a TypeScript app

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

RxJs: Can you spread operators as arguments into pipe operator

Angular Rxjs: syntax error using pipeable operators

Investigate activatedRoute observables using rxjs operators

Cache Http requests using only RxJS operators

Using the + operator to combine arrays in PHP

Custom change operator using Combine

RxJS Custom operator with typescript Union type

Specify signature of a custom RxJS operator in Typescript

Using Combine operators to transform Future into Publisher

using spread operator in typescript

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

Angular combine and transform multiple http requests with forJoin Rxjs operator

RxJS- Conditionally using RetryWhen operator

Angular using rxjs operator to change boolean flag

Using RxJS partition operator with angular cause an error

Using React useEffect hook with rxjs mergeMap operator

Using forkJoin as lettable operator in RxJS 6 pipe

rxjs conditional startWith , endWith using iif operator

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

Combine Lambda Expressions using custom operator

combine the array inside object using the spread operator

Typescript Type guards: Using the in operator

Typescript using & operator on string type

How to make recursive HTTP calls using RxJs operators?