RXJS - Custom Pipe

Refer:
custom-rxjs-operators-by-example
rxjs-custom-operator-internal-variables
operators-defer
pipeable-operators
operator-creation

Two ways to create Custom Pipe:
一是从0开始自定义Operator;二是利用现有的Operator

image.png

GuideLine:

In the most common case, users might like to create an operator to be used only by their app. These can be developed in any way the developer sees fit, but here are some guidelines:

  1. Operators should always return an Observable. You're performing operations on unknown sets of things to create new sets. It only makes sense to return a new set. If you create a method that returns something other than an Observable, it's not an operator, and that's fine.
    Operators返回类型是Observable

  2. Be sure to manage subscriptions created inside of the Observable your operator returns. Your operator is going to have to subscribe to the source (or this) inside of the returned Observable, be sure that it's returned as part of unsubscribe handler or subscription.

  3. Be sure to handle exceptions from passed functions. If you're implementing an Operator that takes a function as an argument, when you call it, you'll want to wrap it in a try/catch and send the error down the error() path on the observable.
    如果自定义的Operator需要take一个函数作为参数,需要注意函数的异常处理。需要将函数的调用用try/catch包裹,并且将error推送到observableerror()

  4. Be sure to teardown scarce resources in your unsubscribe handler of your returned Observable. If you're setting up event handlers or a web socket, or something like that, the unsubscribe handler is a great place to remove that event handler or close that socket.
    确保在unsubscribe种进行资源释放。如果是事件处理程序或web套接字,unsubscribe时需要删除该事件处理程序或关闭该套接字。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容