Anders
Anders

Reputation: 423

Rxjs, Pipe with one argument

Are there any differences when using the Pipe function with one argument, versus not using Pipe at all?

I am currently implementing the takeUntil unsubscribe strategy from this article. In the "official solution" from this SO question the takeUntil operator is sent through a pipe. However, on this page takeUntil is used with no pipe.

I am therefore wondering if there is any difference (memory leakage/performance, etc) in using a Pipe with a single Rx operator, versus no Pipe at all.

private destroy$ = new Subject();
...
this.potatoService.getPotato()
   .pipe(
    takeUntil(this.destroy$)
   ).subscribe(...

as opposed to

this.potatoService.getPotato()
    .takeUntil(this.destroy$)
    .subscribe(...

Upvotes: 6

Views: 1660

Answers (3)

DongBin Kim
DongBin Kim

Reputation: 1879

The point is that the old way adds the operator to the prototype so that every observable instance can use it. That’s why it makes the operator untreeshakable and it’s discouraged to approach that way.

Upvotes: 1

Harun Yilmaz
Harun Yilmaz

Reputation: 8558

Since RxJS v6, takeUntil (and the others) have become pipeable operators rather than a standalone function.

In the link you shared, please have a look at the imports section which means this example uses a former version of RxJS:

import 'rxjs/add/operator/takeUntil';

From the official docs of RxJS v6, the import path of takeUntil becomes:

import { takeUntil } from 'rxjs/operators';

For further reading: https://rxjs-dev.firebaseapp.com/api/operators/takeUntil

Upvotes: 1

Bunyamin Coskuner
Bunyamin Coskuner

Reputation: 8859

There is no difference. The latter is the old way of using operators in RxJS. However, as far as I know, it is deprecated and you should not use it.

We used to prototype operators into Observables with static imports like this

import 'rxjs/add/operator/takeUntil';

However, this makes it impossible to tree shake RxJS. So that, RxJS announced pipeable operators starting from RxJS v5.5. Always use your operators within pipe

Upvotes: 2

Related Questions