Observables处理多个快速和慢速

时间:2018-03-31 20:27:45

标签: rxjs observable

我有一个应用程序,在启动时初始化并调用许多可观察对象。一些观察者很快完成,而另一些观察者则很慢。为了获得最佳性能,我希望并行运行observable(如果可能)并立即运行订阅代码(如果可能)。一些可观测量依赖于一个或多个其他可观测量成功完成。

实现这一目标的最佳方法或结构代码是什么?

以下简化示例似乎是有效的,但我的理解是嵌套的,多个订阅通常是不好的做法。此外,随着可观察量和相互依赖性的增加,将会有越来越多的订阅和嵌套订阅。此外,无法保证快速观察能够始终快速运行。

this.getAppConfig() // All observables depend on appConfig returning data successfully.
    .flatMap(() => {
        return Observable.of('If I am slow, I will block the final subscribe')
    })
    return Observable.forkJoin(
        Observable.of('If I am slow, I will block the final subscribe'),
        Observable.of('If I am slow, I will block the final subscribe')
     )
    .subscribe(() => {
        Observable.of('slow').subscribe(() => console.log('Do not block other observables'))
        Observable.of('fast').subscribe(() => console.log('Run me as soon as possible'))
        Observable.of('slow').subscribe(() => console.log('Do not block other observables'))
        Observable.of('fast').subscribe(() => console.log('Run me as soon as possible'))
    })

0 个答案:

没有答案