我和angular2一起玩了一会儿就被困住了。
对于单个请求,使用http.get
可以正常工作,但我希望每4秒轮询一次实时数据,在修改了很长一段时间并阅读了大量的reactivex内容之后:
Observable.timer(0,4000)
.flatMap(
() => this._http.get(this._url)
.share()
.map(this.extractData)
.catch(this.handleError)
)
.share();
在http.get
-observable发出请求结果之后,是否有一种简单的方法来启动(4秒)间隔?(或者我最后会被发现-地狱?)
我想要的时间线:
Time(s): 0 - - - - - 1 - - - - - 2 - - - - - 3 - - - - - 4 - - - - - 5 - - - - - 6
Action: Request - - Response - - - - - - - - - - - - - - - - - - - -Request-...
Wait: | wait for 4 seconds -------------------------> |
发布于 2016-06-23 23:53:06
更新为RxJS 6
import { timer } from 'rxjs';
import { concatMap, map, expand, catchError } from 'rxjs/operators';
pollData$ = this._http.get(this._url)
.pipe(
map(this.extractData),
catchError(this.handleError)
);
pollData$.pipe(
expand(_ => timer(4000).pipe(concatMap(_ => pollData$)))
).subscribe();
我使用的是RxJS 5,我不确定RxJS 4等效操作符是什么。无论如何,这里是我的RxJS 5解决方案,希望它能有所帮助:
var pollData = this._http.get(this._url)
.map(this.extractData)
.catch(this.handleError);
pollData.expand(
() => Observable.timer(4000).concatMap(() => pollData)
).subscribe();
展开操作符将发出数据,并递归地启动一个新的可观察到的每一个发射。
发布于 2016-06-23 22:06:32
我自己设法做到了,唯一的缺点是http.get
不能更容易地重复。
pollData(): Observable<any> {
//Creating a subject
var pollSubject = new Subject<any>();
//Define the Function which subscribes our pollSubject to a new http.get observable (see _pollLiveData() below)
var subscribeToNewRequestObservable = () => {
this._pollLiveData()
.subscribe(
(res) => { pollSubject.next(res) }
);
};
//Subscribe our "subscription-function" to custom subject (observable) with 4000ms of delay added
pollSubject.delay(4000).subscribe(subscribeToNewRequestObservable);
//Call the "subscription-function" to execute the first request
subscribeToNewRequestObservable();
//Return observable of our subject
return pollSubject.asObservable();
}
private _pollLiveData() {
var url = 'http://localhost:4711/poll/';
return this._http.get(url)
.map(
(res) => { return res.json(); }
);
};
以下是您不能使用更直接的订阅的原因:
var subscribeToNewRequestObservable = () => {
this._pollLiveData()
.subscribe(pollSubject);
};
完成后,http.get
**-observable也将完成您的主题并防止它发出更多的项目。**
这仍然是一个冷冰冰的观察,因此,除非你订阅它,不会提出任何要求。
this._pollService.pollData().subscribe(
(res) => { this.count = res.count; }
);
发布于 2017-08-29 03:11:52
如果您希望轮询延迟取决于以前的请求完成状态,则可以从Can Nguyen对回答稍加修改。
var pollData = () => request() // make request
.do(handler, errorHandler) // handle response data or error
.ignoreElements() // ignore request progress notifications
.materialize(); // wrap error/complete notif-ns into Notification
pollData() // get our Observable<Notification>...
.expand( // ...and recursively map...
(n) => Rx.Observable // ...each Notification object...
.timer(n.error ? 1000 : 5000) // ...(with delay depending on previous completion status)...
.concatMap(() => pollData())) // ...to new Observable<Notification>
.subscribe();
扑通。
或者另一种选择:
var pollData = () => request() // make request
.last() // take last progress value
.catch(() => Rx.Observable.of(null)); // replace error with null-value
pollData()
.expand(
(data) => Rx.Observable
.timer(data ? 5000 : 1000) // delay depends on a value
.concatMap(() => pollData()))
.subscribe((d) => {console.log(d);}); // can subscribe to the value stream at the end
扑通。
https://stackoverflow.com/questions/37938735
复制相似问题