Last active
September 7, 2015 11:24
-
-
Save JaminFarr/654c4ea4acd441975f76 to your computer and use it in GitHub Desktop.
This file contains 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') | |
/** | |
* RxFixedInterval | |
* | |
* Returns an observable sequence that produces a value after each period. | |
* | |
* The difference between FixedInterval and the standard Rx interval is each value is produced when the time | |
* is divisable by the interval. | |
* | |
* Example: If the interval is 1 hour the next value will be produced at the start of the next hour (00 minutes) | |
* not at 1 hours time from the starting time | |
* | |
* To show the difference here is how a Rx.Observable.Interval and a FixedInterval will behave, both with a 10 second interval and starting at 10:07:12. | |
* | |
* count Normal Rx interval FixedInterval | |
* 1 10:07:22 10:07:20 | |
* 2 10:07:32 10:07:30 | |
* 3 10:07:42 10:07:40 | |
* 4 10:07:52 10:07:50 | |
*/ | |
function RxFixedInterval(interval, offset, scheduler) { | |
offset = offset || 0 | |
var now = Date.now() | |
var firstTick = (now + interval) - (now - offset) % interval | |
return Rx.Observable.timer(new Date(firstTick), interval, scheduler) | |
.map(function(count) { | |
return { | |
count: count, | |
time: new Date(firstTick + (count * interval) ) | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment