-
Notifications
You must be signed in to change notification settings - Fork 24
/
jquery.hive.js
275 lines (221 loc) · 8.3 KB
/
jquery.hive.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
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
/*!
* jQuery.Hive Plugin
* http://github.com/rwldrn
* http://pollenjs.com
*
* Copyright 2010, Rick Waldron
* Dual licensed under the MIT or GPL Version 2 licenses.
*
* v0.2.00
* (see source for other credits)
*/
(function(jQuery) {
/**
* jQuery.Hive -> The Hive is a property of jQuery
**/
jQuery.Hive = (function() {
var /* @private */
// Thread ID Cache
_threadsById = {},
// Thread Cache
_threads = [],
// Thread Callback Cache
_fnCache = {},
// Last thread id
_lastThreadInt = 0;
function _addMessageListener(worker, callback) {
worker.addEventListener("message", function(event) {
var fn = callback,
response = event.data,
_msg;
jQuery.event.trigger( "workerMessageReceived", response );
if ( response.SEND_TO ) {
_msg = {
"message": response.message,
"SEND_TO": +response.SEND_TO,
"SEND_FROM": +response.SEND_FROM
};
jQuery.Hive.get(response.SEND_TO).send(_msg);
// If direct message return immediately, do not fire receive callback
return true;
}
return callback.call( worker, response );
}, false);
}
/* @private */
/* @public */
return {
/**
* jQuery.Hive.options -> Setup properties for jQuery.Hive.create( [options] )
**/
options: {
/**
* jQuery.Hive.options.count -> Set property from jQuery.Hive.create( { count: [int] } ), number of threads to create
**/
count: 1,
/**
* jQuery.Hive.options.worker -> Set string property from jQuery.Hive.create( { worker: [file-name] } ), name of worker file
**/
worker: "",
/**
* jQuery.Hive.options.receive -> Set callback from jQuery.Hive.create( { receive: [callback] } ), callback to execute when worker receives message (can be global or worker specific)
**/
receive: jQuery.noop,
/**
* jQuery.Hive.options.created -> Set callback from jQuery.Hive.create( { created: [callback] } ), callback to execute when workers have been created
**/
created: jQuery.noop,
/**
* jQuery.Hive.options.special -> NOT IMPLEMENTED/INCOMPLETE - Set callback as a second argument to $().send()
**/
special: "",
/**
* jQuery.Hive.options.error() -> NOT IMPLEMENTED/INCOMPLETE - Error handler
**/
error: function( event ) {
// INCOMPLETE
}
},
/**
* jQuery.Hive.create( options ) -> Array, create workers, returns array of all workers
**/
create: function( options ) {
// If no worker file is specified throw exception
if ( !jQuery.Hive.options.worker && !options.worker ) {
throw "No Worker file specified";
}
var i = 0,
_options = jQuery.extend({}, jQuery.Hive.options, options);
_options.count = options.count ? options.count : 1;
// If threads exist, new threads to cache
if ( _threads.length > 0 ) {
// force thread count starting position to avoid overwriting existing threads
i = _lastThreadInt + 1;
// set count to reflect added threads
_options.count = _lastThreadInt + _options.count + 1;
}
// Create specified number of threads
for ( ; i < _options.count; i++ ) {
var
// Create new worker thread
thread = new Worker( _options.worker );
// Garbage collect
jQuery( window ).unload( function() {
thread.terminate();
});
// Save this worker's identity
thread.WORKER_ID = (function(i) { return i; })(i);
thread.id = thread.WORKER_ID; // duplicitous... TODO: clean up
// Define Hive properties
thread.send = jQuery.Hive.send;
thread.special = "";
thread.onerror = _options.error;
var _wrapReceived = function onmessage(event) {
return _options.receive.call(this, event);
};
thread.receive = _wrapReceived;
_addMessageListener(thread, _wrapReceived);
// Store this callback in the Hive cache with assoc worker ID
_fnCache[thread.id] = {
active: [ _wrapReceived ],
inactive: []
};
// Store this worker in the Hive cache - by ID
_threadsById[thread.id] = thread;
// Store last thread id created
_lastThreadInt = thread.id;
// Store this worker in the Hive cache
_threads.push( thread );
}
// If a created callback is defined, wrap and fire
if ( _options.created ) {
var _wrapCreated = function() {
_options.created.call(this, _threads);
return _threads;
};
_wrapCreated();
jQuery.Hive.created = _wrapCreated;
}
jQuery.event.trigger( "workerCreated" );
// Allows assignment to var
return _threads;
},
/**
* jQuery.Hive.destroy( [id] ) -> destroy all or specified worker by id
**/
destroy: function( id ) {
if ( id ) {
_threadsById[ id ].terminate();
delete _threadsById[ id ];
delete _fnCache[ id ];
_threads = jQuery.map(_threads, function(obj) {
if ( obj.id != id ) {
return obj;
}
});
return _threads;
}
for ( var idx = 0; idx < _threads.length; idx++ ) {
_threads[ idx ].terminate();
}
// Delete All
jQuery.Hive.options.count = 0;
_threads = [];
_threadsById = {};
_fnCache = {};
jQuery.event.trigger( "workerDestroyed" );
return _threads;
},
/**
* jQuery.Hive.get( id ).send( message, callback ) -> Send [message] to worker thread, set optional receive callback
* --> SIMPLER ALTERNATIVE: $.Hive.get(id).send( [message], function() {} )
* --> Allows for passing a jQuery.Hive.get(id) object to $() ie. $( $.Hive.get(id) ).send( [message] )
**/
send: function( message, /*not implemented*/callback ) {
var _msg = message, _msgStr;
/*not implemented*/
if ( callback ) {
_addMessageListener(this, callback, true);
}
// if message is not an object (string || array)
// normalize it into an object
if ( typeof message == "string" || jQuery.isArray(message) ) {
_msg = {
"message" : message
};
}
if ( !message.SEND_FROM ) {
_msg.SEND_FROM = this.WORKER_ID;
}
_msg.WORKER_ID = this.WORKER_ID;
this.postMessage(_msg);
this._lastMessage = _msgStr;
jQuery.event.trigger( "workerMessageSent" );
return this;
},
/**
* jQuery.Hive.get( [id] ) -> Return all or specified worker by [id], [id] argument is optional
* --> $.Hive.get() returns all worker objects in the $.Hive
* --> $.Hive.get(1) returns the worker object whose ID is 1
**/
get: function( id ) {
if ( id !== undefined ) {
// Returns specified worker by [id] from private object cache
return _threadsById[id];
}
// Returns array of all existing worker threads
return _threads;
}
}
})();
/**
* jQuery.( $.Hive.get( id ) ).send( message ) -> Send [message] to worker thread
* --> SIMPLER ALTERNATIVE: $.Hive.get(id).send( [message] )
* --> Allows for passing a $.Hive.get(id) object to $() ie. $( $.Hive.get(id) ).send( [message] )
**/
jQuery.fn.send = function(message, callback) {
return this.each(function(i, thread) {
thread.send(message, callback);
});
};
})(jQuery);