RxJS pipe() 中的代码未被执行
Code within RxJS pipe() not Being Executed
对于以下代码:
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() 内部,但同样,从未命中断点。
我如何订阅 observable:
this.reportExecutionFacadeService.getExecution(this.modelId, this.reportId, null).subscribe((token) => {
console.log(token);
});
我正在使用 Chrome 开发人员工具调试器,如果有帮助的话 - 请让我知道我在这里不理解的地方。谢谢。
如果 mergeMap
上的 b 断点从未命中,这意味着您的函数 this.getNewExecution
反过来没有发出任何值。
确保函数 this.getNewExecution
returns 任何 Observable 类型(Subject、BehaviorSubject 等)并且该 observable 发出一个值。
只有当 this.getNewExecution
返回的 Observable 发射时,管道内的 mergeMap
才会 运行.
顺便说一句,你可以这样写你的代码(不需要链接管道两次):
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);
}),
tap((token: ExecutionToken) => this.updateExecutionTokenState(token))
)
}
BTW2:看起来像 assignExecutionToClient
returns 一个承诺。如果您希望获取由 promise 解决的结果并将其与 getNewExecution
流合并,最好将其转换为像这样的可观察对象(使用 'rxjs' 中的 from
):
public getExecution(modelId: string, reportId: string, params: ParameterSet | null): Observable<ExecutionToken> {
return this.getNewExecution(modelId, reportId, params, null)
.pipe(
mergeMap((token: ExecutionToken) => {
return from(this
.reportExecutionHubService
.assignExecutionToClient(token)
);
}),
tap((token: ExecutionToken) => this.updateExecutionTokenState(token))
)
}
当然,要真正实现 运行,您必须订阅返回的可观察对象。即在调用函数中,必须写:
someCallingFunction() {
this.getExecution(modelId, reportId, params)
.subscribe(token => console.log(token));
}
Observables 管道在被订阅之前不会运行。所以不订阅 getExecution
不会触发整个事件。
对于以下代码:
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() 内部,但同样,从未命中断点。
我如何订阅 observable:
this.reportExecutionFacadeService.getExecution(this.modelId, this.reportId, null).subscribe((token) => {
console.log(token);
});
我正在使用 Chrome 开发人员工具调试器,如果有帮助的话 - 请让我知道我在这里不理解的地方。谢谢。
如果 mergeMap
上的 b 断点从未命中,这意味着您的函数 this.getNewExecution
反过来没有发出任何值。
确保函数 this.getNewExecution
returns 任何 Observable 类型(Subject、BehaviorSubject 等)并且该 observable 发出一个值。
只有当 this.getNewExecution
返回的 Observable 发射时,管道内的 mergeMap
才会 运行.
顺便说一句,你可以这样写你的代码(不需要链接管道两次):
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);
}),
tap((token: ExecutionToken) => this.updateExecutionTokenState(token))
)
}
BTW2:看起来像 assignExecutionToClient
returns 一个承诺。如果您希望获取由 promise 解决的结果并将其与 getNewExecution
流合并,最好将其转换为像这样的可观察对象(使用 'rxjs' 中的 from
):
public getExecution(modelId: string, reportId: string, params: ParameterSet | null): Observable<ExecutionToken> {
return this.getNewExecution(modelId, reportId, params, null)
.pipe(
mergeMap((token: ExecutionToken) => {
return from(this
.reportExecutionHubService
.assignExecutionToClient(token)
);
}),
tap((token: ExecutionToken) => this.updateExecutionTokenState(token))
)
}
当然,要真正实现 运行,您必须订阅返回的可观察对象。即在调用函数中,必须写:
someCallingFunction() {
this.getExecution(modelId, reportId, params)
.subscribe(token => console.log(token));
}
Observables 管道在被订阅之前不会运行。所以不订阅 getExecution
不会触发整个事件。