AU2 - Why `ICollectionSubscribable.subscribe()` returns `void`?

Hi guys,

I have code like this in Aurelia 2:

    this._observerLocator.getArrayObserver(this.select1).subscribe({
      handleCollectionChange: () => {
        this._signaler.dispatchSignal("select1-changed");
      }
    });

If i want to temporarily unsubscribe and subscribe again (handling navigation back and forward in browser’s history, via "au:router:location-change"), I have to put my ICollectionSubscriber in a variable:

  private _subscribe1 = {
    handleCollectionChange: (...params) => {
      this._signaler.dispatchSignal("select1-changed");
    }
  };
// elsewhere...
this._observerLocator.getArrayObserver(this.select1).unsubscribe(this._subscribe1);

Is there any reason why something like ISubscription is not returned from .subscribe method?

@nenadvicentic for avoiding doing unnecessary work, the observer won’t create & return any callback or object to the caller. You’ll need to store the information of the observer and subscriber to cancel the subscription later:

const observer = ....getArrayObserve(...)
const handler = {
  handleCollectionChange: () => { ... }
}
observer.subscribe(handler)
observer.unsubscribe(handler)
1 Like