-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcell.js
More file actions
311 lines (278 loc) · 10.6 KB
/
cell.js
File metadata and controls
311 lines (278 loc) · 10.6 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
Added filter function to work properly.
Fixed bug when a module was started, stopped and started again.
Changed valitation if a module has a filter.
Proposed to separe $component $inject in another file to work with them.
*/
var components = {};
var workers = {};
var Mediator = function() {
var dMode = false;
var debug = function(msg,type) {
if(dMode){
if(type == "err"){
console.error(msg);
}else{
console.log(msg);
}
}
};
//* The broadcast function sends a broadcast to the mediator and it search in their modules
//If there is a function listening to that event and executes it. If a module have a coding
//error the javascript keeps still working.
var broadcast = function(event, args, source) {
if (!event) {
return;
}
args = args || [];
debug(["Mediator broadcasting:", event,"[",args,"]"].join(' '));
for (var c in components) {
if (typeof components[c][event] == "function" && components[c]['state'] != 'stopped') {
try {
//Added Still uncomplete we need to trigger first the filter before the broadcast function
if(typeof components[c]["filter"] === "function"){
source = components[c]["filter"];
var FN_ARGS = /^function\s*[^\(]*\(\s*([^\)]*)\)/m;
var textEvent = components[c][event].toString();
var argsFunc = textEvent.match(FN_ARGS)[1].split(',');
var textFilter = components[c]["filter"].toString();
var argsFilter = textFilter.match(FN_ARGS)[1].split(',');
if(argsFunc[0] === "" && argsFunc.length == 1 && args.length == 0){
argsFunc.pop();
}
if(argsFunc.length != args.length){
debug("Invalid Arguments for "+ event + " on module "+ c, "err");
}
else if((argsFunc.length == args.length) && (argsFilter.length == args.length) ){
if(!components[c]["filter"].apply(source,args)){
debug("Filter for \""+c+"\" failed with args: ["+ args +"]", "err" );
}else{
debug("Mediator calling: " + event + " on '" + c + "' module");
source = source || components[c];
components[c][event].apply(source, args);
}
}else if((argsFunc.length === 0 && args.length === 0) || ((argsFunc.length == args.length) && (argsFilter.length != args.length))){
debug("Mediator calling: " + event + " on '" + c + "' module");
source = source || components[c];
components[c][event].apply(source, args);
}
}else{
debug("Mediator calling: " + event + " on '" + c + "' module");
source = source || components[c];
components[c][event].apply(source, args);
}
} catch (error) {
var url = error.stack.split('\n')[1].match(/\(.+\)/g)[0];
debug(["Mediator error. Module '", c +"', Function: "+ event +", Args:"+args+", "+ error, url].join(' '));
}
}else{
console.log('No listener for '+event + ' on ' + c);
console.log(components[c]);
}
}
};
//* addComponent function creates a new module and add's it to the component array,
//all the new components start with a 'stopped' state. If it is stopped it won't listen
//to broadcast events.
//Added options object
var addComponent = function(name, component, options) {
if (name in components) {
if(options){
if(options["replace"]) {
removeComponent(name);
}
}else {
throw new Error('Mediator name conflict: ' + name);
}
}
components[name] = component;
components[name]['state']='stopped';
//Added to receive a filter function for all the functions on a module.
if(options && options["filter"] && typeof options["filter"] === "function"){
components[name]["filter"] = options["filter"];
}
};
//* removeComponent function delete the modules from the component's array
var removeComponent = function(name) {
if (name in components) {
delete components[name];
}
};
//* registerToComponent function registers a new function to an existing module on the Mediator.
var registerToComponent = function(name,event,registeredFunction,overwrite){
if(components[name]){
if(!components[name][event] || overwrite === true){
components[name][event] = registeredFunction;
}else{
debug("Event already exists on component \"" + name + "\"");
}
}else{
debug("This component \""+name+"\" doesn't exists");
}
}
//* unregisterFromComponent function unregisters a function from an existing module on the Mediator.
var unregisterFromComponent = function(name,event){
if(components[name]){
if(components[name][event]){
delete components[name][event];
}else{
debug("Event doesn't exists on component \"" + name + "\"");
}
}else{
debug("This component \""+name+"\" doesn't exists");
}
}
//* getComponent function returns the component object for a established name if it exist in the array
var getComponent = function(name) {
return components[name]; // undefined if component has not been added
};
//* contains function return a value if a certain component is in the array
var contains = function(name) {
return (name in components);
};
//* startModule function changes the default stopped value of a component to started
var startModule = function(name,args){
if(components[name]){
if(components[name]['state'] === "started"){
debug("Module: '" + name + "' Already Started ");
return;
}
var filter;
if(typeof components[name]["filter"] === "function"){
filter = components[name]["filter"];
}
if(components[name].toString() !== "[object Object]"){
components[name] = $inject.process(components[name]);
}
components[name]['state']='started';
if(filter){
components[name]["filter"] = filter;
}
debug("Module: '" + name + "' Started ");
if(typeof components[name]["init"] == "function"){
debug(["Module: '" + name + "' calling init method"].join(' '));
components[name].init.apply(components[name], args);
}
}else{
debug(["Module","'"+name+"'","is not registered"].join(" "));
}
};
//* stopModule functions changes the state of the component to stopped
var stopModule = function(name){
if(components[name]){
components[name]['state']='stopped';
debug(["Module: '" + name + "' Stopped"].join(' '));
}else{
debug(["Module","'"+name+"'","is not registered"].join(" "));
}
};
//* startDebug enables log notifications from the Mediator
var startDebug = function(){
if(dMode){
dMode = false;
}else{
dMode = true;
}
};
//* Returns the current state for a certain Module
var getState = function(name){
return components[name]['state'];
}
var addWebWorker = function (name, js, event, message) {
try {
//var pattern = /^[\w.-]+\.js$/;
var pattern = /^[\w.-_\/]+\.js$/;
if(typeof js == "object"){
var blob = new Blob([js.text()],{type:'text/javascript'});
var URL = window.URL || window.webkitURL;
var code = URL.createObjectURL(blob);
workers[name] = new Worker(code);
workers[name].js = code;
workers[name].event = event;
workers[name].terminated = false;
}else if(js.match(pattern)){
workers[name] = new Worker(js);
workers[name].js = js;
workers[name].event = event;
workers[name].terminated = false;
}else{
var blob = new Blob([js],{type:'text/javascript'});
var URL = window.URL || window.webkitURL;
var code = URL.createObjectURL(blob);
workers[name] = new Worker(code);
workers[name].js = code;
workers[name].event = event;
workers[name].terminated = false;
}
if (typeof event === "function") {
workers[name].onmessage = event
}
if (message) {
workers[name].postMessage(message);
}
workers[name].postMessage();
debug(["Worker '", name, "' added and running"].join(""))
} catch (error) {
/*var u = error.stack.split("\n")[1].match(/\(.+\)/g)[0];
debug(["Mediator error. Worker '", name + "', Error: " + error, u].join(" "),"err");*/
console.log(error);
}
};
var stopWebWorker = function (name) {
if (name in workers) {
workers[name].terminate();
workers[name].terminated = true;
debug(["Worker:", "'" + name + "'", "stopped"].join(" "))
} else {
debug(["Worker:", "'" + name + "'", "is not registered"].join(" "))
}
};
var resumeWebWorker = function(name){
if (name in workers) {
if(workers[name].terminated){
var event = workers[name].event;
var js = workers[name].js;
workers[name] = new Worker(js);
workers[name].event = event;
workers[name].onmessage = event;
workers[name].js = js;
debug(["Worker:", "'" + name + "'", "Resumed"].join(" "))
}else{
debug(["Worker:", "'" + name + "'", "is not terminated"].join(" "))
}
} else {
debug(["Worker:", "'" + name + "'", "is not registered"].join(" "))
}
}
var postWebWorker = function (name, data) {
if (name in workers) {
if (data) {
workers[name].postMessage(data);
debug(["Post for worker:", "'" + name + "'", " data: [", data, "]"].join(" "));
}
else {
debug("Data is not defined");
}
} else {
debug(["Worker :", "'" + name + "'", "is not registered"].join(" "))
}
};
return {
name : "Mediator",
broadcast : broadcast,
add : addComponent,
rem : removeComponent,
register : registerToComponent,
unregister: unregisterFromComponent,
get : getComponent,
has : contains,
start : startModule,
stop : stopModule,
debugMode : startDebug,
state : getState,
addWorker : addWebWorker,
stopWorker: stopWebWorker,
postWorker: postWebWorker,
resumeWebWorker: resumeWebWorker
};
}();