Fixes issue #220
This commit is contained in:
parent
16c3c6b01f
commit
cdc2a2a11b
1 changed files with 45 additions and 14 deletions
|
@ -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) {
|
function at(time24hr, callback, pWorlds, repeat) {
|
||||||
|
if (arguments.length === 0){
|
||||||
|
// TODO: Document this behaviour
|
||||||
|
console.log(tasksToString());
|
||||||
|
return;
|
||||||
|
}
|
||||||
var timeParts = time24hr.split( ':' );
|
var timeParts = time24hr.split( ':' );
|
||||||
var timeMins = (timeParts[0] * 60) + (timeParts[1] * 1);
|
var timeMins = (timeParts[0] * 60) + (timeParts[1] * 1);
|
||||||
if (!pWorlds || pWorlds === undefined ) {
|
if (!pWorlds || pWorlds === undefined ) {
|
||||||
|
@ -61,6 +69,21 @@ function at(time24hr, callback, pWorlds, repeat) {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
var atTasks = {};
|
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
|
constructs a function which will be called every x ticks to
|
||||||
track the schedule for a given world
|
track the schedule for a given world
|
||||||
|
@ -68,29 +91,37 @@ var atTasks = {};
|
||||||
function atMonitorFactory(world){
|
function atMonitorFactory(world){
|
||||||
var worldName = ''+ world.name;
|
var worldName = ''+ world.name;
|
||||||
var lastRun = null;
|
var lastRun = null;
|
||||||
|
|
||||||
return function atMonitorForWorld(){
|
return function atMonitorForWorld(){
|
||||||
var timeMins = utils.time24(world);
|
var timeMins = utils.time24(world);
|
||||||
if (timeMins === lastRun){
|
if (timeMins === lastRun){
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
lastRun = timeMins;
|
if (lastRun === null ){
|
||||||
|
lastRun = timeMins - 1;
|
||||||
|
}else {
|
||||||
|
lastRun = lastRun % 1440;
|
||||||
|
}
|
||||||
var worldSchedule = atTasks[worldName];
|
var worldSchedule = atTasks[worldName];
|
||||||
if (!worldSchedule){
|
if (!worldSchedule){
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var tasks = worldSchedule[timeMins];
|
while ( lastRun > timeMins ? (lastRun <= 1440) : ( lastRun < timeMins ) ){
|
||||||
if (!tasks){
|
|
||||||
return;
|
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){
|
function atAddTask( timeMins, callback, world, repeat){
|
||||||
|
@ -105,7 +136,7 @@ function atAddTask( timeMins, callback, world, repeat){
|
||||||
}
|
}
|
||||||
var atMonitors = [];
|
var atMonitors = [];
|
||||||
function onLoadStartMonitor(event){
|
function onLoadStartMonitor(event){
|
||||||
var monitor = setInterval( atMonitorFactory(event.world), 900);
|
var monitor = setInterval( atMonitorFactory(event.world), POLLING_INTERVAL);
|
||||||
atMonitors.push( monitor );
|
atMonitors.push( monitor );
|
||||||
}
|
}
|
||||||
if (__plugin.canary){
|
if (__plugin.canary){
|
||||||
|
|
Reference in a new issue