microsoft/ReSub

Create subscribeToKey method

catalinmiron opened this issue · 2 comments

As a developer
I want to listen only to a particular key 
So that I will not get notified when `trigger()` was called.

Solution

Create subscribeWithKey() method that will be called only when someone triggers with the specified key.

Example

// a.js
this.store.subscribeWithKey(myCallbackFunc, 'myKey');

// store.js

startup() {
  this.trigger('myKey'); // this will announce a change on that key and myCallbackFunc will be called
  this.trigger() // this will announce a change but myCallbackFunc will not be called.
}

Isn't that same as using subscribe() with second parameter?

https://github.com/Microsoft/ReSub/blob/master/src/StoreBase.ts#L239

If a store supports keys, it should generally not call "trigger()" without specifying a key. The only time this is appropriate is in the (rare) case where a store update affects all keys.