-
Notifications
You must be signed in to change notification settings - Fork 0
/
am.emmiter.js
82 lines (68 loc) · 2.24 KB
/
am.emmiter.js
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
'use strict'
function amEventEmitter(){
this.events = {};
//e.x:
// this.events = {
// 'event1' : [ function(){}, function(){} ]
// }
this.register = (event, callBackFunction) => {
if(event == undefined || callBackFunction == undefined){
errorNotifier({'error': 'register function required parameters EventName and callback function'});
return false;
}
if(typeof callBackFunction !== "function"){
errorNotifier({'error': 'register function required second parameters to be a callback function'});
return false;
}
if (this.events[event] != undefined){
this.events[event].push(callBackFunction);
}else{
this.events[event] = [];
this.events[event].push(callBackFunction);
}
return true;
}
this.unregister = (event) => {
if (this.events[event] != undefined){
delete this.events[event];
return true;
}else{
errorNotifier({'error' : 'No event registered with name <'+event+'>'});
return false;
}
}
this.mute = (event) => {
if (this.events[event] != undefined){
this.events[event].unshift('mutted');
return true;
}else{
errorNotifier({'error' : 'No event registered with name <'+event+'>'});
return false;
}
}
this.unmute = (event) => {
if (this.events[event] != undefined && this.events[event][0] == "mutted"){
this.events[event].shift();
return true;
}else{
errorNotifier({'error' : 'No event registered with name <'+event+'>'});
return false;
}
}
this.emit = (event,data) => {
if(typeof this.events[event] === 'undefined' ){
errorNotifier({'error' : 'No event registered with name <'+event+'>'});
return false;
}
if(this.events[event][0] == 'mutted'){
return false;
}
for(let i=0;i<this.events[event].length;i++){
this.events[event][i](data);
}
}
let errorNotifier = (error) => {
throw(error);
console.error(error)
}
}