Loop For on a BehaviorSubject stream

泪湿孤枕 提交于 2019-12-24 19:17:44

问题


My thread variable contains 3 different elements. I want to create a For loop that iterates on these three objects.

threads: BehaviorSubject<{[key: string]: Thread }> = new BehaviorSubject({});

Here is my function:

searchUser(): Observable<Thread> {
this.searchArrayThreads = [];
let element = document.getElementById('chat-window-input');

return this.threads.map((threadDictionary: {[key: string]: Thread}) => {
  for( let key in threadDictionary ) {
    console.log("key", threadDictionary[key]);
    if(threadDictionary[key].participants[0].name.startsWith(str)) {
      return threadDictionary[key];
    }
  }
});
}

This function only works once. In her first call, she is iterating the 3 elements. Then it iterates just on the last element.


回答1:


Because that's the nature of BehaviourSubject, show the "current" value or in this case the last value pushed to it (don't know if push it's the right word but this is just to explanation purposes). When you use the next() method to add values to the subject it will show you the last value, in this case it will show you the last value, therefore, loop only once (because the last value it's just one).

Maybe you should read this Medium article about Rx.Subject() to understand better how they work and what they do:

Understanding rxjs BehaviorSubject, ReplaySubject and AsyncSubject

Don't know your current requirements in this particular case but maybe you can concat() previous and last values or maybe opt for ReplaySubject() (that emit all values to the observers).

Something ROUGHLY like this (TS):

const currentValues: Thread[] = this.threads.getValues(); // "current" values
const newValues: Thread[] = newThreadsValues; //new Threads from somewhere

this.threads.next(currentValues.concat(newValues);

This woulds work for Thread arrays but if you want to add only a a Thread entity you should go for Rx.ReplaySubject()



来源:https://stackoverflow.com/questions/46038790/loop-for-on-a-behaviorsubject-stream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!