From cdc2a2a11b9bbcae1cf1dbef668dc636d7420799 Mon Sep 17 00:00:00 2001 From: walterhiggins Date: Sun, 1 Mar 2015 13:52:23 +0000 Subject: [PATCH] Fixes issue #220 --- src/main/js/modules/at.js | 59 +++++++++++++++++++++++++++++---------- 1 file changed, 45 insertions(+), 14 deletions(-) diff --git a/src/main/js/modules/at.js b/src/main/js/modules/at.js index 04851cc..03d3384 100644 --- a/src/main/js/modules/at.js +++ b/src/main/js/modules/at.js @@ -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){