Skip to content

Instantly share code, notes, and snippets.

@seanislegend
Last active May 10, 2025 03:56
Show Gist options
  • Save seanislegend/94b3b0bd70b6d9d1af2c to your computer and use it in GitHub Desktop.
Save seanislegend/94b3b0bd70b6d9d1af2c to your computer and use it in GitHub Desktop.

Revisions

  1. seanislegend revised this gist Sep 17, 2015. 1 changed file with 11 additions and 5 deletions.
    16 changes: 11 additions & 5 deletions rxjs-example-poll-url.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,9 @@
    let timeout = 1000;

    /**
    * Create a custom observable that creates a XHR request and returns complete when the promise is fulfilled
    */
    let dataObservable = Rx.Observable.create((o) => {
    let observable = Rx.Observable.create((o) => {
    dataService.fetch('test.json')
    .then((data) => {
    o.onNext(data);
    @@ -12,11 +14,15 @@ let dataObservable = Rx.Observable.create((o) => {


    /**
    * Use our observable and call every 2 seconds.
    * Call our observable immediately and then add an interval for the polled requests
    */
    let source = Rx.Observable
    .interval(this.options.timeout)
    .flatMapLatest(() => this.observer);
    let source = observable
    .take(1)
    .merge(
    Rx.Observable
    .interval(timeout)
    .flatMapLatest(observable)
    )



  2. seanislegend revised this gist Sep 15, 2015. 1 changed file with 0 additions and 3 deletions.
    3 changes: 0 additions & 3 deletions rxjs-example-poll-url.js
    Original file line number Diff line number Diff line change
    @@ -11,9 +11,6 @@ let dataObservable = Rx.Observable.create((o) => {
    });


    /**
    * Use our observable and call every 2 seconds.
    */
    /**
    * Use our observable and call every 2 seconds.
    */
  3. seanislegend revised this gist Sep 15, 2015. 1 changed file with 8 additions and 4 deletions.
    12 changes: 8 additions & 4 deletions rxjs-example-poll-url.js
    Original file line number Diff line number Diff line change
    @@ -12,11 +12,15 @@ let dataObservable = Rx.Observable.create((o) => {


    /**
    * Use our observable and call every 2 seconds. Repeat ensure it continues to be called.
    * Use our observable and call every 2 seconds.
    */
    let source = dataObservable
    .sample(2000)
    .repeat();
    /**
    * Use our observable and call every 2 seconds.
    */
    let source = Rx.Observable
    .interval(this.options.timeout)
    .flatMapLatest(() => this.observer);



    /**
  4. seanislegend created this gist Sep 15, 2015.
    29 changes: 29 additions & 0 deletions rxjs-example-poll-url.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    /**
    * Create a custom observable that creates a XHR request and returns complete when the promise is fulfilled
    */
    let dataObservable = Rx.Observable.create((o) => {
    dataService.fetch('test.json')
    .then((data) => {
    o.onNext(data);
    o.onCompleted();
    })
    .fail(o.onError);
    });


    /**
    * Use our observable and call every 2 seconds. Repeat ensure it continues to be called.
    */
    let source = dataObservable
    .sample(2000)
    .repeat();


    /**
    * Return events from the observable.
    */
    source.subscribe(
    (data) => console.log(data),
    (error) => console.log(error),
    () => console.log('done')
    );