[英]Close subscription on condition with takeUntil()
I have a subscription to get providers from a NgRx reducer. 我已订阅,可以从NgRx减速器获取提供程序。 I want to use
takeUntil()
to automatically close the subscription when is finally returns an array that has content: 我想使用
takeUntil()
在最终返回具有内容的数组时自动关闭订阅:
// Fetch providers
this.store.pipe(select(reducer.getProviders))
// .takeUntil(reducer.getProviders.length > 0)
.subscribe(providers => {
if (providers) {
this.providers = providers;
// takeUntil(/** something **/);
}
});
Can someone help me with this? 有人可以帮我弄这个吗?
I can't figure out how to make use of takeUntil()
我不知道如何利用
takeUntil()
You're looking for takeWhile()
. 您正在寻找
takeWhile()
。
takeUntil()
's parameter is an observable, and once that observable emits it will stop taking values. takeUntil()
的参数是一个可观察值,一旦该可观察值发出,它将停止获取值。
takeWhile()
's parameter is a predicate which determines whether to take values. takeWhile()
的参数是确定是否采用值的谓词。
See the documentation for takeWhile on learn-rxjs: https://www.learnrxjs.io/operators/filtering/takewhile.html 请参阅有关学习-rxjs上的takeWh的文档: https ://www.learnrxjs.io/operators/filtering/takewhile.html
takeWhile(predicate: function(value, index): boolean, inclusive?: boolean): Observable
takeWhile(predicate:function(value,index):boolean,inclusive ?: boolean):可观察
Emit values until provided expression is false.
发射值,直到提供的表达式为假。
takeUntil
accepts an Observable. takeUntil
接受一个Observable。 (Source: docs ). (来源: docs )。 For your case, it would make more sense to use
takeWhile
, this will emit values as long as a particular condition is satisfied (Source: docs ). 对于您的情况,使用
takeWhile
会更有意义,只要满足特定条件,它就会发出值(来源: docs )。 Set the optional inclusive
property to true so that it will also emit the first item that didn't pass the predicate. 将可选的
inclusive
属性设置为true,以便它还会发出未通过谓词的第一项。
this.store.pipe(select(reducer.getProviders))
.takeWhile(reducer => reducer.getProviders.length == 0, true)
.subscribe(providers => {
if (providers) {
this.providers = providers;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.