/
worker.js
154 lines (154 loc) · 5.23 KB
/
worker.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
(function () {'use strict';
/*! (c) 2018 Andrea Giammarchi (ISC) */
function walkThrough(O, K) { return O[K]; }
var namespace;
var channels = {};
var instances = {};
var onceExposed = new Promise(function (resolve) {
self.workway = function workway(exposed) {
return Promise.resolve(exposed).then(function (result) {
namespace = result;
resolve(remoted(result));
});
};
function remoted(object) {
return function $(object, current, remote) {
Object.keys(object).forEach(function (key) {
var value = object[key];
var path = current.concat(key);
if (typeof value === 'function') {
remote[key] = /^[A-Z]/.test(key) ?
{
type: 'class',
path: path,
methods: Object.getOwnPropertyNames(value.prototype)
.filter(no(['constructor']))
.concat('destroy'),
statics: Object.getOwnPropertyNames(value)
.filter(no([
'arguments', 'callee', 'caller',
'length', 'name', 'prototype'
]))
.reduce(
function (info, key) {
if (typeof value[key] === 'function') {
info.methods.push(key);
} else {
info.values.push([key, value[key]]);
}
return info;
},
{
methods: [],
values: []
}
)
} :
{
type: 'function',
path: path
};
} else if (remote.toString.call(value) === '[object Object]') {
remote[key] = {
type: 'object',
path: path,
value: {}
};
$(value, path, remote[key].value);
} else if (value !== void 0) {
remote[key] = {
type: 'any',
path: path,
value: value
};
}
});
return remote;
}(object, [], {});
function no(within) {
return function (what) {
return within.indexOf(what) < 0;
};
}
}
});
self.addEventListener('message', function (event) {
var method;
var data = event.data;
var channel = data.channel;
var message = data.message;
if (channels[channel]) {
event.stopImmediatePropagation();
var id = message.id;
var path = message.path;
var args = message.args;
var resolved = function (result) { send({result: result}); };
var rejected = function (error) {
if (
error != null &&
typeof error === 'object' &&
'message' in error
)
send({error: {
stack: error.stack,
message: error.message
}});
else
send({error: {source: error}});
};
var send = function (message) {
message.id = id;
self.postMessage({
channel: channel,
message: message
});
};
try {
if (message.hasOwnProperty('method')) {
method = message.method;
var Class = path.reduce(walkThrough, namespace);
if (!Class)
return rejected('Unknown Class ' + path.join('.'));
if (message.hasOwnProperty('object')) {
var object = message.object;
var instance = instances[object.id] ||
(instances[object.id] = new Class);
if (method === 'destroy')
delete instances[object.id];
else {
Object.keys(object.value)
.forEach(function (key) {
instance[key] = object.value[key];
});
Promise.resolve(instance[method].apply(instance, args))
.then(resolved, rejected);
}
} else {
Promise.resolve(Class[method].apply(Class, args))
.then(resolved, rejected);
}
} else {
var context = path.slice(0, -1).reduce(walkThrough, namespace);
if (!context)
return rejected('Unknown namespace ' + path.slice(0, -1).join('.'));
method = path[path.length - 1];
if (typeof context[method] !== 'function')
return rejected('Unknown method ' + path.join('.'));
Promise.resolve(context[method].apply(context, args))
.then(resolved, rejected);
}
} catch(error) {
rejected(error);
}
} else if (/^(-?\d+\.\d+)$/.test(channel)) {
channels[channel] = true;
event.stopImmediatePropagation();
onceExposed.then(function (namespace) {
self.postMessage({
channel: channel,
namespace: namespace
});
});
}
});
}());