【发布时间】:2018-01-14 15:29:00
【问题描述】:
如何检查 mobx observable 是否有订阅者? 或者观察者获取订阅者如何获取事件?
例如,我有一些方法:fetchData(),还有一些变量:@observable responseData,并且我只想在 responseData 有订阅者(@reaction 或 @autorun 等消费者)时运行 fetchData()。
如果有方法isObservableHasSubscribers(observableVar);就更好了
完整代码示例(堆栈:Angular 和 Mobx):
SomeStore {
@observable responseData;
fetchData(){
if(/* check? */) return; // <- here need some Check ????
fetchDataFromServer().then((d)=>{
this.responseData = d;
})
}
}
const store = new SomeStore();
let unimportant;
// this is optional consumer (subscriber)...
autorun(() => {
unimportant = store.responseData;
});
store.fetchData();
我想为此找到一种优雅的方式。
有什么想法吗?
【问题讨论】:
-
MobX uses
hasObserversunder the hood,因此如果您找不到自己跟踪的方法,可能值得打开一个问题并与创建者讨论。
标签: javascript angular observer-pattern mobx