-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.js
174 lines (155 loc) · 4.02 KB
/
index.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
'use strict';
function doEval(self, __pseudoworker_script) {
/* jshint unused:false */
(function () {
/* jshint evil:true */
eval(__pseudoworker_script);
}).call(global);
}
function PseudoWorker(path) {
var messageListeners = [];
var errorListeners = [];
var workerMessageListeners = [];
var workerErrorListeners = [];
var postMessageListeners = [];
var terminated = false;
var script;
var workerSelf;
var api = this;
// custom each loop is for IE8 support
function executeEach(arr, fun) {
var i = -1;
while (++i < arr.length) {
if (arr[i]) {
fun(arr[i]);
}
}
}
function callErrorListener(err) {
return function (listener) {
listener({
type: 'error',
error: err,
message: err.message
});
};
}
function addEventListener(type, fun) {
/* istanbul ignore else */
if (type === 'message') {
messageListeners.push(fun);
} else if (type === 'error') {
errorListeners.push(fun);
}
}
function removeEventListener(type, fun) {
var listeners;
/* istanbul ignore else */
if (type === 'message') {
listeners = messageListeners;
} else if (type === 'error') {
listeners = errorListeners;
} else {
return;
}
var i = -1;
while (++i < listeners.length) {
var listener = listeners[i];
if (listener === fun) {
delete listeners[i];
break;
}
}
}
function postError(err) {
var callFun = callErrorListener(err);
if (typeof api.onerror === 'function') {
callFun(api.onerror);
}
if (workerSelf && typeof workerSelf.onerror === 'function') {
callFun(workerSelf.onerror);
}
executeEach(errorListeners, callFun);
executeEach(workerErrorListeners, callFun);
}
function runPostMessage(msg, transfer) {
function callFun(listener) {
try {
listener({data: msg, ports: transfer});
} catch (err) {
postError(err);
}
}
if (workerSelf && typeof workerSelf.onmessage === 'function') {
callFun(workerSelf.onmessage);
}
executeEach(workerMessageListeners, callFun);
}
function postMessage(msg, transfer) {
if (typeof msg === 'undefined') {
throw new Error('postMessage() requires an argument');
}
if (terminated) {
return;
}
if (!script) {
postMessageListeners.push({msg: msg, transfer: (transfer ? transfer : undefined)});
return;
}
runPostMessage(msg, transfer);
}
function terminate() {
terminated = true;
}
function workerPostMessage(msg) {
if (terminated) {
return;
}
function callFun(listener) {
listener({
data: msg
});
}
if (typeof api.onmessage === 'function') {
callFun(api.onmessage);
}
executeEach(messageListeners, callFun);
}
function workerAddEventListener(type, fun) {
/* istanbul ignore else */
if (type === 'message') {
workerMessageListeners.push(fun);
} else if (type === 'error') {
workerErrorListeners.push(fun);
}
}
var xhr = new XMLHttpRequest();
xhr.open('GET', path);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status >= 200 && xhr.status < 400) {
script = xhr.responseText;
workerSelf = {
postMessage: workerPostMessage,
addEventListener: workerAddEventListener,
close: terminate
};
doEval(workerSelf, script);
var currentListeners = postMessageListeners;
postMessageListeners = [];
for (var i = 0; i < currentListeners.length; i++) {
runPostMessage(currentListeners[i].msg, currentListeners[i].transfer);
}
} else {
postError(new Error('cannot find script ' + path));
}
}
};
xhr.send();
api.postMessage = postMessage;
api.addEventListener = addEventListener;
api.removeEventListener = removeEventListener;
api.terminate = terminate;
return api;
}
module.exports = PseudoWorker;