Skip to content
Browse files

+ sandman js scheduler

  • Loading branch information...
1 parent f6ea5a7 commit 9b7ee0aa47e694a680320e81b138ff0663fd6775 @floere committed Jun 17, 2009
Showing with 67 additions and 0 deletions.
  1. +67 −0 sandman-scheduler.js
View
67 sandman-scheduler.js
@@ -0,0 +1,67 @@
+// Sandman schedules sleeping callbacks.
+//
+function Sandman() {
+
+ // Saves the callbacks in the form:
+ // remaining_time => [callback, callback, …]
+ //
+ var schedule = {};
+
+ // Saves the callback to be executed in duration ticks.
+ //
+ // Sandman.sleep(10, function() {
+ // // code to be executed in 10 ticks.
+ // });
+ //
+ this.sleep = function(duration, callback) {
+ var callbacks = schedule[duration];
+ if (callbacks) {
+ // we already have an array of callbacks at that remaining time, append.
+ callbacks[callbacks.length] = callback;
+ } else {
+ // we do not already have an array of callbacks. Thus initialize one.
+ schedule[duration] = [callback];
+ };
+ };
+
+ // Advances one tick.
+ // If there are callbacks with zero time remaining, call them all.
+ //
+ // Sandman.tick();
+ //
+ // Note: Only use this method directly if you control the "main loop", i.e. in games.
+ //
+ this.tick = function() {
+ var newSchedule = {};
+ for (var key in schedule.items) {
+ if (key == 0) {
+ for(var callback in schedule.items[0]) {
+ // execute the callback
+ callback();
+ }
+ } else {
+ // reduce the key by one
+ newSchedule[key-1] = schedule.items[key];
+ };
+ }
+ schedule = newSchedule;
+ };
+
+ // Wake the Sandman, let him sprinkle sand.
+ // Starts the scheduling process.
+ //
+ // Note: This only works in a browser with setTimeout.
+ //
+ this.sand = function() {
+
+ };
+
+ // Let the Sandman vanish.
+ //
+ // Note: This only works in a browser with setTimeout.
+ //
+ this.vanish = function() {
+
+ };
+
+}

0 comments on commit 9b7ee0a

Please sign in to comment.
Something went wrong with that request. Please try again.