Taking this way too seriously.
authorCameron Ball <c.ball1729@gmail.com>
Fri, 24 Apr 2015 09:00:14 +0000 (17:00 +0800)
committerCameron Ball <c.ball1729@gmail.com>
Fri, 24 Apr 2015 09:00:14 +0000 (17:00 +0800)
.gitignore
BaseRegistry.as
ConfigParserRegistry.as
IPollable.as [new file with mode: 0644]
IPollableRegistry.as [new file with mode: 0644]
IRegistry.as
Overlay.as
PollEvent.as [new file with mode: 0644]
PollableBaseRegistry.as [new file with mode: 0644]
Registry.as

index d8c3ba5..f3a1112 100644 (file)
@@ -1,4 +1,5 @@
 *.fla
 *.swf
 StreamInfo.txt
+StreamInfo2.txt
 audio
\ No newline at end of file
index 07a3cac..4e3c65d 100644 (file)
@@ -1,15 +1,14 @@
 package  {
+       
        import flash.events.Event;
        import flash.events.EventDispatcher;
        
-       public class BaseRegistry extends EventDispatcher implements IRegistry  {
+       public class BaseRegistry implements IRegistry  {
 
-               private var stage:Object;
-               private var head:Object;
-               private var lastUpdated:Array;
+               protected var stage:Object;
+               protected var head:Object;
+               protected var lastUpdated:Array;
                
-               public static const REGISTRY_UPDATE = "registry_update";
-
                public function BaseRegistry()  {
                        this.stage = {};
                        this.head = {};
@@ -25,7 +24,6 @@
                                this.head = Util.extend(this.head, this.stage);
                                this.lastUpdated = Util.toPropertyChains(this.stage);
                                this.stage = {};
-                               this.dispatchEvent(new Event(REGISTRY_UPDATE));
                        }
                }
                
index a30f486..649899e 100644 (file)
@@ -1,16 +1,16 @@
 package  {
-       import flash.events.Event;
-       import flash.events.EventDispatcher;
        import flash.net.*
+       import flash.events.Event;
+       import flash.events.IOErrorEvent;
        
-       public class ConfigParserRegistry extends BaseRegistry {
+       public class ConfigParserRegistry extends PollableBaseRegistry {
                private var request:URLRequest;
                private var loader:URLLoader;
                private var fileLock:Boolean;
                private var loadCallback:Function;
 
-
                public function ConfigParserRegistry(fileName:String) {
+                       super(this.parse);
                        this.fileLock = false;
                        this.request = new URLRequest(fileName);
                        this.loader = new URLLoader();
@@ -21,6 +21,7 @@
                                this.loadCallback = this.onLoaded(this);
                                
                                loader.addEventListener(Event.COMPLETE, this.loadCallback);
+                               loader.addEventListener(IOErrorEvent.IO_ERROR, this.ioError);
                                this.fileLock = true;
                                this.loader.load(this.request);
                        } else {
                private function releaseLock() {
                        this.fileLock = false;
                }
-                               
+               
+               // Sometimes I think this acesses the file at the same time it is being saved
+               // which causes havock. In that case I just release the lock and clear everything.
+               private function ioError(e:Event) {
+                       this.loader.removeEventListener(Event.COMPLETE, this.loadCallback);
+                       this.loader.removeEventListener(IOErrorEvent.IO_ERROR, this.ioError);
+                       this.releaseLock();
+               }
+               
                private function onLoaded(configParser:ConfigParserRegistry):Function {         
                        return function(e:Event):void {
                                //Deal with windows style line endings, and tabs.
@@ -39,6 +48,7 @@
                                configParser.parseVars(lines);
                                configParser.releaseLock();
                                configParser.loader.removeEventListener(Event.COMPLETE, configParser.loadCallback);
+                               configParser.loader.removeEventListener(IOErrorEvent.IO_ERROR, configParser.ioError);
                        }
                }
                
diff --git a/IPollable.as b/IPollable.as
new file mode 100644 (file)
index 0000000..eaf7e15
--- /dev/null
@@ -0,0 +1,13 @@
+package  {
+       import flash.events.IEventDispatcher;
+       
+       public interface IPollable extends IEventDispatcher {
+
+               function getPollableFunction():Function;
+               function dispatchUpdateEvent():void;
+               function getUpdateEventType():String;
+               function dispatchCycleCompleteEvent():void;
+               function getCycleCompleteEventType():String;
+       }
+       
+}
diff --git a/IPollableRegistry.as b/IPollableRegistry.as
new file mode 100644 (file)
index 0000000..8cbacba
--- /dev/null
@@ -0,0 +1,9 @@
+package  {
+       
+       public interface IPollableRegistry extends IRegistry, IPollable {
+
+               // I Dunno LOL ¯\(°_o)/¯
+
+       }
+       
+}
index 1a4e4cc..cac496e 100644 (file)
@@ -1,7 +1,6 @@
 package  {
-       import flash.events.IEventDispatcher;
-       
-       public interface IRegistry extends IEventDispatcher
+
+       public interface IRegistry
        {
                function add(obj:Object):void;
                function commit():void;
index db74c99..7c19c93 100644 (file)
@@ -5,32 +5,29 @@
 
        public class Overlay extends flash.display.MovieClip {
 
-               private var conf:ConfigParserRegistry;
-               //private var musicBox:MusicBox;
+               private var registry:Registry;
                
                /*
                 * After doing some science, 50ms seems to be roughly the time it takes
                 * for a request to complete and for the locking to not happen.
                 */
-               private const REFRESH_RATE = 1000;
+               private const REFRESH_RATE = 10;
 
                public function Overlay() {
-                       this.conf = new ConfigParserRegistry("StreamInfo.txt");
-                       setInterval(this.conf.parse, REFRESH_RATE);
-                       this.conf.addEventListener(BaseRegistry.REGISTRY_UPDATE, this.updated);
+                       this.registry = new Registry(REFRESH_RATE);
+                       var confParser:ConfigParserRegistry = new ConfigParserRegistry("StreamInfo.txt");
+                       var confParser2:ConfigParserRegistry = new ConfigParserRegistry("StreamInfo2.txt");
                        
-                       
-                       //this.streamInfo = new StreamInfo("Cam", REFRESH_RATE);
-                       //this.musicBox = new MusicBox(this.streamInfo);
-                       
-                       //this.streamInfo.addEventListener(StreamInfo.INFO_UPDATE, this.streamInfoUpdated);
+                       this.registry.addSubRegistry(confParser);
+                       this.registry.addSubRegistry(confParser2);
+                                                                                               
+                       //this.registry.addEventListener(confParser.getUpdateEventType(), this.updated);
                }
                
                private function updated(e:Event) {
-                       var cam:Object = this.conf.get('Cam');
-                       trace(cam.MusicBoxBase);
+                       trace("updated");
+                       //var cam:Object = this.conf.get('Cam');
+                       //trace(cam.MusicBoxBase);
                }
-
        }
-       
 }
diff --git a/PollEvent.as b/PollEvent.as
new file mode 100644 (file)
index 0000000..a845fc7
--- /dev/null
@@ -0,0 +1,15 @@
+package  {
+       
+       import flash.events.Event;
+       
+       public class PollEvent extends Event {
+
+               public var pollTarget:Object;
+
+               public function PollEvent(type:String, target:Object) {
+                       super(type);
+                       this.pollTarget = target;
+               }
+       }
+       
+}
diff --git a/PollableBaseRegistry.as b/PollableBaseRegistry.as
new file mode 100644 (file)
index 0000000..7966c2b
--- /dev/null
@@ -0,0 +1,68 @@
+package  {
+       import flash.events.Event;
+       import flash.events.EventDispatcher;
+       
+       public class PollableBaseRegistry extends BaseRegistry implements IPollableRegistry {
+
+               private var dispatcher:EventDispatcher;
+               private var pollFunction:Function;
+               
+               private static const REGISTRY_UPDATE = "registry_update";
+               private static const CYCLE_COMPLETE = "cycle_complete";
+
+               public function PollableBaseRegistry(pollFunction:Function = null) {
+                       this.dispatcher = new EventDispatcher();
+                       this.pollFunction = pollFunction;
+               }
+               
+               public function getPollableFunction():Function {
+                       return this.pollFunction;
+               }
+               
+               public override function commit():void {
+                       if(!Util.objectIsEmpty(this.stage)) {
+                               this.head = Util.extend(this.head, this.stage);
+                               this.lastUpdated = Util.toPropertyChains(this.stage);
+                               this.stage = {};
+                               this.dispatchUpdateEvent();
+                       }
+                       
+                       this.dispatchCycleCompleteEvent();
+               }
+               
+               public function dispatchUpdateEvent():void {
+                       this.dispatchEvent(new PollEvent(this.getUpdateEventType(), this));
+               }
+               
+               public function getUpdateEventType():String {
+                       return REGISTRY_UPDATE;
+               }
+               
+               public function dispatchCycleCompleteEvent():void {
+                       this.dispatchEvent(new PollEvent(this.getCycleCompleteEventType(), this));
+               }
+               
+               public function getCycleCompleteEventType():String {
+                       return CYCLE_COMPLETE;
+               }
+               
+               //Wrappers around EventDispatcher
+               public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void {
+                       this.dispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference);
+               }
+               public function dispatchEvent(event:Event):Boolean {
+                       return this.dispatcher.dispatchEvent(event);
+               }
+               public function hasEventListener(type:String):Boolean {
+                       return this.dispatcher.hasEventListener(type);
+               }
+               public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void {
+                       this.dispatcher.removeEventListener(type, listener, useCapture);
+               }
+               public function willTrigger(type:String):Boolean {
+                       return this.dispatcher.willTrigger(type);
+               }
+
+       }
+       
+}
index b424963..288a2b6 100644 (file)
@@ -1,41 +1,50 @@
 package  {
+       import flash.events.Event;
+       import flash.utils.*;
+       import flash.text.engine.EastAsianJustifier;
                
-       import flash.events.EventDispatcher;
-       import flash.sampler.Sample;
-               
-       public class Registry implements IRegistry extends EventDispatcher {
-
-               private var stage:Object;
-               private var head:Object;
-               private var lastUpdated:Array;
-               
-               public static const REGISTRY_UPDATE = "registry_update";
-
-               public function Registry() {
-                       this.stage = {};
-                       this.head = {};
-               }
+       public class Registry extends PollableBaseRegistry {
+       
+               private var subRegistries:Array;
+               private var numRegistriesPolled:int;
+               private var refreshRate:int;
 
-               public function add(obj:Object):void {
-                       this.stage = Util.merge(this.stage, obj);
+               public function Registry(refreshRate:int) {
+                       this.subRegistries = [];
+                       this.numRegistriesPolled = 0;
+                       this.refreshRate = refreshRate;
+                       
+                       setInterval(this.poll, this.refreshRate);
                }
                
-               public function commit():void {
-                       this.head = Util.extend(this.head, this.stage);
-                       this.lastUpdated = Util.toPropertyChains(this.stage);
-                       this.stage = {};
+               public function addSubRegistry(subRegistry:IPollableRegistry, registryCallback:Function = null) {
+                       this.subRegistries.push(subRegistry);
+                       subRegistry.addEventListener(subRegistry.getUpdateEventType(), this.updated);
+                       subRegistry.addEventListener(subRegistry..getCycleCompleteEventType(), this.cycleComplete);
                }
                
-               public function get(propChain:String):* {
-                       return Util.resolvePropertyChain(propChain, this.head);
+               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 subRegistry:Object in this.subRegistries) {
+                                       this.numRegistriesPolled++;
+                                       var fn:Function = subRegistry.getPollableFunction();
+                                       fn();
+                               }
+                       }
                }
                
-               public function getLastUpdated():Array {
-                       return this.lastUpdated;
+               private function updated(e:PollEvent) {
+                       trace(e.pollTarget.getLastUpdated());
                }
                
-               public function get(member:String) {
-                       return Util.resolvePropertyChain(member, this.head);
+               private function cycleComplete(e:PollEvent) {
+                       if(this.numRegistriesPolled == this.subRegistries.length) {
+                               //trace("polling complete");
+                               this.numRegistriesPolled = 0;
+                       }
+                       
                }
        }