-
Notifications
You must be signed in to change notification settings - Fork 1
/
worker.common.js
36 lines (35 loc) · 990 Bytes
/
worker.common.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
module.exports = function (self) {
var common = {
emit: function(id) {
return function(data) {
self.postMessage(Object.assign({ id: id }, data))
}
},
wrapError: function(error) {
return isError(error) ? { message: error.message, stack: error.stack } : error
},
emitError: function(emit) {
return function(error) {
emit({ error: common.wrapError(error) })
}
},
userEmit: function(emit) {
userEmit = function(userData) {
emit({ user: userData })
}
userEmit.delayed = function(userData) {
return function(valueToChain) {
emit({ user: userData })
return valueToChain
}
}
return userEmit
}
}
return common
}
function isError(target) {
return !!(target && (target.constructor === Error || ['error', 'exception'].some(function(word) {
return target.constructor.prototype.constructor.name.toLowerCase().includes(word)
})))
}