13

angular2 をいじって、しばらくすると動かなくなってしまいました。

単一のリクエストに対して使用しhttp.getても問題なく動作しますが、かなりの時間をいじって、最終的に多くのreactivexのものを読んだ後、4秒ごとにライブデータをポーリングしたいと思います:

Observable.timer(0,4000)
  .flatMap(
    () => this._http.get(this._url)
       .share()
       .map(this.extractData)
       .catch(this.handleError)
  )
  .share(); 

-observable がリクエストの結果を発行した後に (4 秒) 間隔を開始する簡単な方法はありますか? (それとも観測地獄http.getに落ちてしまうのでしょうか?)

私が欲しいタイムライン:

Time(s): 0 - - - - - 1 - - - - - 2 - - - - - 3 - - - - - 4 - - - - - 5 - - - - - 6
Action:  Request - - Response - - - - - - - - - - - - - - - - - - - -Request-... 
Wait:                | wait for 4 seconds -------------------------> |
4

4 に答える 4

2

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; }
);
于 2016-06-24T06:06:32.747 に答える
1

ポーリングの遅延を以前のリクエストの完了ステータスに依存させたい場合に備えて、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

プランク

于 2017-08-29T11:11:52.203 に答える
-1

より便利な場合は、 interval を使用してみてください。呼び出すと、しばらくしてからポーリングをキャンセルできますsubscribeSubscription

let observer = Observable.interval(1000 * 4);
let subscription = observer.subsscribe(x => {
    this._http.get(this._url)
     .share()
     .map(this.extractData)
     .catch(this.handleError)
});

....
// if you don't require to poll anymore..
subscription.unsubscribe();
于 2016-06-21T08:21:00.180 に答える