简体   繁体   中英

Observer subscribe pattern in blog

I've been reading this blog https://blog.lftechnology.com/implementing-the-observer-pattern-in-javascript-198ccb62124d and couldn't figure out the following piece of code: The return statement here:

return () =>
      (this.subscribers = this.subscribers.filter(
        subscriber => subscriber !== fn
      )); 

inside this piece of code:

subscribe(fn) {
    if (Array.isArray(fn)) {
      return this.subscribeMany(fn);
    }

    this.subscribers.push(fn);

    return () =>
      (this.subscribers = this.subscribers.filter(
        subscriber => subscriber !== fn
      ));
  }

Should this be removed altogether rather than subscriber => subscriber !== fn as indicated? They just pushed the value fn to this.subscribers in the previous line and then remove it immediately? it doesn't make sense.

订阅observable会返回一个Subscription对象,该对象具有不带参数的unsubscribe()方法,只是处理订阅所持有的资源。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM