RxJSdistinctUntilChanged-对象比较 [英] RxJS distinctUntilChanged - object comparsion
本文介绍了RxJSdistinctUntilChanged-对象比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有对象流,如果当前对象与先前的对象不同,则需要进行比较,在这种情况下,需要发出新的值.我发现distinctUntilChanged运算符应该完全执行我想要的操作,但是由于某种原因,除了第一个操作符外,它永远不会发出值.如果我删除了distinctUntilChanged值,则会正常发出.
I have stream of objects and I need to compare if current object is not same as previous and in this case emit new value. I found distinctUntilChanged operator should do exactly what I want, but for some reason, it never emit value except first one. If i remove distinctUntilChanged values are emited normally.
我的代码:
export class SettingsPage {
static get parameters() {
return [[NavController], [UserProvider]];
}
constructor(nav, user) {
this.nav = nav;
this._user = user;
this.typeChangeStream = new Subject();
this.notifications = {};
}
ngOnInit() {
this.typeChangeStream
.map(x => {console.log('value on way to distinct', x); return x;})
.distinctUntilChanged(x => JSON.stringify(x))
.subscribe(settings => {
console.log('typeChangeStream', settings);
this._user.setNotificationSettings(settings);
});
}
toggleType() {
this.typeChangeStream.next({
"sound": true,
"vibrate": false,
"badge": false,
"types": {
"newDeals": true,
"nearDeals": true,
"tematicDeals": false,
"infoWarnings": false,
"expireDeals": true
}
});
}
emitDifferent() {
this.typeChangeStream.next({
"sound": false,
"vibrate": false,
"badge": false,
"types": {
"newDeals": false,
"nearDeals": false,
"tematicDeals": false,
"infoWarnings": false,
"expireDeals": false
}
});
}
}
推荐答案
我遇到了同样的问题,并使用JSON.stringify比较对象来解决此问题:
I had the same problem, and fixed it with using JSON.stringify to compare the objects:
.distinctUntilChanged((a, b) => JSON.stringify(a) === JSON.stringify(b))
肮脏但有效的代码.
这篇关于RxJSdistinctUntilChanged-对象比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文