我有一个简单的服务方法:
notify(userID: string) {
let _URL = this.baseUrl + '/notification/GetUnseen?userId=' + userID;
this.datatService.set(_URL);
return this.datatService.get()
.flatMap((response) =>
response.json().slice())
.distinct();
}它返回一个对象流,其中包含有关用户通知的信息。我想在不使用setTimeout的情况下每5秒用interval操作符执行这个调用?
当我尝试这个:
notify(userID: string) {
let _URL = this.baseUrl + '/notification/GetUnseen?userId=' + userID;
this.datatService.set(_URL);
return this.datatService.get()
**.interval(5000)**
.flatMap((response) =>
response.json().slice())
.distinct();
}我有个错误。有什么建议吗?
发布于 2016-12-02 13:55:17
notify(userID: string) {
return Observable.interval(5000)
.map(() => this.baseUrl + '/notification/GetUnseen?userId=' + userID)
.switchMap(url => {
this.dataService.set(url);
return this.dataService.get();
})
.map(response => ....{ <your handler> }
}注意: 1.您必须订阅返回的值才能开始调用。2.您的数据服务是状态的,这可能会创建两个不同客户端设置url然后调用它的争用条件,考虑将get转换为无状态函数:get(url),在这种情况下,您的代码将是
notify(userID: string) {
return Observable.interval(5000)
.map(() => this.baseUrl + '/notification/GetUnseen?userId=' + userID)
.switchMap(url => this.dataService.get(url))
.map(response => ....{ <your handler> }
}https://stackoverflow.com/questions/40933599
复制相似问题