I get why removing global state is a good thing in general, but here you are replacing it with a whole lot of complexity for not a particularly clear reason. Is tracking the user count with an integer dangerous <i>in this case</i> (Have you run into problems with it)? Is your solution not using global state anyway, just wrapped in a library?<p>All in all, are the abstractions (a bunch of new concepts plus a new library) <i>worth it</i> to just replace an integer?<p><pre><code> function timerFunc() {
if (connectionCounter) get results();
}
socket.on('connect', function() { connectCounter++; });
socket.on('disconnect', function() { connectCounter--; });
</code></pre>
The real problem is you want to start and stop the timer when someone connects. Just keep it simple and keep it going, but not doing anything if there is nobody connected, like above.
Hmm...<p>"The server always asks for new location data, every 30 seconds, even when no one is listening."<p>...<p>"Our task is to pause the timer when there are no websocket connections and start the timer when a client connects."<p>That sounds like asking for trouble. Maybe there's something I am not understanding, but why not have the timer run continuously and have the timer <i>action</i> check whether it needs to request location data?<p>Yes, that will cause the process to wake up every 30 seconds even if there is no action, but unless circumstances are very special that won't be a significant problem.
Very nice explanation. Gives a good insight how you can "think in observables". Two small things 1. instead of merging zero$ into connectionClosed$ you can use the startWith() method. 2. Instead of using the head method to pluck the first item of an array you could use array destructuring. (But I can understand that you wouldn't do it in this to limit the amount of "new" concepts)
Send a message to a queue asking for refreshed data... ignore messages that occur within a given timeframe.<p>Or put your server in front of a CDN / Nginx, set cache headers for 30 seconds ;)