What is pipe for in rxJS

前端 未结 4 827
青春惊慌失措
青春惊慌失措 2020-12-02 15:07

I think I have the base concept, but there are some obscurities

So in general this is how I use an observable:



        
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-02 15:47

    A good summary I've come up with is:

    It decouples the streaming operations (map, filter, reduce...) from the core functionality(subscribing, piping). By piping operations instead of chaining, it doesn't pollute the prototype of Observable making it easier to do tree shaking.

    See https://github.com/ReactiveX/rxjs/blob/master/doc/pipeable-operators.md#why

    Problems with the patched operators for dot-chaining are:

    Any library that imports a patch operator will augment the Observable.prototype for all consumers of that library, creating blind dependencies. If the library removes their usage, they unknowingly break everyone else. With pipeables, you have to import the operators you need into each file you use them in.

    Operators patched directly onto the prototype are not "tree-shakeable" by tools like rollup or webpack. Pipeable operators will be as they are just functions pulled in from modules directly.

    Unused operators that are being imported in apps cannot be detected reliably by any sort of build tooling or lint rule. That means that you might import scan, but stop using it, and it's still being added to your output bundle. With pipeable operators, if you're not using it, a lint rule can pick it up for you.

    Functional composition is awesome. Building your own custom operators becomes much, much easier, and now they work and look just like all other operators from rxjs. You don't need to extend Observable or override lift anymore.

提交回复
热议问题