Skip to content

Instantly share code, notes, and snippets.

@jubishop
Created July 22, 2008 21:47
Show Gist options
  • Save jubishop/1314 to your computer and use it in GitHub Desktop.
Save jubishop/1314 to your computer and use it in GitHub Desktop.
package flash.events {
public class EventDispatcher {
// This would actually dispatch event to captured/uncaptured listeners...
// Assume this and the rest of event dispatcher behave as you'd expect :)
private function _dispatch(event, captured:Boolean = false):void {
var listener:Function;
event.currentTarget = this;
if (captured) for each (listener in capturedListeners[event.type]) {
if (event.stoppedImmediatePropagation) break;
listener(event);
}
else for each (listener in uncapturedListeners[event.type]) {
if (event.stoppedImmediatePropagation) break;
listener(event);
}
}
// event.eventPhase is CAPTURING at construction
// This demonstrates the flow of capturing/target/bubbling
public function dispatchEvent(event:Event):void {
if (event.target == null) event.target = this;
if (event.eventPhase == EventPhase.BUBBLING_PHASE) {
_dispatch(event);
if (parent) parent.dispatchEvent(event);
return;
}
if (event.eventPhase == EventPhase.CAPTURING_PHASE) {
if (parent) parent.dispatchEvent(event);
if (event.target != this) {
_dispatch(event, true);
return;
}
}
if (event.target == this) {
if (event.stoppedPropagation) return;
event.eventPhase = EventPhase.AT_TARGET;
_dispatch(event);
if (event.stoppedPropagation) return;
if (event.bubbles) {
event.eventPhase = EventPhase.BUBBLING_PHASE;
if (parent) parent.dispatchEvent(event);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment