I have a process that sends me packets of data at intervals, and I need to manage that stream based on the timing of when the packets arrive and so on. At some point I also close the stream and the process.
Right now, I'm using a set of timers to do this, but I hope I can do it with rxjs since it seems a very good fit for this kind of thing. So far, I haven't had much success.
The problem
The stream is supposed to send me packets at regular intervals, but it usually deviates a lot and sometimes gets stuck.
I want to close the stream at some point, under the following conditions:
- If it takes more than
startDelayto send me the first packet. - After the first packet is sent, if there is a pause of more than
middleDelaybetween two packets. - After a constant time period
maxChannelTime.
When I'm about to close the stream due to any of the above reasons, I first request it to close politely so it can do some cleanup. Sometimes it also sends me a final data packet during the cleanup. But I want to wait no longer than cleanupTime for the cleanup and last data to arrive before I close the stream and ignore any more messages.
Elaboration
I'll create the "streams" by wrapping an event with an Observable. I have no trouble doing that.
By "closing" a stream, I mean telling the process to stop emitting data, and possibly to close (i.e. die).