This repository was archived by the owner on Jul 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEventDispatcher.js
More file actions
60 lines (54 loc) · 1.38 KB
/
EventDispatcher.js
File metadata and controls
60 lines (54 loc) · 1.38 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
class EventDispatcher{
constructor()
{
this.listners = {} ;
}
/**@description Add a listner to this type
* @param {string} type
* @param {function} trigger
* @return {void}
*/
addEventListner(type,trigger)
{
if(trigger===undefined)
{
return ;
}
console.log(trigger+" added for "+type)
if(this.listners[type] === undefined)
{
this.listners[type] = [] ;
}
this.removeEventListner(type,trigger);
this.listners[type].push(trigger);
}
/**@description Remove this function from the current listnere type */
removeEventListner(type,trigger)
{
if(trigger===undefined)
{
return ;
}
if(this.listners[type]!==undefined)
{
let index = this.listners[type].indexOf(trigger);
if(index>=0)
{
this.listners[type].splice(index,1);
}
}
}
/**@description Dispatch all triggers for this type of event
* @param {string} type
* @param {any}
*/
dispatchEvent(type,param=null)
{
console.log("* dispatched : "+type);
if(this.listners[type]!==undefined)
{
this.listners[type].forEach(function(item){item(type,param)})
}
}
}
export default EventDispatcher ;