package { import flash.events.Event import flash.utils.* import flash.utils.getQualifiedClassName public class RegistryManager extends PollableBaseRegistry { // This object will keep track of which registry last updated which // values. This way there is no need to copy across values. We just check // who had it last and ask her for it. private var valueMap:Object; private var registries:Array; private var numRegistriesPolled:int; private var pollRate:int; public function RegistryManager(pollRate:int) { this.registries = []; this.valueMap = {}; this.numRegistriesPolled = 0; this.pollRate = pollRate; super.whoAmI(); var test = super; test.whoAmI(); // INCEPTION INTENSIFIES //this.addRegistry(super); //setInterval(this.poll, this.pollRate); } public function addRegistry(registry:IPollableRegistry, registryCallback:Function = null) { this.registries.push(registry); registry.addEventListener(registry.getUpdateEventType(), this.updated); registry.addEventListener(registry.getCycleCompleteEventType(), this.cycleComplete); } public override function get(propChain:String):* { trace('should not see this'); return (this.valueMap[propChain] && this.valueMap[propChain].get(propChain)); } public override function getLastUpdated():Array { var updatedValues = []; for (var propChain in this.valueMap) { trace(this.valueMap[propChain].whoAmI()); // HMMMM... // if we are looking at ourselves AND we have updated values, OR we are looking at something else and it has updated values if(this.valueMap[propChain].getLastUpdated().indexOf(propChain) >= 0) updatedValues.push(propChain) } return updatedValues; } private function poll() { // This acts as a lock. After all the registries are polled, it resets to 0 // so we know it's safe to poll again. if(this.numRegistriesPolled === 0) { for each(var registry:Object in this.registries) { this.numRegistriesPolled++; var fn:Function = registry.getPollableFunction(); fn(); } } } // Fires when _any_ of our registries are updated private function updated(e:PollEvent) { var update = false; for each(var propChain in e.pollTarget.getLastUpdated()) { this.valueMap[propChain] = e.pollTarget; update = true; } //for each(var propChain in e.pollTarget.getLastUpdated()) { // update = true; // this.valueMap[propChain] = e.pollTarget; //} update && this.dispatchEvent(new PollEvent(this.getUpdateEventType(), this)); } private function cycleComplete(e:PollEvent) { if(this.numRegistriesPolled == this.registries.length) { this.numRegistriesPolled = 0; } } public override function whoAmI() { trace("regman"); } } }