Craig
Craig

Reputation: 1776

RxJs operators are available without patching?

I am running angular5 with RxJs 5.5 and just found that operators like filter are available without importing them. They seem to be known methods of the Observable type without patching. If that is true though then there would be no point in pipeable operators since they came in to address the issue of patching the global observable type by instead offering methods that dont patch. Does that sound correct?

If it is correct then how is this even working without importing filter?

.callFunctionThatReturnsObserable()
.filter(x => x == "astring")

Upvotes: 0

Views: 177

Answers (1)

Mark Hughes
Mark Hughes

Reputation: 7374

If you import Observable from 'rxjs' (or 'rxjs/Rx') anywhere in your application, that will patch Observable with most of the standard operators for your entire application.

This has the disadvantage of including all operators in your production built file, but has the advantage that you do not need to specifically import the needed operators anywhere.

If you change to importing from 'rxjs/Observable' everywhere, the operators will no longer be pulled in, so you will need to import those that you use - but again, note that you only need to import them once, anywhere within your application, and they will be globally available.

In RxJS 6 / Angular 6, this is changed and the operators are no longer patched onto the Observable type, but are standalone functions to be used with the Observable pipe method. With this, you import Observable from 'rxjs', and you then import the operators in each file you use them in. It's much cleaner, because you don't end up in the situation you describe where you find that, actually, somehow you've imported all of them globally whether or not you need them.

Upvotes: 1

Related Questions