-
Notifications
You must be signed in to change notification settings - Fork 0
/
spectre.js
138 lines (118 loc) · 4.38 KB
/
spectre.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
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
(function(window) {
'use strict';
function Event() {
var _state = ["Registered", "Triggered", "Waiting", "Completed"];
var _config = {
registerAutomatically: true,
evObj: {
name: '',
fn: '',
state: _state[0]
}
}
var events = {}
/**
* Events for automatic subscription
*/
var unSubscribedEvent = {};
var eventMethods = {
on: function(name, fn) {
var data = null;
/**
* Before subscribing check for already triggered events
*/
var isUnsubscrbed = false;
if (unSubscribedEvent[name]) {
/**
* publish this event first and then let the function continue.
* This could also be a promise to make sure that the function executes asynchronously
*
*/
data = unSubscribedEvent[name].data;
/**
* To make sure that the event is published after automatic subscription
*/
isUnsubscrbed = true;
}
/**
* Initialize events[name] = [] so that the function calls do not stack up and trigger it multiple times
*/
events[name] = [];
// var idx = 0;
/**
* If the same function already exists then remove the previous one and re-register a new function to maintain the reference to the subscribed object
*/
// events[name].forEach(function(e) {
// if (e.fn.toString() === fn.toString()) {
// events[name].splice(idx, 1);
// }
// idx++;
// })
events[name].push({
name: name,
fn: fn,
state: _state[0]
});
if (isUnsubscrbed) {
/**
* Once subscribed to, publish the event back to receive the data and store it as per the local logic or the callback provided
*/
this.publish(name, unSubscribedEvent[name].data);
/**
* Remove event once subscription is successful
*/
unSubscribedEvent[name] = null;
}
},
publish: function(name, data) {
/**
* If event is triggered and is not Registered then save the data
*/
if (!events[name]) {
/**
* Create new unSubscribedEvent object every time such an event is registered
* All unSubscribedEvent will not need explicit registration. These will be autosubscribed. The function calls will get overwritten.
*/
unSubscribedEvent[name] = {};
unSubscribedEvent[name] = { name: name, data: data };
/**
* Return after storing the unsubscribed event since continuing will throw an error
*/
return;
}
/**
* If event is subscribed then run normally
*/
/**
* Maintinaing loop count
*/
events[name].forEach(function(val) {
/**
* Trigger only events which did not execute before
*/
val.state = _state[1];
val.fn(data);
val.state = _state[3];
});
},
removeTriggeredEvents: function(name) {
}
}
return eventMethods;
}
if (typeof(eventMethods) === 'undefined') {
window.eventManager = Event();
} else {
console.log("Library already defined.");
}
})(window);
Array.prototype.match = function(val) {
var isMatch = false;
this.forEach(function(e) {
if (e.fn.toString() === val) {
isMatch = true;
return isMatch;
}
});
return isMatch;
}