Created
March 18, 2015 23:23
-
-
Save bobpace/2e6f926f5dbc9afd6caa to your computer and use it in GitHub Desktop.
Random interval observable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Rx = require('rx'); | |
var _ = require('lodash'); | |
module.exports = function(minSeconds, maxSeconds) { | |
if (minSeconds >= maxSeconds) { | |
throw new Error('min needs to be less than max'); | |
} | |
var getDueTime = () => _.random(minSeconds, maxSeconds) * 1000; | |
return new Rx.AnonymousObservable((observer) => { | |
return Rx.Scheduler.timeout.scheduleRecursiveWithRelative( | |
getDueTime(), | |
(self) => { | |
observer.onNext(); | |
self(getDueTime()); | |
} | |
) | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment