EventCurb.as , removeAllListeners(object)

In AS3 we can check if an object has listeners, as in:

1
if(object.hasEventListener( type))

. But there is no method for getting all the listeners of an object. Or more importantly for removing all of them. Doh.

The hackish approach to this is to subclass EventDispatcher and write some short code that keeps a reference to each listener.

Which is what my class EventCurb does. – I should say it’s written overnight so anyone that likes can go ahead and try break their application…

Usage is as Singleton:

1
var _ev: EventCurb = EventCurb.getInstance();

To add listener:

1
2
_ev.addListener(obj, MouseEvent.MOUSE_DOWN, someFunc);
_ev.addListener(obj, MouseEvent.MOUSE_UP, someFunc);

If you try to add the same listener twice you will get a trace telling you that listener is already added, and no addition occurs.

To get all listeners of an object:

1
_ev.getListeners(obj);

This returns an Array of all listeners assigned to object which can be traversed as arr[index].type, arr[index].listener.

To remove listener of object:

1
2
_ev.removeListener(obj, MouseEvent.MOUSE_DOWN, someFunc);
_ev.removeListener(obj, MouseEvent.MOUSE_UP, someFunc);

And finally to remove All listeners of an object:

1
_ev.removeAllListeners(obj);

You can copy the class from below or download it from here. Feel free to rewrite or improve upon as you so wish.

EventCurb.as

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package
{
import flash.events.EventDispatcher;
import flash.utils.Dictionary;
/**
* ...
* @author Thomas James Thorstensson
* @version 1.0.1
*/

public class EventCurb extends EventDispatcher
{
private static var instance:EventCurb= new EventCurb();
private var objDict:Dictionary = new Dictionary(true);
private var _listener:Function;
private var objArr:Array;
private var obj:Object;

public function EventCurb() {
if( instance ) throw new Error( "Singleton and can only be accessed through Singleton.getInstance()" );
}

public static function getInstance():EventCurb {
return instance;
}

override public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
{
super.addEventListener(type, listener, useCapture, priority, useWeakReference);
}

override public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void
{
super.removeEventListener(type, listener, useCapture);
}

public function addListener(o:EventDispatcher, type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void {
// the object as key for an array of its event types
if (objDict[o] == null) objArr = objDict[o] = [];
for (var i:int = 0; i < objArr.length; i++) {
if ( objArr[i].type == type)
trace ("_______object already has this listener not adding!")
return
}
obj = { type:type, listener:listener }
objArr.push(obj);
o.addEventListener(type, listener, useCapture, priority, useWeakReference);
}

public function removeListener(o:EventDispatcher, type:String, listener:Function, useCapture:Boolean = false):void {
// if the object has listeners (ie exists in dictionary)
if (objDict[o] as Array !== null) {
var tmpArr:Array = [];
tmpArr = objDict[o] as Array;
for (var i:int = 0; i < tmpArr.length; i++) {
if (tmpArr[i].type == type) objArr.splice(i);
}

o.removeEventListener(type, listener, useCapture);
if (tmpArr.length == 0) {
delete objDict[o]
}
}else {
trace("_______object has no listeners");
}
}

/**
* If object has listeners, returns an Array which can be accessed
* as array[index].type,array[index].listeners
* @param o
* @return Array
*/

public function getListeners(o:EventDispatcher):Array{
if (objDict[o] as Array !== null) {
var tmpArr:Array = [];
tmpArr = objDict[o] as Array;
// forget trying to trace out the function name we use the function literal...
for (var i:int = 0; i < tmpArr.length; i++) {
trace("_______object " + o + " has event types: " + tmpArr[i].type +" with listener: " + tmpArr[i].listener);
}
return tmpArr

}else {
trace("_______object has no listeners");
return null
}

}

public function removeAllListeners(o:EventDispatcher, cap:Boolean = false):void {
if (objDict[o] as Array !== null) {
var tmpArr:Array = [];
tmpArr = objDict[o] as Array;
for (var i:int = 0; i < tmpArr.length; i++) {
o.removeEventListener(tmpArr[i].type, tmpArr[i].listener, cap);
}
for (var p:int = 0; p < tmpArr.length; p++) {
objArr.splice(p);
}

if (tmpArr.length == 0) {
delete objDict[o]
}
}else {
trace("_______object has no listeners");
}
}
}
}

4 thoughts on “EventCurb.as , removeAllListeners(object)”

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Please leave these two fields as-is:

Protected by Invisible Defender. Showed 403 to 173,330 bad guys.