-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathEventDispatcherBase.ts
More file actions
289 lines (254 loc) · 8.08 KB
/
Copy pathEventDispatcherBase.ts
File metadata and controls
289 lines (254 loc) · 8.08 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import { ASObject, AXClass } from '@awayfl/avm2';
import { Event } from './Event';
/**
* Base export class for dispatching events
*
* @export class away.events.EventDispatcher
*
*/
export class EventDispatcherBase extends ASObject {
static axClass: typeof EventDispatcherBase & AXClass;
private _listenerObjects: Array<ListenerObject>;
private _t: any;
public toString(): string {
return '';
}
constructor(target: any = null) {
super();
this._t = target || this;
this._listenerObjects || (this._listenerObjects = []);
}
/**
* Add an event listener
* @method addEventListener
* @param {String} Name of event to add a listener for
* @param {Function} Callback function
*/
public addEventListener(type: string,
listener: (event: Event) => void,
useCapture: boolean = false,
priority: number /*int*/ = 0,
useWeakReference: boolean = false): void {
this._listenerObjects || (this._listenerObjects = []);
let l: ListenerObject = this._listenerObjects[type];
if (l === undefined)
l = this._listenerObjects[type] = new ListenerObject();
l.addEventListener(listener, priority);
}
/**
* Remove an event listener
* @method removeEventListener
* @param {String} Name of event to remove a listener for
* @param {Function} Callback function
*/
public removeEventListener(type: string, listener: (event: Event) => void): void {
this._listenerObjects || (this._listenerObjects = []);
const l: ListenerObject = this._listenerObjects[type];
if (l) {
l.removeEventListener(listener);
if (l.numListeners == 0)
delete this._listenerObjects[type];
}
}
/**
* Dispatch an event
* @method dispatchEvent
* @param {Event} Event to dispatch
*/
public dispatchEvent(event: Event): boolean {
this._listenerObjects || (this._listenerObjects = []);
const l: ListenerObject = this._listenerObjects[event.type];
if (l) {
// Flash reports the aggregation target (EventDispatcher(target) constructor arg)
// as currentTarget during dispatch — e.g. GreenSock dispatches tween events via an
// internal aggregated dispatcher and handlers cast event.currentTarget to the tween.
if (this._t)
(<any> event).currentTarget = this._t;
if (!event.target)
event.target = this._t;
//console.log("dispatchEvent", event.type, (<any>this).adaptee?.id);
l.dispatchEvent(event);
}
return true;
}
/**
* check if an object has an event listener assigned to it
* @method hasListener
* @param {String} Name of event to remove a listener for
* @param {Function} Callback function
*/
public hasEventListener(type: string, listener?: (event: Event) => void): boolean {
if (this._listenerObjects[type] === undefined)
return false;
if (listener != null)
return this._listenerObjects[type].getEventListenerIndex(listener) !== -1;
return this._listenerObjects[type].numListeners > 0;
}
}
interface IListenerPriority{
priority: number,
listeners?: Array<(event: Event) => void>
}
function sortListenersByPriority(a: IListenerPriority, b: IListenerPriority) {
return a.priority < b.priority ? -1 : 1;
}
export class ListenerObject {
private _index: number = 0;
private _singlePriority: number = 0;
private _listeners: Array<(event: Event) => void> = new Array<(event: Event) => void>();
private _listenersByPriority: IListenerPriority[];
public numListeners: number = 0;
public addEventListener(listener: (event: Event) => void, priority: number = 0): void {
// if event already exists, it will not be added again, and old priority will remain
if (!this._listenersByPriority) {
// single priority mode:
if (this._listeners.indexOf(listener) !== -1) {
// the listener is already present - do nothing just return
return;
} else {
// its a new listener
if (this._listeners.length == 0 || priority == this._singlePriority) {
// same priority as existing listeners. just add it and return
this._listeners.push(listener);
this.numListeners++;
return;
} else {
// different priority - switch to multi-priority mode
if (this._singlePriority < priority) {
this._listenersByPriority = [
{
priority:this._singlePriority,
listeners:this._listeners.concat()
},
{
priority:priority,
listeners:[listener]
}
];
} else {
this._listenersByPriority = [
{
priority:priority,
listeners:[listener]
},
{
priority:this._singlePriority,
listeners:this._listeners.concat()
}
];
}
this.numListeners++;
}
return;
}
}
// multi-priority mode:
//check if a listener already exists
for (let i = 0; i < this._listenersByPriority.length; i++) {
if (this._listenersByPriority[i].listeners.indexOf(listener) !== -1) {
return;
}
}
// find priority item
let priorityItem = null;
for (let i = 0; i < this._listenersByPriority.length; i++) {
if (this._listenersByPriority[i].priority == priority) {
priorityItem = this._listenersByPriority[i];
break;
}
}
if (!priorityItem) {
priorityItem = {
priority:priority,
listeners:[]
};
this._listenersByPriority.push(priorityItem);
if (this._listenersByPriority.length > 1) {
this._listenersByPriority.sort(sortListenersByPriority);
}
}
priorityItem.listeners.push(listener);
this.numListeners++;
}
public removeEventListener(listener: (event: Event) => void): void {
let index: number = -1;
if (!this._listenersByPriority) {
index = this._listeners.indexOf(listener);
if (index === -1)
return;
this._listeners.splice(index, 1);
this.numListeners--;
return;
}
//check if listener exists
for (let i = 0; i < this._listenersByPriority.length; i++) {
index = this._listenersByPriority[i].listeners.indexOf(listener);
if (index !== -1) {
this.numListeners--;
this._listenersByPriority[i].listeners.splice(index, 1);
return;
}
}
}
public dispatchEvent(event: Event): void {
// when listeners do attach/remove new listeners the new listeners will be ignored during this dispatch,
// and listeners that got removed will still dispatch during this dispatch
// this means we need to concat the listener-array before we iterate it
if (!this._listenersByPriority) {
// single-priority mode:
const listeners = this._listeners.length > 1 ? this._listeners.concat() : this._listeners;
const numListeners = listeners.length;
let i = 0;
for (i = 0; i < numListeners; i++) {
if (listeners[i])
listeners[i].call(listeners[i], event);
}
return;
}
// multi-priority mode:
let p = this._listenersByPriority.length;
//console.log("multi-priority mode", p);
while (p > 0) {
p--;
//console.log("multi-priority mode next", p);
const listeners = this._listenersByPriority[p].listeners.length > 1 ?
this._listenersByPriority[p].listeners.concat() : this._listenersByPriority[p].listeners;
const numListeners = listeners.length;
let i = 0;
//let len=events.length;
//console.log("events", len);
for (i = 0; i < numListeners; i++) {
if (listeners[i])
listeners[i].call(listeners[i], event);
}
}
if (this._listenersByPriority.length == 1) {
// if only 1 priority exists, we can go back to single-priority-mode
this._listeners = this._listenersByPriority[0].listeners.concat();
this._singlePriority = this._listenersByPriority[0].priority;
this._listenersByPriority = null;
}
}
/**
* get Event Listener Index in array. Returns -1 if no listener is added
* @method getEventListenerIndex
* @param {String} Name of event to remove a listener for
* @param {Function} Callback function
*/
public getEventListenerIndex(listener: (event: Event) => void): number {
if (!this._listenersByPriority) {
// single-priority mode:
return this._listeners.indexOf(listener);
}
// multi-priority mode:
for (let i = 0; i < this._listenersByPriority.length; i++) {
for (let e = 0; e < this._listenersByPriority[i].listeners.length; e++) {
if (this._listenersByPriority[i].listeners[e] == listener) {
return i;
}
}
}
return -1;
}
}
export default EventDispatcherBase;