在BehaviorSubject流上循环

编程入门 行业动态 更新时间:2024-10-24 04:30:12
本文介绍了在BehaviorSubject流上循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的线程变量包含3个不同的元素.我想创建一个对这三个对象进行迭代的For循环.

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

这是我的功能:

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]; } } }); }

此功能仅一次有效.在她的第一个电话中,她正在迭代这三个元素. 然后仅在最后一个元素上进行迭代.

解决方案

由于这是BehaviourSubject的性质,因此请显示当前"值,或者在这种情况下,请显示上次推送的值(不知道是否推送了正确的词,但这只是出于解释目的).当您使用next()方法向主题添加值时,它将为您显示最后一个值,在这种情况下,它将为您显示最后一个值,因此,仅循环一次(因为最后一个值只是一个).

也许您应该阅读有关Rx.Subject()的这篇中型文章,以更好地了解它们的工作方式和作用:

了解rxjs BehaviorSubject,ReplaySubject和AsyncSubject

在这种情况下不知道您当前的要求,但是也许您可以concat()上一个和最后一个值,或者选择ReplaySubject()(向观察者发出所有值).

有点像这样的东西(TS):

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

这对于Thread数组是可行的,但是如果您只想添加Thread实体,则应该使用Rx.ReplaySubject()

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.

解决方案

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()

更多推荐

在BehaviorSubject流上循环

本文发布于:2023-11-24 03:33:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1623823.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:BehaviorSubject

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!