admin管理员组文章数量:1420205
I have the following Observable
:
dataService.ts
findMessages(chatItem: any): Observable<any[]> {
return Observable.create((observer) => {
this.firebaseDataService.findMessages(chatItem).subscribe((firebaseItems: any[]) => {
// do something
observer.next(somedata);
});
});
}
which calls this function:
firebaseDataService.ts
findMessages(chatItem: any): Observable<any[]> { // populates the firelist
return this.af.database.list('/message/', {
query: {
orderByChild: 'negativtimestamp'
}
}).map(items => {
const filtered = items.filter(
item => ((item.memberId1 === chatItem.memberId1 && item.memberId2 === chatItem.memberId2)
|| (item.memberId1 === chatItem.memberId2 && item.memberId2 === chatItem.memberId1))
);
return filtered;
});
}
The dataService.findMessages(chatItem)
function is only ever called once.
This observes the firebase list. So if any items on the list change, this Observable
is fired.
Problem
If the function is accessed, or one item is added to the list, the Observer is fired as expected, but the // do something
line is called multiple times. I would only expect it to be called once.
Question
- Is there a way to enforce that it's only called once?
- Or once it's called for the first time, to break out of the
subscribe
? - Or, is my code pletely wrong?
Any advise appreciated.
I have the following Observable
:
dataService.ts
findMessages(chatItem: any): Observable<any[]> {
return Observable.create((observer) => {
this.firebaseDataService.findMessages(chatItem).subscribe((firebaseItems: any[]) => {
// do something
observer.next(somedata);
});
});
}
which calls this function:
firebaseDataService.ts
findMessages(chatItem: any): Observable<any[]> { // populates the firelist
return this.af.database.list('/message/', {
query: {
orderByChild: 'negativtimestamp'
}
}).map(items => {
const filtered = items.filter(
item => ((item.memberId1 === chatItem.memberId1 && item.memberId2 === chatItem.memberId2)
|| (item.memberId1 === chatItem.memberId2 && item.memberId2 === chatItem.memberId1))
);
return filtered;
});
}
The dataService.findMessages(chatItem)
function is only ever called once.
This observes the firebase list. So if any items on the list change, this Observable
is fired.
Problem
If the function is accessed, or one item is added to the list, the Observer is fired as expected, but the // do something
line is called multiple times. I would only expect it to be called once.
Question
- Is there a way to enforce that it's only called once?
- Or once it's called for the first time, to break out of the
subscribe
? - Or, is my code pletely wrong?
Any advise appreciated.
Share Improve this question edited May 8, 2017 at 11:08 Richard asked May 8, 2017 at 9:55 RichardRichard 8,96534 gold badges123 silver badges255 bronze badges 6- stackoverflow./questions/43831736/… – martin Commented May 8, 2017 at 10:12
-
Hi Martin, thanks for the ment. I do however get,
[ts] Property 'multicast' does not exist on type 'Observable<any[]>'.
. I may need to investigate a little further. – Richard Commented May 8, 2017 at 10:49 -
1
=> resolved by:
import 'rxjs/add/operator/multicast';
– Richard Commented May 8, 2017 at 10:55 -
I am not sure how to construct the parameters for
multicast(parameters)
. It looks like I need aRx.Subject
, but what is this? How do i get this? – Richard Commented May 8, 2017 at 11:04 - use take(1) if you need to have only one emition – Julia Passynkova Commented May 8, 2017 at 15:14
1 Answer
Reset to default 3but the // do something line is called multiple times
It is called in the callback passed to this.firebaseDataService.findMessages(chatItem).subscribe
. The subscribe
function will fire whenever there is a new item. If you only want to take one item you can use the take
operator
More
Docs on take
: https://github./Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/take.md
本文标签: javascriptObservable subscribe called multiple timesStack Overflow
版权声明:本文标题:javascript - Observable subscribe called multiple times? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745310529a2652909.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论