반응형
rxjs 비동기 처리 예제
* 라우터 이동 혹은 5분에 한 번씩 api를 다시 조회
const REFRESH_INTERVAL = 60000 * 5; // 5분
public data$ = new Observable();
private cache1$: Observable<any> | undefined;
get SyncData(): any {
if (!this.cache1$) {
const timer$ = timer(0, REFRESH_INTERVAL);
const router$ = this.router.events.pipe(filter(e => e instanceof NavigationEnd));
this.cache1$ = merge(router$, timer$).pipe(
distinctUntilChanged(),
switchMap(_ => this.fetch('/api/data.php', 'get')),
shareReplay(1)
);
}
return this.cache1$;
}
this.data$ = this.SyncData
반응형
'개발 > front-end' 카테고리의 다른 글
Angular에서 뒤로가기 시 스크롤 위치 복원 (0) | 2024.07.17 |
---|---|
angular Infinite Scroll을 이용한 페이징 처리 (0) | 2024.07.17 |
angular 라이브러리 만들기 (0) | 2024.07.05 |
angualar CryptoJS 를 이용한 api 통신 예제 (0) | 2024.07.05 |
angualr directive 로 인쇄기능 만들기 (0) | 2024.07.05 |