RxJS pipeline() 中的代码未执行
对于以下代码:
public getExecution(modelId: string, reportId: string, params: ParameterSet | null): Observable<ExecutionToken> {
return this.getNewExecution(modelId, reportId, params, null)
.pipe(
mergeMap((token: ExecutionToken) => {
return this.reportExecutionHubService.assignExecutionToClient(token).then(resolve => token);
})
)
.pipe(
tap((token: ExecutionToken) => this.updateExecutionTokenState(token))
);
}
我在 mergeMap() 行上放置了一个断点;但断点从未被命中。
击中了 tap(...) 行上的断点;但 this.updateExecutionTokenState(token))
未被调用 - 我在 private updateExecutionTokenState(token: ExecutionToken): void {
的第一行放置了一个断点,并且该断点从未被调用打。
private updateExecutionTokenState(token: ExecutionToken): void {
this.store.dispatch(new executionTokenActions.UpdateExecutionToken({ // breakpoint on this line
reportId: token.reportId,
executionSessionId: token.executionSessionId,
executionRequestId: token.executionRequestId,
modelRevisionId: token.modelRevisionId,
dateReceived: token.dateReceived
}));
}
同样,对于以下代码:
public get(payload: ReportExecutionRequest): Observable<ExecutionToken> {
return this.apiService.postWithReturn<ReportExecutionRequestResponse>('reportExecution/', payload)
.pipe(
catchError((response: HttpErrorResponse): Observable<any> => {
if (response.error?.code === this.SESSION_TIMEOUT_ERROR_CODE) {
// this.store.dispatch(new modelActions.SetPageErrored(true));
this.store.dispatch(new executionTokenActions.ExpireCurrentReportExecutionToken());
// swallow the error
return of();
}
return throwError(response);
}),
map<ReportExecutionRequestResponse, ExecutionToken>((response) => {
if (payload.executionSessionId !== response.executionSessionId) {
this.toastrService.info('toast_info_preparing_execution_session');
}
const token: ExecutionToken = {
reportId: payload.targetSet,
dateReceived: new Date(),
executionSessionId: response.executionSessionId,
executionRequestId: response.executionRequestId,
modelRevisionId: payload.modelRevisionId
};
// this.reportExecutionLocalStorageService.add(token);
this.store.dispatch(new executionTokenActions.AddExecutionToken(token));
return token;
})
);
}
行:上放置了一个断点
if (payload.executionSessionId !== response.executionSessionId) {
我在 map() 内部的
,但同样,该断点从未被命中。我如何订阅可观察的:
this.reportExecutionFacadeService.getExecution(this.modelId, this.reportId, null).subscribe((token) => {
console.log(token);
});
我正在使用 Chrome 开发者工具调试器,如果有帮助的话 - 请让我知道我在这里不理解的内容。谢谢。
For the following code:
public getExecution(modelId: string, reportId: string, params: ParameterSet | null): Observable<ExecutionToken> {
return this.getNewExecution(modelId, reportId, params, null)
.pipe(
mergeMap((token: ExecutionToken) => {
return this.reportExecutionHubService.assignExecutionToClient(token).then(resolve => token);
})
)
.pipe(
tap((token: ExecutionToken) => this.updateExecutionTokenState(token))
);
}
I put a breakpoint on the line with the mergeMap(); but the breakpoint was never hit.
The breakpoint on the tap(...) line was hit; but this.updateExecutionTokenState(token))
was not called - I put a breakpoint on the first line of private updateExecutionTokenState(token: ExecutionToken): void {
and that breakpoint was never hit.
private updateExecutionTokenState(token: ExecutionToken): void {
this.store.dispatch(new executionTokenActions.UpdateExecutionToken({ // breakpoint on this line
reportId: token.reportId,
executionSessionId: token.executionSessionId,
executionRequestId: token.executionRequestId,
modelRevisionId: token.modelRevisionId,
dateReceived: token.dateReceived
}));
}
Likewise, for the following code:
public get(payload: ReportExecutionRequest): Observable<ExecutionToken> {
return this.apiService.postWithReturn<ReportExecutionRequestResponse>('reportExecution/', payload)
.pipe(
catchError((response: HttpErrorResponse): Observable<any> => {
if (response.error?.code === this.SESSION_TIMEOUT_ERROR_CODE) {
// this.store.dispatch(new modelActions.SetPageErrored(true));
this.store.dispatch(new executionTokenActions.ExpireCurrentReportExecutionToken());
// swallow the error
return of();
}
return throwError(response);
}),
map<ReportExecutionRequestResponse, ExecutionToken>((response) => {
if (payload.executionSessionId !== response.executionSessionId) {
this.toastrService.info('toast_info_preparing_execution_session');
}
const token: ExecutionToken = {
reportId: payload.targetSet,
dateReceived: new Date(),
executionSessionId: response.executionSessionId,
executionRequestId: response.executionRequestId,
modelRevisionId: payload.modelRevisionId
};
// this.reportExecutionLocalStorageService.add(token);
this.store.dispatch(new executionTokenActions.AddExecutionToken(token));
return token;
})
);
}
I placed a breakpoint on the line:
if (payload.executionSessionId !== response.executionSessionId) {
which is just inside the map(), but again, the breakpoint was never hit.
How I am subscribing to the observable:
this.reportExecutionFacadeService.getExecution(this.modelId, this.reportId, null).subscribe((token) => {
console.log(token);
});
I am using Chrome developer tools debugger if that helps - please let me know what I am not understanding here. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果
mergeMap
上的 b 断点从未被命中,则意味着您的函数this.getNewExecution
不会发出任何值。确保函数
this.getNewExecution
返回任何 Observable 类型(Subject、BehaviourSubject 等),并且该 observable 发出一个值。只有当
this.getNewExecution
返回的 Observable 发出时,管道内的mergeMap
才会运行。顺便说一句,您可以像这样编写代码(不需要两次链接管道):
BTW2:看起来
assignExecutionToClient
返回一个承诺。如果您希望获取由 Promise 解析的结果并将其与getNewExecution
流合并,最好将其转换为像这样的可观察对象(使用from
from ' rxjs'):当然,为了使整个过程真正运行,您必须订阅返回的可观察值。也就是说,在调用函数中,必须这样写:
Observables 管道在被订阅之前不会运行。因此,不订阅
getExecution
将不会触发整个事件。If b breakpoint on the
mergeMap
is never hit, that means that your functionthis.getNewExecution
in turn, is not emitting any value.Make sure that the function
this.getNewExecution
returns any Observable type (Subject, BehaviorSubject, etc.) and that the observable emits a value.Only when the Observable returned by
this.getNewExecution
emits, only thenmergeMap
inside the pipe would run.BTW, you can write your code like so (no need to chain pipe twice):
BTW2: It looks like
assignExecutionToClient
returns a promise. If you're looking to take the result resolved by the promise and merge it with thegetNewExecution
stream, its better to turn it into an observable like so (usingfrom
from 'rxjs'):And of course, for the whole thing to actually run, you must subscribe to the returned observable. That is, in the calling function, you must write:
Observables pipe will not run until they are subscribed to. So not subscribing to
getExecution
will not trigger the whole thing.