title: RxJS polling by interval and when manually called
body: <p>In my Angular app I'm working on notifications and I have an REST API to call for latest user's notifications. I need to call this API on few minutes since it's not really important that user gets notifications in real time (they probably won't even appear that fast). However the idea to refresh notifications on the client side is next:</p>
  
  <ul>
  <li>When user logs in start refreshing notifications - here is the first manual call to start refreshing the API on few minutes
  
  <ul>
  <li>If user leaves app opened or is just navigating through the app then don't change timer and wait for the rest of the time</li>
  <li>If user opens subpage where it can perform actions related to notifications and does it, then refresh notifications and reset timer</li>
  </ul></li>
  <li>Refresh notifications until logout</li>
  </ul>
  
  <p>I already have working code for the described procedure, but I'm somehow unsure that it's correct for what I need. Here is the code for performing calls (for manual check there is just a Subject and for stop checking there is a subscription to observable - code below is actually separated, but here is in one place because of readability):</p>
  
  <pre><code>// Subject for manual triggering
  this.checkFeed = new Subject&lt;void&gt;();
  
  // Call for refresh in own method
  this.checkFeed.next();
  
  // Waiting for manual refresh or triggering it on some interval after it was last triggered
  this.feedSub = this.checkFeed.asObservable()
        .switchMap(() =&gt; Observable.timer(0, this.interval))
        .mergeMap(() =&gt; this.fetchChanges())
        .distinctUntilChanged(this.compareFeed)
        .subscribe(res =&gt; this.notify(res));
  
  // Unsubscription when logging out
  if (this.feedSub) this.feedSub.unsubscribe();
  </code></pre>
  
  <p>The part which I'm most unsure about is <code>.switchMap(() =&gt; Observable.timer(0, this.interval))</code> since it needs <code>0</code> to start right away (which is ok, but still doesn't look correct at all?). So is there any better way to achieve what I described?</p>
  
  <p>I also have another question how to start check for notifications from another observable - which operator should I use. As I mentioned I have call to the Subject's next in own method like this:</p>
  
  <pre><code>refreshFeed(): void {
    this.checkFeed.next();
  }
  </code></pre>
  
  <p>So when there is some other observable performing (the action when notifications should be refreshed) I need to call this one. What's the correct way to call <code>void</code> method when other observable has response from API? I was thinking of something like this:</p>
  
  <pre><code>someActionThatCanChangeNotifications(): Observable&lt;any&gt; {
    return this.api.get('path/to/endpoint')
      .do(() =&gt; this.feedService.refreshFeed());
  }
  </code></pre>
  
  <p>Is this ok, or is there also any better way?</p>
  
  <p>Thanks in advance for help!</p>