使用takeUntil()关闭条件的订阅 [英] Close subscription on condition with takeUntil()

查看:155
本文介绍了使用takeUntil()关闭条件的订阅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个订阅,可以从NgRx减速器获取提供程序.我想使用takeUntil()自动关闭订阅,直到最终返回具有内容的数组:

I have a subscription to get providers from a NgRx reducer. I want to use takeUntil() to automatically close the subscription when is finally returns an array that has content:

// Fetch providers
this.store.pipe(select(reducer.getProviders))
//  .takeUntil(reducer.getProviders.length > 0)
    .subscribe(providers => {
        if (providers) {
            this.providers = providers;
//          takeUntil(/** something **/);
        }
    });

有人可以帮我吗?

我不知道如何使用takeUntil()

推荐答案

takeUntil接受一个Observable. (来源:文档).对于您的情况,使用takeWhile更有意义,只要满足特定条件,它就会发出值(来源:

takeUntil accepts an Observable. (Source: 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). Set the optional inclusive property to true so that it will also emit the first item that didn't pass the predicate.

this.store.pipe(select(reducer.getProviders))
    .takeWhile(reducer => reducer.getProviders.length == 0, true)
    .subscribe(providers => {
        if (providers) {
            this.providers = providers;
        }
    });

这篇关于使用takeUntil()关闭条件的订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆