Commiting to continue stuff at home master
authorCameron Ball <c.ball1729@gmail.com>
Fri, 1 May 2015 09:05:06 +0000 (17:05 +0800)
committerCameron Ball <c.ball1729@gmail.com>
Fri, 1 May 2015 09:05:06 +0000 (17:05 +0800)
Overlay.as
PollableBaseRegistry.as
RegistryManager.as [new file with mode: 0644]
Util.as

index 7c19c93..ea3f1c3 100644 (file)
@@ -5,7 +5,7 @@
 
        public class Overlay extends flash.display.MovieClip {
 
-               private var registry:Registry;
+               private var registry:RegistryManager;
                
                /*
                 * After doing some science, 50ms seems to be roughly the time it takes
                private const REFRESH_RATE = 10;
 
                public function Overlay() {
-                       this.registry = new Registry(REFRESH_RATE);
+                       this.registry = new RegistryManager(REFRESH_RATE);
                        var confParser:ConfigParserRegistry = new ConfigParserRegistry("StreamInfo.txt");
                        var confParser2:ConfigParserRegistry = new ConfigParserRegistry("StreamInfo2.txt");
                        
-                       this.registry.addSubRegistry(confParser);
-                       this.registry.addSubRegistry(confParser2);
+                       this.registry.addRegistry(confParser);
+                       this.registry.addRegistry(confParser2);
                                                                                                
-                       //this.registry.addEventListener(confParser.getUpdateEventType(), this.updated);
+                       this.registry.addEventListener(registry.getUpdateEventType(), this.updated);
                }
                
                private function updated(e:Event) {
                        trace("updated");
+                       trace(this.registry.getLastUpdated());
                        //var cam:Object = this.conf.get('Cam');
                        //trace(cam.MusicBoxBase);
                }
index 7966c2b..a83bc58 100644 (file)
@@ -12,7 +12,7 @@
 
                public function PollableBaseRegistry(pollFunction:Function = null) {
                        this.dispatcher = new EventDispatcher();
-                       this.pollFunction = pollFunction;
+                       this.pollFunction = pollFunction || this.commit;
                }
                
                public function getPollableFunction():Function {
                public function willTrigger(type:String):Boolean {
                        return this.dispatcher.willTrigger(type);
                }
+               
+               public function whoAmI() {
+                       trace("poll base");
+               }
 
        }
        
diff --git a/RegistryManager.as b/RegistryManager.as
new file mode 100644 (file)
index 0000000..98e04ee
--- /dev/null
@@ -0,0 +1,98 @@
+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");
+               }
+       }
+       
+}
diff --git a/Util.as b/Util.as
index b4074b6..e095bca 100644 (file)
--- a/Util.as
+++ b/Util.as
                        
                        return true;
                }
+               
+               public static function arrayConcatUnique(arr1:Array, arr2:Array):Array  {
+                       for each (var value:* in arr2) {
+                               if (arr1.indexOf(value) == -1)
+                                       arr1.push(value);
+                       }
+                       
+                       return arr1;
+               }
 
        }