Tips of using RxJS

Async EventEmitter

Oh EventEmitter is Angular extension, not RxJS, whatever!

asyncEvent = new EventEmitter(true);

EventEmitter accept boolean argument, subscription get called in next JavaScript cycle, if sync event causes dead-lock, performance issues, or other problems, async is a quick solution.

Debounced or Shuffle Event

class OurComponent {
  private changed = new Subject<string>();

  private _value: string;

  get value() { return this._value; }

  set value(v: string) {
    if (v != this._value) {
       this._value = v;
       this.changed.next(v);
    }
  }

  @Output
  get delayedChange(): Observable<string> {
      return this.changed.pipe(debounceTime(500));
  }
}

Change delayedChange be a property, return a new observable filtered with debounceTime RxJS operation.

Disable an Event on Dispose

Suppose an event got from Dom world or other external source, you want disable derived event on component dispose, see code of cdk CdkScrollable directive for a clue.

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容