-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathWebWorker.js
219 lines (198 loc) · 5.56 KB
/
WebWorker.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
217
218
219
/**
* @class Ext.ux.WebWorker
* @author Vincenzo Ferrari <wilk3ert@gmail.com>
*
* Wrapper for HTML5 WebWorker
*
* This class provide an interface for HTML5 WebWorker.
*
* <h1>Pure text communication</h1>
* The communication is text-only, without objects or any other kind of data.
*
* var webworker = Ext.create ('Ext.ux.WebWorker', {
* file: 'myWorker.js' ,
* listeners: {
* message: function (ww, message) {
* console.log ('A new message is arrived: ' + message);
* } ,
* error: function (ww, error) {
* Ext.Error.raise (error);
* }
* }
* });
*
* <h1>Pure event-driven communication</h1>
* The communication is event-driven: an event and a String or Object are sent and the webworker handles different events.
*
* var webworker = Ext.create ('Ext.ux.WebWorker', {
* file: 'myWorker.js' ,
* listeners: {
* message: function (ww, message) {
* ww.send ('parse', 'a string to parse');
* ww.send ('verify equation', {
* equation: 'x+y-z=10' ,
* x: 10 ,
* y: 5 ,
* z: 5
* });
* }
* }
* });
*
* // A 'terminate' event is sent from the worker (myWorker.js)
* // 'data' has 'log' and 'msg' fields
* webworker.on ('terminate', function (data) {
* console.log ('Log: ' + data.log);
* console.log ('Message: ' + data.msg);
* });
*
* <h1Mixed communication</h1>
* The communication is mixed: it can handles text-only and event-driven communication.
*
* var webworker = Ext.create ('Ext.ux.WebWorker', {
* file: 'myWorker.js' ,
* listeners: {
* message: function (ww, message) {
* console.log ('Text-only message arrived is: ' + message);
*
* ww.send ('parse', 'a string to parse');
* ww.send ('verify equation', {
* equation: 'x+y-z=10' ,
* x: 10 ,
* y: 5 ,
* z: 5
* });
* }
* }
* });
*
* // A 'terminate' event is sent from the worker (myWorker.js)
* // 'data' has 'log' and 'msg' fields
* webworker.on ('terminate', function (data) {
* console.log ('Log: ' + data.log);
* console.log ('Message: ' + data.msg);
* });
*/
Ext.define ('Ext.ux.WebWorker', {
alias: 'widget.webworker' ,
mixins: {
observable: 'Ext.util.Observable'
} ,
config: {
/**
* @cfg {String} blob Inline worker. Use only this or file.
*/
blob: '' ,
/**
* @cfg {String} file A separated that contains the worker. Use only this or blob.
*/
file: '' ,
/**
* @cfg {String} itemId A further id specified by the user.
*/
itemId: ''
} ,
/**
* Creates new WebWorker
* @param {Object} config The configuration options may be specified as follows:
*
* var config = {
* file: 'myWorker.js'
* };
*
* or
*
* var config = {
* blob: 'onmessage = function (e) {postMessage ("A message from worker: + " e.data);}'
* };
*
* var ww = Ext.create ('Ext.ux.WebWorker', config);
*
* @return {Ext.ux.WebWorker} An instance of Ext.ux.WebWorker or null if an error occurred.
*/
constructor: function (cfg) {
var me = this;
me.initConfig (cfg);
me.mixins.observable.constructor.call (me, cfg);
me.addEvents (
/**
* @event error
* Fires after an error occured
* @param {Ext.ux.WebWorker} this The webworker
* @param {Object} error The error object to display
*/
'error' ,
/**
* @event message
* Fires after a message is arrived from the worker.
* @param {Ext.ux.WebWorker} this The webworker
* @param {String/Object} message The message arrived
*/
'message'
);
try {
// Makes inline worker
if (Ext.isEmpty (me.getFile ())) {
var winURL = window.URL || window.webkitURL ,
blob = new Blob ([me.getBlob ()], {type: 'text/plain'}) ,
inlineFile = winURL.createObjectURL (blob);
me.worker = new Worker (inlineFile);
}
// Uses file
else {
me.worker = new Worker (me.getFile ());
}
me.id = Ext.id ();
me.worker.onmessage = function (message) {
// Message event is always sent
me.fireEvent ('message', me, message.data);
/*
message.data : object
msg.event : event to raise
msg.data : data to handle
*/
if (Ext.isObject (message.data)) me.fireEvent (message.data.event, me, message.data.data);
};
me.worker.onerror = function (message) {
me.fireEvent ('error', me, message);
};
}
catch (err) {
Ext.Error.raise (err);
return null;
}
return me;
} ,
/**
* @method send
* Sends data. If there's only the first parameter (events), it sends it as a normal string, otherwise as an object
* @param {String/String[]} events Events that have to be handled by the worker
* @param {String/Object} data The data to send
*/
send: function (events, data) {
var me = this;
// Treats it as a normal message
if (arguments.length === 1) {
if (Ext.isString (events)) me.worker.postMessage (events);
else Ext.Error.raise ('String expected!');
}
// Treats it as an event-driven message
else if (arguments.length >= 2) {
if (Ext.isString (events)) events = [events];
Ext.each (events, function (event) {
var msg = {
event: event ,
data: data
};
me.worker.postMessage (msg);
});
}
} ,
/**
* @method stop
* Stops the webworker
*/
stop: function () {
this.worker.terminate ();
}
});