从内部 flatMap 通知 [英] Notify from inner flatMap
问题描述
这是一个相当复杂的示例:
Here a quite complex sample:
主要内容:
this.runInstructionAndGetResult().subscribe({
next: val => console.log(`NEXT VALUE: ${val}`),
error: val => console.log(`ERROR VALUE: ${val}`),
complete: val => console.log(`COMPLETE`)
});
可观察的:
public runInstructionAndGetResult(): Observable<string> {
return this.runAnInstruction()
.flatMap((data) => {
console.info("flatMap of runAnInstruction:", data);
return this.getInstructionExecutionStatusInPolling()
.filter(data => data != "Polling")
.take(1)
.flatMap((data) => {
console.info("flatMap of getInstructionExecutionStatusInPolling:", data);
return this.getInstructionResult();
}).map((data) => {
console.info("Map of getInstructionResult:", data);
return data;
});
});
}
public runAnInstruction(): Observable<string> {
return Observable.of("StartRun");
}
public getInstructionResult(): Observable<string> {
return Observable.of("FinalResult");
}
public getInstructionExecutionStatusInPolling(): Observable<string> {
return Observable.interval(1000)
.concatMap(data => {
return this.getInstructionExecutionStatus();
});
}
public getInstructionExecutionStatus(): Observable<string> {
return Observable.of("Polling", "Terminate");
}
这里是:https://plnkr.co/edit/c1cahMtVARQnLgnHWlEe?p=preview
主要问题是我只想得到关于内部流外部进化"的通知.
Main problem is that i just would like to be notify about "evolution" of inner stream outside.
现在只有当所有内部 flatMap 都完成时,我们才在 main 上有next"事件.
如何获得通知?例如在轮询期间,我如何向主流发出显式值?
How to get notify? How can i emit explicit values to main stream for example during polling?
谢谢.
推荐答案
我找到了一个解决方案来分享.
I found a solution to share.
这里plunker更新:
Here plunker updated:
https://plnkr.co/edit/c1cahMtVARQnLgnHWlEe?p=preview
基本上我使用以下方法创建一个简单的可观察对象:https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/create.md
Basically i create a simple observable using : https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/create.md
然后我以编程方式调用 next 方法并最终完成:
then i call programmatically next method and complete finally:
public runInstructionAndGetResult(): Observable<string> {
return Observable.create((ops)=> {
ops.next(1);
this.runAnInstruction()
.concatMap((data) => {
ops.next(2);
console.info("flatMap of runAnInstruction:", data);
return this.getInstructionExecutionStatusInPolling()
.filter(data => data != "Polling")
.take(1)
.concatMap((data) => {
ops.next(3);
console.info("flatMap of getInstructionExecutionStatusInPolling:", data);
return this.getInstructionResult();
}).map((data) => {
console.info("Map of getInstructionResult:", data);
ops.next(4);
ops.complete();
return data;
});
}).subscribe();
});
}
这篇关于从内部 flatMap 通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!