Fixes issue #220

This commit is contained in:
walterhiggins 2015-03-01 13:52:23 +00:00
parent 16c3c6b01f
commit cdc2a2a11b

View file

@ -47,7 +47,15 @@ at('06:00', wakeup, null, false);
```
***/
var SECOND = 1000;
var POLLING_INTERVAL = 3 * SECOND; // this is probably precise enough
function at(time24hr, callback, pWorlds, repeat) {
if (arguments.length === 0){
// TODO: Document this behaviour
console.log(tasksToString());
return;
}
var timeParts = time24hr.split( ':' );
var timeMins = (timeParts[0] * 60) + (timeParts[1] * 1);
if (!pWorlds || pWorlds === undefined ) {
@ -61,6 +69,21 @@ function at(time24hr, callback, pWorlds, repeat) {
});
};
var atTasks = {};
function tasksToString(){
var result = '';
for (var world in atTasks){
result += 'world: ' + world +'\n';
for (var time in atTasks[world]){
var scheduledFuncs = atTasks[world][time];
for (var i = 0;i < scheduledFuncs.length; i++){
result += ' ' + time + ': ' + scheduledFuncs[i].constructor + '\n';
}
}
result += '(current world time: ' + utils.time24(world) + ')\n';
}
return result;
}
/*
constructs a function which will be called every x ticks to
track the schedule for a given world
@ -68,19 +91,26 @@ var atTasks = {};
function atMonitorFactory(world){
var worldName = ''+ world.name;
var lastRun = null;
return function atMonitorForWorld(){
var timeMins = utils.time24(world);
if (timeMins === lastRun){
return;
}
lastRun = timeMins;
if (lastRun === null ){
lastRun = timeMins - 1;
}else {
lastRun = lastRun % 1440;
}
var worldSchedule = atTasks[worldName];
if (!worldSchedule){
return;
}
var tasks = worldSchedule[timeMins];
while ( lastRun > timeMins ? (lastRun <= 1440) : ( lastRun < timeMins ) ){
var tasks = worldSchedule[lastRun++];
if (!tasks){
return;
continue;
}
utils.foreach(tasks, function(task, i){
if (!task){
@ -91,6 +121,7 @@ function atMonitorFactory(world){
tasks[i] = null;
}
});
}
};
}
function atAddTask( timeMins, callback, world, repeat){
@ -105,7 +136,7 @@ function atAddTask( timeMins, callback, world, repeat){
}
var atMonitors = [];
function onLoadStartMonitor(event){
var monitor = setInterval( atMonitorFactory(event.world), 900);
var monitor = setInterval( atMonitorFactory(event.world), POLLING_INTERVAL);
atMonitors.push( monitor );
}
if (__plugin.canary){