forked from flightxd/flight-framework
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding option to be observable by interface vs utility.
- Loading branch information
Jacob Wright
committed
Feb 15, 2010
1 parent
51c0015
commit a3e843f
Showing
7 changed files
with
192 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package flight.observers | ||
{ | ||
import flash.events.IEventDispatcher; | ||
|
||
public interface IObservable | ||
{ | ||
function addCheck(property:String, observerHost:IEventDispatcher, observer:Function):void; | ||
function removeCheck(property:String, observer:Function):void; | ||
|
||
function addHook(property:String, observerHost:IEventDispatcher, observer:Function):void; | ||
function removeHook(property:String, observer:Function):void; | ||
|
||
function addObserver(property:String, observerHost:IEventDispatcher, observer:Function):void; | ||
function removeObserver(property:String, observer:Function):void; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package flight.observers | ||
{ | ||
import flash.events.EventDispatcher; | ||
import flash.events.IEventDispatcher; | ||
import flash.utils.Dictionary; | ||
|
||
public class Observable extends EventDispatcher implements IObservable | ||
{ | ||
private var target:IObservable; | ||
private var properties:Object = {}; | ||
|
||
public function Observable(target:IObservable = null) | ||
{ | ||
this.target = target || this; | ||
} | ||
|
||
public function addCheck(property:String, observerHost:IEventDispatcher, observer:Function):void | ||
{ | ||
addFunction(Observe.CHECK, property, observerHost, observer); | ||
} | ||
|
||
public function removeCheck(property:String, observer:Function):void | ||
{ | ||
removeFunction(property, observer); | ||
} | ||
|
||
public function addHook(property:String, observerHost:IEventDispatcher, observer:Function):void | ||
{ | ||
addFunction(Observe.HOOK, property, observerHost, observer); | ||
} | ||
|
||
public function removeHook(property:String, observer:Function):void | ||
{ | ||
removeFunction(property, observer); | ||
} | ||
|
||
public function addObserver(property:String, observerHost:IEventDispatcher, observer:Function):void | ||
{ | ||
addFunction(Observe.OBSERVE, property, observerHost, observer); | ||
} | ||
|
||
public function removeObserver(property:String, observer:Function):void | ||
{ | ||
removeFunction(property, observer); | ||
} | ||
|
||
public function canChange(property:String, oldValue:*, newValue:*):Boolean | ||
{ | ||
var check:Boolean = true; | ||
|
||
var checks:Dictionary = properties[property]; | ||
if (checks) { | ||
check = runFunctions(Observe.CHECK, checks, property, oldValue, newValue); | ||
if (!check) return false; | ||
} | ||
|
||
checks = properties["*"]; | ||
if (checks) { | ||
check = runFunctions(Observe.CHECK, checks, property, oldValue, newValue); | ||
if (!check) return false; | ||
} | ||
|
||
var typeChecks:Array = Observe.findTypeObservers(target, property); | ||
for each (checks in typeChecks) { | ||
check = runFunctions(Observe.CHECK, checks, property, oldValue, newValue); | ||
if (!check) return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public function modifyChange(property:String, oldValue:*, newValue:*):* | ||
{ | ||
var hooks:Dictionary = properties[property]; | ||
if (hooks) { | ||
newValue = runFunctions(Observe.HOOK, hooks, property, oldValue, newValue); | ||
} | ||
|
||
hooks = properties["*"]; | ||
if (hooks) { | ||
newValue = runFunctions(Observe.HOOK, hooks, property, oldValue, newValue); | ||
} | ||
|
||
var typeHooks:Array = Observe.findTypeObservers(target, property); | ||
for each (hooks in typeHooks) { | ||
newValue = runFunctions(Observe.HOOK, hooks, property, oldValue, newValue); | ||
} | ||
} | ||
|
||
public function notifyChange(property:String, oldValue:*, newValue:*):void | ||
{ | ||
var observers:Dictionary = properties[property]; | ||
if (observers) { | ||
runFunctions(Observe.OBSERVE, observers, property, oldValue, newValue); | ||
} | ||
|
||
observers = properties["*"]; | ||
if (observers) { | ||
runFunctions(Observe.OBSERVE, observers, property, oldValue, newValue); | ||
} | ||
|
||
var typeObservers:Array = Observe.findTypeObservers(target, property); | ||
for each (observers in typeObservers) { | ||
runFunctions(Observe.OBSERVE, observers, property, oldValue, newValue); | ||
} | ||
} | ||
|
||
private function addFunction(type:String, property:String, observerHost:IEventDispatcher, observer:Function):void | ||
{ | ||
var funcs:Dictionary = properties[property]; | ||
if (!funcs) { | ||
properties[property] = funcs = new Dictionary(true); | ||
} | ||
|
||
funcs[observer] = type; | ||
if (observerHost != null) { | ||
observerHost.addEventListener("_", observer); | ||
} | ||
} | ||
|
||
private function removeFunction(property:String, observer:Function):void | ||
{ | ||
var observers:Dictionary = properties[property]; | ||
if (!observers) return; | ||
|
||
delete observers[observer]; | ||
} | ||
|
||
private function runFunctions(type:String, funcs:Dictionary, property:String, oldValue:*, newValue:*):* | ||
{ | ||
var result:*; | ||
for (var i:Object in funcs) { | ||
if (funcs[i] != type) continue; | ||
var func:Function = i as Function; | ||
if (func.length == 0) { | ||
result = func.call(); | ||
} else if (func.length == 1) { | ||
result = func.call(null, newValue); | ||
} else if (func.length == 2) { | ||
result = func.call(null, oldValue, newValue); | ||
} else if (func.length == 3) { | ||
result = func.call(null, property, oldValue, newValue); | ||
} else { | ||
result = func.call(null, target, property, oldValue, newValue); | ||
} | ||
if (type == Observe.CHECK && result == false) return false; | ||
if (type == Observe.HOOK) newValue = result; | ||
} | ||
return newValue; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters