RxJSでポーリングする方法
ポーリング処理をするサービスのソースコード
app.service.ts
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Subject, Observable, interval } from 'rxjs';
import { flatMap, takeWhile } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class AppService {
subject = new Subject<any>();
data$ = this.subject.asObservable();
constructor(
private http: HttpClient
) {
this.polling().subscribe(data => {
this.subject.next(data);
});
}
polling(): Observable<any> {
return interval(1000).pipe(
flatMap(() => this.http.get('http://localhost:3000/data')),
takeWhile(() => true),
);
}
}
あとは、data$
をsubscribeするだけ。
Author And Source
この問題について(RxJSでポーリングする方法), 我々は、より多くの情報をここで見つけました https://qiita.com/shinoshu/items/d018cf5cad457678ecaa著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .