RxJS所有个人可观测值均已完成 [英] RxJS All Individual Observables completed

查看:70
本文介绍了RxJS所有个人可观测值均已完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个唯一ID数组,想对它们执行批量操作(HTTP补丁,删除等).这需要单独完成,而我需要显示单独的结果.

I have an array of unique ID's and want to perform a bulk action (HTTP patch, delete etc) on them. That needs to be done individually and I need to display individual results.

由于请求是单独的,因此它的响应不应相互影响.结果按收到时显示.

As the requests are individual and it's responses should not affect each other. Results are displayed as they are received.

每个调用都是一个单独的Observable,我正在寻找的是一种了解所有Observables何时完成的方法. onCompleted 方法仅在没有错误的情况下起作用.

Every call is a separate Observable and what I'm looking for is a way to know when all Observables have completed. The onCompleted method works only when there are no errors.

目的是防止在仍在处理呼叫的情况下单击按钮.

The goal is to prevent the button from being clicked while there are still call's being processed.

this.inProgress = {};
this.myIdArray = [1, 2, 3, 4];

actionHandler(type) {

    this.inProgress[type] = true;

    const myAction = {
        patch       : id => this.patch(id, this.body),
        delete      : id => this.delete(id)
    };

    from(myIdArray)
        .pipe (
            tap(myAction[type])
        )
        .subscribe(
            res => {}, 
            err => {}, 
            () => this.inProgress[type] = false ); // onCompleted
}


delete(id) {
    this.myService.delete(id).subscribe(
        res => {} // confirm
        err => {} // show error
    );
}

推荐答案

您可以使用 查看全文

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