Dispatch goodness
authorCameron Ball <c.ball1729@gmail.com>
Fri, 17 Apr 2015 06:36:00 +0000 (14:36 +0800)
committerCameron Ball <c.ball1729@gmail.com>
Fri, 17 Apr 2015 06:36:00 +0000 (14:36 +0800)
.gitignore
Overlay.as
StreamInfo.as
StreamInfo.txt [deleted file]

index ea39644..95bd463 100644 (file)
@@ -1,2 +1,3 @@
 *.fla
-*.swf
\ No newline at end of file
+*.swf
+StreamInfo.txt
\ No newline at end of file
index 3e42a1f..41ed20b 100644 (file)
@@ -1,23 +1,31 @@
 package  {
        import flash.utils.*;
+       import flash.events.Event;
        import flash.display.MovieClip;
 
        public class Overlay extends flash.display.MovieClip {
 
+               private var streamInfo:StreamInfo;
+
                /*
                 * 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 = 50;
+               private const REFRESH_RATE = 500;
 
                public function Overlay() {
-                       var streamInfo:StreamInfo = new StreamInfo();
+                       this.streamInfo = new StreamInfo("Cam", REFRESH_RATE);
+                       this.streamInfo.addEventListener(StreamInfo.INFO_UPDATE, this.streamInfoUpdated);
                        
-                       setInterval(function() {
-                               streamInfo.parse("Cam", function() {
-                                       test.text = streamInfo.getStreamParameter("a");
-                               });
-                       },this.REFRESH_RATE);
+                       //setInterval(function() {
+                       //      streamInfo.parse("Cam", function() {
+                       //              test.text = streamInfo.getStreamParameter("MusicBoxBase");
+                       //      });
+                       //},this.REFRESH_RATE);
+               }
+               
+               private function streamInfoUpdated(e:Event) {
+                       trace(this.streamInfo.getLastUpdated());
                }
 
        }
index 2b61fe3..6ae479a 100644 (file)
@@ -1,28 +1,34 @@
 package  {
        import flash.events.Event;
+       import flash.events.EventDispatcher;
+       import flash.utils.setInterval;
        import flash.net.*
        
-       public class StreamInfo {
+       public class StreamInfo extends EventDispatcher {
                private var request:URLRequest;
                private var loader:URLLoader;
                private var streamInfo = {};
+               private var lastUpdated:Array;
+               private var section:String;
                private var fileLock = false;
-               private var callback:Function;
+               private var loadCallback:Function;
+               
+               public static const INFO_UPDATE = "info_update";
 
-               public function StreamInfo() {
+               public function StreamInfo(section:String, refreshRate:int) {
                        this.request = new URLRequest("file:///Z:/Overlay/StreamInfo.txt");
                        this.loader = new URLLoader();
+                       this.section = section;
+                       setInterval(this.parse, refreshRate);
                }
                
-               public function parse(section:String, callback:Function) {
+               public function parse() {
                        if(!this.fileLock) {
-                               this.callback = callback;
-                               var loadCallback:Function = this.onLoaded(section, this);
+                               this.loadCallback = this.onLoaded(this);
                                
-                               loader.addEventListener(Event.COMPLETE, loadCallback);
+                               loader.addEventListener(Event.COMPLETE, this.loadCallback);
                                this.fileLock = true;
                                this.loader.load(this.request);
-                               loader.removeEventListener(Event.COMPLETE, callback);
                        } else {
                                trace("Lock active, not doing anything.");
                        }
                        return this.streamInfo[name];
                }
                
+               public function getLastUpdated():Array {
+                       return this.lastUpdated;
+               }
+               
                private function releaseLock() {
                        this.fileLock = false;
                }
                                
-               private function onLoaded(section:String, streamInfo:StreamInfo):Function {             
+               private function onLoaded(streamInfo:StreamInfo):Function {             
                        return function(e:Event):void {
                                //Deal with windows style line endings, and tabs.
                                var lines = streamInfo.loader.data.replace(/(\t|\r)/gi, "").split("\n");
-                               var sectionLines:Array = streamInfo.extractSection(lines, section);
+                               var sectionLines:Array = streamInfo.extractSection(lines);
                                streamInfo.parseVars(sectionLines);
-                               streamInfo.callback();
                                streamInfo.releaseLock();
+                               streamInfo.loader.removeEventListener(Event.COMPLETE, streamInfo.loadCallback);
                        }
                }
                
-               private function extractSection(lines:Array, section:String) {
+               private function extractSection(lines:Array) {
                        var sectionLines:Array = new Array();
                        var foundFirstLineOfSection:Boolean = false;
                        for each(var line in lines) {
-                               foundFirstLineOfSection = !foundFirstLineOfSection ? line.indexOf("[" + section + "]") >= 0 : !line.match(/\[\w+\]/);
+                               foundFirstLineOfSection = !foundFirstLineOfSection ? line.indexOf("[" + this.section + "]") >= 0 : !line.match(/\[\w+\]/);
                                
                                if(foundFirstLineOfSection) {
                                        sectionLines.push(line);
                }
                
                private function parseVars(lines:Array) {
+                       var broadcastUpdate = false;
                        for each(var line in lines) {
                                var keyValue = line.split("=", 2);
                                if(keyValue.length == 2) {
-                                       this.streamInfo[keyValue[0]] = keyValue[1];
+                                       if(this.streamInfo[keyValue[0]] != keyValue[1]) {
+                                               if(!broadcastUpdate) {
+                                                       this.lastUpdated = new Array();
+                                               }
+
+                                               broadcastUpdate = true;
+                                                                                               
+                                               this.lastUpdated.push(keyValue[0]);
+                                               this.streamInfo[keyValue[0]] = keyValue[1];
+                                       }
                                }
                        }
+                       
+                       if(broadcastUpdate) {
+                               this.dispatchEvent(new Event(INFO_UPDATE));
+                       }
                }
        }
 }
diff --git a/StreamInfo.txt b/StreamInfo.txt
deleted file mode 100644 (file)
index c1aeb2f..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-[Leeroy]
-Fang=It
-Yea=Boi
-
-[Cam]
-a=memesss
-c=d
\ No newline at end of file