forked from wilk/ExtJS-WebWorker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebWorkerManager.js
216 lines (194 loc) · 5.84 KB
/
WebWorkerManager.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
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
/**
* @class Ext.ux.WebWorkerManager
* @author Vincenzo Ferrari <wilk3ert@gmail.com>
* @singleton
*
* Manager of Ext.ux.WebWorker
*
* This singleton provide some useful functions to use for many webworkers.
*
* var ww1 = Ext.create ('Ext.ux.WebWorker', {
* file: 'myWorker.js'
* });
*
* Ext.ux.WebWorkerManager.register (ww1);
*
* var blob = [
* 'onmessage = function (msg) {' ,
* ' if (msg.event === "system shutdown") {' ,
* ' var res = {' ,
* ' event: "stop" ,' ,
* ' data: "Computation of worker 2 terminates correctly in 15h13m20s."' ,
* ' };' ,
* ' postMessage (res);' ,
* ' }' ,
* '}'
* ].join ('');
*
* var ww2 = Ext.create ('Ext.ux.WebWorker', {
* blob: blob
* });
*
* Ext.ux.WebWorkerManager.register (ww2);
*
* var ww3 = Ext.create ('Ext.ux.WebWorker', {
* file: 'otherWorker.js'
* });
*
* Ext.ux.WebWorkerManager.register (ww3);
*
* Ext.ux.WebWorkerManager.listen ('stop', function (ww, data) {
* Ext.Msg.show ({
* title: 'Worker stopped' ,
* msg: 'Log of the computation: ' + data ,
* icon: Ext.Msg.INFO ,
* buttons: Ext.Msg.OK
* });
* });
*
* // This will handled by everyone
* Ext.ux.WebWorkerManager.broadcast ('system shutdown', 'BROADCAST: the system will shutdown in few minutes.');
*
* Ext.ux.WebWorkerManager.stopAll ();
*
* Ext.ux.WebWorkerManager.unregister (ww1);
* Ext.ux.WebWorkerManager.unregister (ww2);
* Ext.ux.WebWorkerManager.unregister (ww3);
*/
Ext.define ('Ext.ux.WebWorkerManager', {
singleton: true ,
/**
* @property {Ext.util.HashMap} wsList
* @private
*/
wwList: Ext.create ('Ext.util.HashMap') ,
/**
* @method register
* Registers one or more Ext.ux.WebWorker
* @param {Ext.ux.WebWorker/Ext.ux.WebWorker[]} webworkers WebWorkers to register. Could be only one.
*/
register: function (webworkers) {
var me = this;
if (Ext.isObject (webworkers)) webworkers = [webworkers];
Ext.each (webworkers, function (webworker) {
me.wwList.add (webworker.id, webworker);
});
} ,
/**
* @method unregister
* Unregisters one or more Ext.ux.WebWorker
* @param {Ext.ux.WebWorker/Ext.ux.WebWorker[]} webworkers WebWorkers to unregister
*/
unregister: function (webworkers) {
var me = this;
if (Ext.isObject (webworkers)) webworkers = [webworkers];
Ext.each (webworkers, function (webworker) {
if (me.wwList.containsKey (webworker.id)) me.wwList.removeAtKey (webworker.id);
});
} ,
/**
* @method contains
* Checks if a webworker is already registered or not
* @param {Ext.ux.WebWorker} webworker The WebWorker to find
* @return {Boolean} True if the webworker is already registered, False otherwise
*/
contains: function (webworker) {
return this.wwList.containsKey (webworker.id);
} ,
/**
* @method get
* Retrieves a registered webworker by its id
* @param {String} id The id of the webworker to search
* @return {Ext.ux.WebWorker} The webworker or undefined
*/
get: function (id) {
return this.wwList.get (id);
} ,
/**
* @method getExcept
* Retrieves registered webworkers except the input
* @param {Ext.ux.WebWorker/Ext.ux.WebWorker[]} webworkers WebWorkers to exclude
* @return {Ext.util.HashMap} Registered webworkers except the input
* @private
*/
getExcept: function (webworkers) {
if (Ext.isObject (webworkers)) webworkers = [webworkers];
var list = this.wwList.clone ();
Ext.each (webworkers, function (webworker) {
list.removeAtKey (webworker.id);
});
return list;
} ,
/**
* @method each
* Executes a function for each registered webwork
* @param {Function} fn The function to execute
*/
each: function (fn) {
this.wwList.each (function (id, webworker, len) {
fn (webworker);
});
} ,
/**
* @method stopAll
* Stops any registered webworker
*/
stopAll: function () {
var me = this;
me.wwList.each (function (id, webworker, len) {
webworker.stop ();
me.unregister (webworker);
});
} ,
/**
* @method listen
* Adds an handler for events given to each registered webworker
* @param {String/String[]} events Events to listen
* @param {Function} handler The events' handler
*/
listen: function (events, handler) {
if (Ext.isString (events)) events = [events];
this.wwList.each (function (id, webworker, len) {
Ext.each (events, function (event) {
webworker.on (event, handler);
});
});
} ,
/**
* @method listenExcept
* Adds an handler for events given to each registered webworker, except webworkers given
* @param {String/String[]} events Events to listen
* @param {Ext.ux.WebWorker/Ext.ux.WebWorker[]} webworkers WebWorkers to exclude
* @param {Function} handler The events' handler
*/
listenExcept: function (events, webworkers, handler) {
if (Ext.isString (events)) events = [events];
this.getExcept(webworkers).each (function (id, webworker, len) {
Ext.each (events, function (event) {
webworker.on (event, handler);
});
});
} ,
/**
* @method multicast
* Sends a message to each webworker, except those specified
* @param {Ext.ux.WebWorker/Ext.ux.WebWorker[]} webworkers An array of webworkers to take off the communication
* @param {String} event The event to raise
* @param {String/Object} data The data to send
*/
multicast: function (webworkers, event, data) {
this.getExcept(webworkers).each (function (id, webworker, len) {
if (Ext.isEmpty (data)) webworker.send (event);
else webworker.send (event, data);
});
} ,
/**
* @method broadcast
* Sends a message to each webworker
* @param {String} event The event to raise
* @param {String/Object} data The data to send
*/
broadcast: function (event, data) {
this.multicast ([], event, data);
}
});