I need to create a bot that tweets with random intervals, like this:
wait 1 hour
tweets something
wait 5 hours
tweets something
wait 30 minutes
tweets something
wait 10 hours
tweets something
where the delay time is in between 10 hours or something like that
The problem with observables is that I need to generate these intervals and then feed it into the chain of observables. For example, I need to generate an array with all the delays and only them subscribe to it in order to tweet.
Isn't there a way to create an observable that emits a new item just when itself ends? For example:
Rx.Observable.just().timer(randomTime()).subscribe(emitObservableAgain());
I think this is somehow not what Observables are meant to do. Like... is it a good pratice to connect the end of a chain of observables to itself?
ALSO, how to import the rx.timer into nodejs? Importing just 'rx' (from npm) won't give me these functions
You could use repeat:
Rx.Observable.just()
.switchMap(() => Rx.Observable.timer(randomTime()))
.do(tweetMyThing)
.repeat()
.subscribe();
Edit: I Have added a live-script that shows how the stream works:
// Helper-Methods to visualize the stream
function randomTime() {
return ~~(Math.random() * 2000) + 50;
}
var lastReceivedEvent = +new Date();
function tweetMyThing(value) {
console.log("Received after " + (+new Date() - lastReceivedEvent));
lastReceivedEvent = +new Date();
}
// \end of helper-methods
// the stream
Rx.Observable.just()
.switchMap(() => Rx.Observable.timer(randomTime()))
.do(tweetMyThing)
.repeat(20)
.subscribe();
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/4.1.0/rx.all.js"></script>