This repository was archived by the owner on Sep 8, 2020. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathThread.jQuery.js
More file actions
167 lines (153 loc) · 5.13 KB
/
Copy pathThread.jQuery.js
File metadata and controls
167 lines (153 loc) · 5.13 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
/**
* @author Joseph Evans <joeevs196@gmail.com>
* @version 1.0.0
* @since 02/09/2018
* @desc The purpose of this file is to implement a multi threading
* wrapper for jQuery. The reason as to why it's wrapped in a
* self invoked function is to proevent any naming issues, in
* addition to the fact that jQuery may or may not have been
* declared already, in which case, this code will throw an error.
* @copyright Joseph Evans (c) 2018
* @todo Test how well the code below works with jQuery.
*/
!function () {
$ = jQuery || $ || {};
if ($ == null) {
throw new Error("jQuery is not defined.");
} else if ($.fn == null) {
throw new Error("The prototype property isn't available.");
}
$.fn = $.fn || {};
$.fn.Thread = function () {
/**
* @private
* @desc These variables which are simply initialised below
* are simply the private properties that we want a thread
* object to have access to.
*/
var name, execute, update, oncomplete, data, thread;
/**
* @public
* @desc This object is essentially a wrapper for all of the
* public methods.
*/
var publicObject = {
/**
* @function name
* @param {String} n
* @return {String||Object}
* @desc This will simply return the name assigned to the
* current thread. Or if a string is provided to this
* method then it will assign the name and then return
* the thread object.
*/
name: function (n) {
if (n == null) {
return name;
} else {
name = n;
return publicObject;
}
},
/**
* @function execute
* @param {Function} code
* @return {Function||Object}
* @desc The purpose of this code is to return the code that
* has been allocated to the current thread object. Or to set
* the code that will be executed by this thread object, in
* which case, it will return an object.
*/
execute : function (code) {
if (code == null) {
return execute;
} else {
var exec = code.toString();
exec = exec.substr(0, exec.lastIndexOf("}"));
exec += 'self.postMessage(arguments[0].data); \n};';
execute = exec;
return publicObject;
}
},
/**
* @function onComplete
* @param {Function} complete
* @return {Function||Object}
* @desc The purpose of this method is to return the code that
* should be fired when the thread has finished it's main bulk
* of the code. Or to set the code that will be fired within
* this thread.
*/
onComplete : function (complete) {
if (complete == null) {
return oncomplete;
} else {
oncomplete = complete;
return publicObject;
}
},
/**
* @function data
* @param {*} d
* @return {*}
* @desc The purpose of this method is to simply return any data that
* handed into this thread object. Or this method can be used to
* set the data that has been assigned to this thread object,
* in which case it will return an object.
*/
data : function (d) {
if (d == null) {
return data;
} else {
data = d;
return publicObject;
}
},
/**
* @function getThread
* @return {Worker}
* @desc The purpose of this method is to simply return the worker that
* is wrapped up inside this thread object.
*/
getThread : function () {
return thread;
},
/**
* @function start
* @return {Object}
* @desc The prupose of this method is to initiate executing the code
* that has been assigned to the current thread object.
*/
start : function () {
var stringToExecute = 'self.onmessage = ' + execute.toString();
var blob = new Blob([stringToExecute], {type:"text/javascript"});
var URIObject = window.URL.createObjectURL(blob);
thread = new Worker(URIObject);
if (typeof oncomplete == 'function') { thread.onmessage = oncomplete; }
thread.postMessage(data);
return publicObject;
},
/**
* @function kill
* @return {Object}
* @desc The purpose of this method is to help clear up some memory,
* this will terminate the thread, best idea to run this code
* once the thread has finished, just to be sure.
*/
kill : function () {
try {
thread.terminate;
thread = null;
} catch (Exception) {
/* no need to worry about it */
}
return publicObject;
}
};
/**
* @ignore
* @desc Here the wrapper containing all of the above methods is returned.
*/
return publicObject;
};
}();