如何在订阅 observable 之前使用 distinctUntilChanged 检查 json 对象
How to check json object with distinctUntilChanged before subscribing to observable
只有当我从观察者主题收到的 JSON 对象的值发生变化时,我才想订阅 observable:
searchedName = '';
filters = {
"page": 1,
"perPage": 10,
"sortOrder": "asc",
"tag": "allUsers",
"sortBy": "firstname"
}
getUsers(e)
{
console.log("searched")
const searchedKeyword = this.searchedName.trim();
if(searchedKeyword !== '')
this.filters['name'] = searchedKeyword
this._service.triggerCallForUsers(this.filters)
}
ngOnInit()
{
//Initially called to load users..
this._service.triggerCallForUsers(this.filters)
//Subscribe to fetch users
this._service.startFetchingUsers$
.distinctUntilChanged()
.subscribe((filters) => {
console.log("filters", filters)
if (filters) {
this._service.getUsersByFilter(filters)
.subscribe(
users => {
console.log('users', users);
},
error => {}
);
}
})
}
过滤器在哪里:
这可能吗?
尝试解决如下:
.distinctUntilChanged((a, b) => {
console.log('compare', b['name'], a['name'], b['name'] === a['name']);
return a === b
})
//第一次订阅时
输出:- 比较无输出
//When I add 's' searchedName field i.e filters which is sent to
next() will contain
filters['name']
output: - compare s undefined false
// If I change searchedName to 'sd':
output:- compare sd sd true.
更新:
<input type="text" (keyUp.enter)="getUsers($event)" [(ngModel)]="searchedName">
服务:
在服务中,我在 observable 上调用 next():
private startFetchingUsers = new BehaviorSubject < any > (null);
startFetchingUsers$ = this.startFetchingUsers.asObservable();
triggerCallForCareGroup(filter) {
this.startFetchingUsers .next(filter);
}
如果您需要深度相等,请尝试使用例如来自 lodash 的 isEqual
。
此外,您在 subscribe
中的逻辑过多 - 考虑使用 do
/tap
for side-effects and mergeMap
合并其他 Observables。
import { isEqual } from 'lodash';
this._service.startFetchingUsers$
.distinctUntilChanged(isEqual)
.do(filters => console.log("filters", filters))
.mergeMap(filters => this._service.getUsersByFilter(filters))
.do(users => console.log('users', users))
.subscribe({
error(error) {
console.error(error);
},
});
when I change name parameter in filter json I get updated value a['name'] and b['name'] as same.
这表明您的代码中的其他地方也可能存在问题。你能分享你调用 startFetchingUsers.next(newFilters)
的代码吗?
更新
您正在改变 this.filters
对象,因此您没有看到更改,因为 a === b
始终成立。对象本身是一样的!像这样使用浅拷贝:this._service.triggerCallForUsers({ ...this.filters })
只有当我从观察者主题收到的 JSON 对象的值发生变化时,我才想订阅 observable:
searchedName = '';
filters = {
"page": 1,
"perPage": 10,
"sortOrder": "asc",
"tag": "allUsers",
"sortBy": "firstname"
}
getUsers(e)
{
console.log("searched")
const searchedKeyword = this.searchedName.trim();
if(searchedKeyword !== '')
this.filters['name'] = searchedKeyword
this._service.triggerCallForUsers(this.filters)
}
ngOnInit()
{
//Initially called to load users..
this._service.triggerCallForUsers(this.filters)
//Subscribe to fetch users
this._service.startFetchingUsers$
.distinctUntilChanged()
.subscribe((filters) => {
console.log("filters", filters)
if (filters) {
this._service.getUsersByFilter(filters)
.subscribe(
users => {
console.log('users', users);
},
error => {}
);
}
})
}
过滤器在哪里:
这可能吗?
尝试解决如下:
.distinctUntilChanged((a, b) => {
console.log('compare', b['name'], a['name'], b['name'] === a['name']);
return a === b
})
//第一次订阅时 输出:- 比较无输出
//When I add 's' searchedName field i.e filters which is sent to next() will contain
filters['name']
output: - compare s undefined false
// If I change searchedName to 'sd':
output:- compare sd sd true.
更新:
<input type="text" (keyUp.enter)="getUsers($event)" [(ngModel)]="searchedName">
服务:
在服务中,我在 observable 上调用 next():
private startFetchingUsers = new BehaviorSubject < any > (null);
startFetchingUsers$ = this.startFetchingUsers.asObservable();
triggerCallForCareGroup(filter) {
this.startFetchingUsers .next(filter);
}
如果您需要深度相等,请尝试使用例如来自 lodash 的 isEqual
。
此外,您在 subscribe
中的逻辑过多 - 考虑使用 do
/tap
for side-effects and mergeMap
合并其他 Observables。
import { isEqual } from 'lodash';
this._service.startFetchingUsers$
.distinctUntilChanged(isEqual)
.do(filters => console.log("filters", filters))
.mergeMap(filters => this._service.getUsersByFilter(filters))
.do(users => console.log('users', users))
.subscribe({
error(error) {
console.error(error);
},
});
when I change name parameter in filter json I get updated value a['name'] and b['name'] as same.
这表明您的代码中的其他地方也可能存在问题。你能分享你调用 startFetchingUsers.next(newFilters)
的代码吗?
更新
您正在改变 this.filters
对象,因此您没有看到更改,因为 a === b
始终成立。对象本身是一样的!像这样使用浅拷贝:this._service.triggerCallForUsers({ ...this.filters })