-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathnerve.js
136 lines (109 loc) · 4.48 KB
/
nerve.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
(function () {
// issue #1: routes should be object instead of array as per usage below.
var routes = {};
function findSubscriber(callReference, array) {
if (!array)
return null;
var i = 0, len = array.length;
for (; i < len; i++) {
if (array[i].callee === callReference)
return array[i];
}
return null;
}
window.nerve = {
on: function (channel, route, callback, scope) {
/// <summary>Listen to a given channel or listen to a channel and route combination</summary>
/// <param name="channel" type="String">The category of a an event</param>
/// <param name="route" optional="true" type="String">The sub category of an event</param>
/// <param name="callback" type="Function">A callback to to handle the event</param>
/// <param name="scope" type="Function">The scope reference you are calling about</param>
var c = channel, r = null, cb = null, caller = null;
if (arguments.length == 1) {
throw Error('A channel and a callback must be specified');
} else if (arguments.length == 2) {
if (Object.prototype.toString.call(arguments[1]) == "[object Function]") {
cb = arguments[1];
caller = arguments.callee;
}
} else if (arguments.length == 3 && Object.prototype.toString.call(arguments[2]) == "[object Function]") {
// issue #1: arguments[1] was being checked as the funciton, but [1] should be the route.
// issue #1: r was not being set and shoudl be the arguments[1] or route parameter.
if (Object.prototype.toString.call(arguments[2]) == "[object Function]") {
r = arguments[1];
cb = arguments[2];
caller = arguments[3] || arguments.callee;
} else {
throw Error('Last parameter must be a callback function');
}
} else if (arguments.length == 4) {
c = channel;
r = route;
cb = callback;
caller = scope || arguments.callee;
}
if (!cb) {
return;
}
if (!routes[channel]) {
//--- check on route
routes[channel] = [];
}
if (!r) {
r = 'root';
}
if (r && !routes[channel][r]) {
routes[channel][r] = [];
}
//--- check to make sure we aren't adding ourselves twice
if (findSubscriber(caller, routes[channel][r]))
return;
routes[channel][r].push({
callee: caller,
callback: cb
});
},
off: function (channel, route, scope) {
if (routes[channel]) {
var r = 'root', caller = scope || arguments.callee;
if (route) r = route;
if (!routes[channel][r]) return;
var i = 0, len = routes[channel][r].length;
for (; i < len; i++) {
if (routes[channel][r][i].callee === caller)
delete routes[channel][r][i];
}
}
},
send: function (channel, route, context) {
/// <summary></summary>
/// <param name="channel" type="Object"></param>
/// <param name="route" type="Object"></param>
/// <param name="context" type="Object"></param>
var r = 'root', ctx = null;
if (arguments.length == 2) {
ctx = arguments[1];
} else if (arguments.length == 3) {
r = route;
ctx = context;
}
if (!routes[channel] || !routes[channel][r]) {
return;
}
var listeners = routes[channel][r], i = 0, len = listeners.length;
for (; i < len; i++) {
(function (ch, rt, idx) {
var ref = setTimeout(function () {
try {
routes[ch][rt][idx].callback(ctx);
clearTimeout(ref);
} catch (e) {
return;
}
});
})(channel, r, i);
}
}
};
}
)();