Commiting to continue stuff at home
[swf-overlay.git] / RegistryManager.as
1 package {
2 import flash.events.Event
3 import flash.utils.*
4 import flash.utils.getQualifiedClassName
5
6 public class RegistryManager extends PollableBaseRegistry {
7
8 // This object will keep track of which registry last updated which
9 // values. This way there is no need to copy across values. We just check
10 // who had it last and ask her for it.
11 private var valueMap:Object;
12 private var registries:Array;
13 private var numRegistriesPolled:int;
14 private var pollRate:int;
15
16 public function RegistryManager(pollRate:int) {
17 this.registries = [];
18 this.valueMap = {};
19 this.numRegistriesPolled = 0;
20 this.pollRate = pollRate;
21
22 super.whoAmI();
23
24 var test = super;
25 test.whoAmI();
26
27 // INCEPTION INTENSIFIES
28 //this.addRegistry(super);
29
30 //setInterval(this.poll, this.pollRate);
31 }
32
33 public function addRegistry(registry:IPollableRegistry, registryCallback:Function = null) {
34 this.registries.push(registry);
35 registry.addEventListener(registry.getUpdateEventType(), this.updated);
36 registry.addEventListener(registry.getCycleCompleteEventType(), this.cycleComplete);
37 }
38
39 public override function get(propChain:String):* {
40 trace('should not see this');
41 return (this.valueMap[propChain] && this.valueMap[propChain].get(propChain));
42 }
43
44 public override function getLastUpdated():Array {
45 var updatedValues = [];
46 for (var propChain in this.valueMap) {
47 trace(this.valueMap[propChain].whoAmI());
48 // HMMMM...
49 // if we are looking at ourselves AND we have updated values, OR we are looking at something else and it has updated values
50 if(this.valueMap[propChain].getLastUpdated().indexOf(propChain) >= 0)
51 updatedValues.push(propChain)
52 }
53
54 return updatedValues;
55 }
56
57 private function poll() {
58 // This acts as a lock. After all the registries are polled, it resets to 0
59 // so we know it's safe to poll again.
60 if(this.numRegistriesPolled === 0) {
61 for each(var registry:Object in this.registries) {
62 this.numRegistriesPolled++;
63 var fn:Function = registry.getPollableFunction();
64 fn();
65 }
66 }
67 }
68
69 // Fires when _any_ of our registries are updated
70 private function updated(e:PollEvent) {
71 var update = false;
72
73 for each(var propChain in e.pollTarget.getLastUpdated()) {
74 this.valueMap[propChain] = e.pollTarget;
75 update = true;
76 }
77
78 //for each(var propChain in e.pollTarget.getLastUpdated()) {
79 // update = true;
80 // this.valueMap[propChain] = e.pollTarget;
81 //}
82
83 update && this.dispatchEvent(new PollEvent(this.getUpdateEventType(), this));
84 }
85
86 private function cycleComplete(e:PollEvent) {
87 if(this.numRegistriesPolled == this.registries.length) {
88 this.numRegistriesPolled = 0;
89 }
90
91 }
92
93 public override function whoAmI() {
94 trace("regman");
95 }
96 }
97
98 }