我有一个包含数组的可观测对象,我在UI中使用该数组和一个异步管道来显示合同表.该表中的一列显示页面加载时"未处理"的状态值.
contracts$: Observable<ContractDto[]>
个
在查看init时,我从类似this.contracts$ = this.api.getContracts()
的API加载合同
这很好,现在来说说我的问题:
通过单击按钮,用户可以启动运行所有合同的流程,以执行每个合同的操作.每个操作包含一个API调用,可能需要1-3秒.
现在,我想更新视图,向用户显示哪些合同已经处理,这样他就可以得到一个指示器,了解处理的进度.因此,我想在observable中覆盖合约,并在操作执行后将状态字段更新为"processed".
但是我找不到一种方法来覆盖可观察的合约并在每次迭代中更新这个.contracts$.我唯一能做的就是一个接一个地迭代和处理,但只有在处理了all个动作时才更新这个.contracts$.
我当前的代码如下所示:
// this only gets updated once all contracts have been processed
this.contracts$ = this.contracts$
.pipe(
// flatten array of contracts
concatMap(contract => contract),
// executing the process one by one; returning the updated contract
concatMap(contract => this.deploy(contract)),
// without toArray() i get an error on this.contracts$ assignment because
// ContractDto can not be assigned to ContractDto[] - which is correct.
// and here is my missing link because I want to update this.contracts$ after EACH iteration.
toArray(),
)
非常感谢您的时间提前