Fixes issue #220

This commit is contained in:
walterhiggins 2015-03-01 13:52:23 +00:00
parent 16c3c6b01f
commit cdc2a2a11b
1 changed files with 45 additions and 14 deletions

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,29 +91,37 @@ 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];
if (!tasks){
return;
while ( lastRun > timeMins ? (lastRun <= 1440) : ( lastRun < timeMins ) ){
var tasks = worldSchedule[lastRun++];
if (!tasks){
continue;
}
utils.foreach(tasks, function(task, i){
if (!task){
return;
}
setTimeout(task.callback.bind(null, timeMins, world), 1);
if (!task.repeat){
tasks[i] = null;
}
});
}
utils.foreach(tasks, function(task, i){
if (!task){
return;
}
setTimeout(task.callback.bind(null, timeMins, world), 1);
if (!task.repeat){
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){