39

I want to create an observable that returns data from a webapi. I'd like it to return the data immediately, and poll the API every 10 seconds. The code below shows I'm using the 'interval' method. But this delays the first set of data by 10 seconds. How do I get that first flush of data to come down with no initial delay?

export class EventService {
    public events$: Observable<Event[]>;
    private _eventsObserver: Observer<Event[]>;
    private pollInterval: number = 5000;

    private _dataStore: {
        events: Event[];
    };

    constructor(private http: Http) {
        this._dataStore = { events: [] };

        this.events$ = new Observable(observer => this._eventsObserver = observer)
            .startWith(this._dataStore.events)
            .share();
    }

    pollEvents() {
        return Observable.interval(10000)
            .switchMap(() => {
                return this.http.get('app/resources/data/scheduleevents.json')
                    .map((responseData) => {
                        return responseData.json();
                    });
            })
            .map((events: Array<any>) => {
                let result: Array<Event> = [];
                if (events["data"]) {
                    events["data"].forEach((event) => {
                        result.push(event);
                    });
                }
                return result;
            });
    }
}
Alexander Abakumov
  • 13,617
  • 16
  • 88
  • 129
FeeFiFoFum
  • 1,719
  • 1
  • 11
  • 18

6 Answers6

78

Got it:

        .interval(5000)
        .startWith(0);
Alexander Abakumov
  • 13,617
  • 16
  • 88
  • 129
FeeFiFoFum
  • 1,719
  • 1
  • 11
  • 18
  • but then I get 0 twice https://plnkr.co/edit/Cl5DQ7znJRDe0VTv0Ux5?p=preview – adamdport May 24 '17 at 18:07
  • 1
    @adamport the first zero is what you initialized with **startWith(0)**, but the second zero is from the 0-index from the successive 4 executions **take(4)** – Andrew Lobban Dec 12 '17 at 20:05
21

Use timer. I think the timer is what you need (see RxJS tab): http://reactivex.io/documentation/operators/timer.html#collapseRxJS

Could be used like:

Observable.timer(0, 5000).flatMap(() => apiCall())

Where 0 - delay before emitting the first value, 5000 - emit value after each 5s

enter image description here

Maksim Nesterenko
  • 5,661
  • 11
  • 57
  • 91
  • http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#static-method-timer I don't think it is deprecated. – maxisam Dec 13 '17 at 19:41
3
    let timer = TimerObservable.create(0, 5000);
    this.sub = timer.subscribe(t => {
        this.yourMethod()
    });

To unsubscribe run this.sub.unsubscribe()

Yossi Neiman
  • 815
  • 1
  • 7
  • 13
2

I personnally use interval with startWith (need RxJs 6+), here is a complete example:

   history: any;
   historySubscription: Subscription;

   constructor(private jobService: JobService) { }

   ngOnInit() {

      this.historySubscription = interval(10000).pipe(
         startWith(0),
         flatMap(() => this.jobService.getHistory())
      ).subscribe(data => {
         this.history = data;
      });

   }

   ngOnDestroy() {
      this.historySubscription.unsubscribe();
   }

This retrieves history on init and then every 10 seconds.

Another alternative is using timer as explained by @Alendorff.

Emeric
  • 6,315
  • 2
  • 41
  • 54
0

For angualr2 below is the code i have written in my application and it is working as expected -

In service --

import { Observable } from 'rxjs/Observable';
import 'rxjs/Rx';

getList(): Observable<IVM> {
    return Observable.interval(5000).startWith(0)
          .switchMap(() => 
             this._http.get(this._vmURL )
                 .map((response: Response) => <IVM>response.json().data)
                 .do(data => console.log('All: ' + JSON.stringify(data)))
                .catch(this.handleError)
             );
}

In component --

private getInstanceDetails(): void {
    this._vmDataService.getList()
        .subscribe(vmList => {
           //Do whatever you want with the vmList here :) 
        },
        error => this.errorMessage = <any>error);
}

Thanks, Kindly let me know your thoughts.

0
Observable.interval(5L, TimeUnit.SECONDS)
        .startWith(0)
        .observeOn(AndroidSchedulers.mainThread())
        .map { foobar() }

works fine for me. Thanks

SebLu
  • 19
  • 1