[英]Angular listen on subscription start of rxjs observable?
Is there a way to detect the start of the subcription of a rxjs observable within the pipe?有没有办法检测管道内可观察的 rxjs 的订阅开始?
I want to trigger a loading indicator when a http observable (destroyed when respone has been finalized) get subscribed.我想在订阅 http observable(在响应完成时销毁)时触发加载指示器。
Or do I have to create a wrapper observable for this action?或者我是否必须为此操作创建一个可观察的包装器?
It depends on what RxJS version you're using.这取决于您使用的 RxJS 版本。 With RxJS < 7.3 you can use
defer()
:使用 RxJS < 7.3 你可以使用
defer()
:
defer(() => {
loadingFlag = true;
return this.http.doyourrequest().pipe(
finalize(() => loadingFlag = false),
);
});
Since RxJS >= 7.3 you can use new event handlers for tap()
:由于 RxJS >= 7.3 你可以为
tap()
使用新的事件处理程序:
this.http.doyourrequest().pipe(
tap({
subscribe: () => loadingFlag = true,
finalize: () => loadingFlag = false,
}),
);
since every subscription to http observable causes new http call it is safe to set such flag outside of the pipe, at least it is how I do it.由于对 http observable 的每个订阅都会导致新的 http 调用,因此在管道外设置此类标志是安全的,至少我是这样做的。
getSomeData(){
loadingFlag=true;
return this.http.doyourrequest().pipe(finalize(()=>loadingFlag=false));
}
if you really want to go trough sub count(which is always down to 0 after every request due to finalizztion of http obs) check the implementation of refCount() and share() operators which internally counts the subscribers如果你真的想通过子计数(由于 http obs 的 finalizztion,每次请求后总是下降到 0)检查 refCount() 和 share() 运算符的实现,它们在内部对订阅者进行计数
Instead of setting some indicator as a side effect in your stream, try creating an observable that returns the state of your request.不要在您的流中设置一些指标作为副作用,而是尝试创建一个返回请求状态的 observable。
readonly makeRequestSubject = new Subject<RequestParams>();
readonly request$ = makeRequestSubject.pipe(
switchMap(params => this.doRequest(params).pipe(
map(result => ({ params, result, state: 'complete' })),
catchError(error => ({ error, params, state: 'error' }))
startWith({ params, state: 'loading' })
),
startWith({ state: 'notstarted' }),
shareReplay(1)
);
readonly isLoading$ = this.request.pipe(map(x => x === 'loading'), distinctUntilChanged());
readonly results$ = this.request.pipe(map(x => x === 'complete' ? x.results : []));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.