class Monitor { private var functionQueue:Array; private var queueCopy:Array; private var watcher:Function; public var isReady:Boolean; public var name:String; function Monitor(name:String) { if (name == undefined) { name = "S"+Math.floor(Math.random()*10000000000); } trace ("You created a monitor called "+name); this.functionQueue = new Array(); this.queueCopy = new Array(); this.isReady = true; this.name = name; } function addFunction( fun_arg:Object ):Void { this.functionQueue.push(fun_arg); } function addFunctions( fun_arg_array:Array ):Void { this.functionQueue = this.functionQueue.concat(fun_arg_array); } function deleteFunctions():Void { this.functionQueue = new Array(); } function process():Void { this.watcher = function(prop, oldVal, newVal) { if (newVal == true) { callFunction(); } return newVal; } this.queueCopy = this.functionQueue.concat(); callFunction(); } private function callFunction() { this.unwatch("isReady"); if (this.queueCopy.length == 0) { this.isReady = true; trace (this.name+" has finished processing"); return; } this.isReady = false; var fun_arg:Object = this.queueCopy.shift(); if (fun_arg.args instanceof Array) { fun_arg.fun.apply(this, fun_arg.args); } else { fun_arg.fun.call(this, fun_arg.args); } this.watch("isReady", this.watcher); } function setReady(val:Boolean) { this.isReady = val; }; }