【发布时间】:2021-11-26 08:26:04
【问题描述】:
我有一个 API 调用是可观察的情况。 我还有一个可观察的 B 来跟踪该查询的进度:
generateUrl$(uploadConfig: GenerateUploadUrlVariables, file: File) {
const generation$ = new BehaviorSubject<ActionWithResult<{ url: string }>>({
state: 'INIT',
result: null,
});
const complete = function (result: ActionWithResult<{ url: string }>) {
generation$.next(result);
generation$.complete();
};
this.generateUploadUrl(uploadConfig).pipe(
switchMap((result) => {
generation$.next({ state: 'UPLOADING', result: null });
const url = result.data.generateUploadUrl.url || '';
return this.httpClient
.pipe(
tap(() => {
complete({ state: 'SUCCEEDED', result: { url } });
}),
catchError((e) => {
complete({ state: 'FAILED', result: null });
return throwError(() => e);
})
);
}),
catchError((e) => {
complete({ state: ActionState.FAILED, result: null });
return throwError(() => e);
})
).subscribe();
return generation$
}
现在可以了
this.generateUrl$(option, file).subscribe(e=> {console.log(e)})
我会得到查询的状态,以及查询成功后的结果。
但问题是,如果有人犯了错误,干脆做:
this.generateUrl$()没有订阅,不会跟踪结果,但API仍然会被调用。
我想将 api observable this.generateUploadUrl 与跟踪 observable generation$ 绑定
类似
return generation$.pipe(
whenSomeoneSubscribeToIt(() => {
this.generateUploadUrl(uploadConfig)
.pipe(//all the stuff above)
.subscribe()
})
)
有可能吗?
【问题讨论】:
标签: rxjs