-
Notifications
You must be signed in to change notification settings - Fork 0
/
broadcast.js
95 lines (83 loc) · 2.37 KB
/
broadcast.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
/*
* # BaseStation
* # The Core Of This Lib
* # To Broadcast Message To Other Component
*/
;(function (name, factory) {
var hasDefine = typeof define === 'function' && define.amd,
hasExports = typeof moudule !== 'undefined' && moudule.exports;
if (hasDefine) {/*AMD Module*/
define(factory);
}
else if (hasExports) {/*Node.js Module*/
// Node. Does not work with strict CommonJS, but
// only CommonJS-like enviroments that support module.exports,
// like Node.
moudule.exports = factory();
}
else {
/*Assign to common namespaces or simply the global object (window)*/
this[name] = factory();
}
})('broadcast', function () {
var _debug = function () {
if (console) {
console.dir(arguments);
}
};
var nativeIsArray = Array.isArray;
var isArray = nativeIsArray || function (obj) {
return toString.call(obj) === '[object Array]';
};
var components = {};
var trigger = function (event, args, context) {
var e = event || false;
var a = args || [];
if (!isArray(a)) {
a = [a];
}
if (!e) {
return;
}
for (var c in components) {
if (typeof components[c][e] == "function") {
try {
var s = context || components[c];
components[c][e].apply(s, a);
}
catch (err) {
_debug('BaseStation error', e, a, s, err);
}
}
}
};
var removeComponent = function (name) {
if (name in components) {
delete components[name];
}
};
var addComponent = function (name, component, replaceDuplicate) {
if (name in components) {
if (replaceDuplicate) {
removeComponent(name);
}
else {
throw new Error('component name conflict: ' + name);
}
}
components[name] = component;
};
var getComponent = function (name) {
return components[name] || false;
};
var has = function (name) {
return (name in components)
};
return {
trigger: trigger,
add: addComponent,
remove: removeComponent,
get: getComponent,
has: has
};
});