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
|
var URL = require("core/mini-url");
exports.Worker = function (package, id) {
var worker = new Worker(
URL.resolve(module.location, 'worker-script.js')
);
worker.postMessage({
"type": "init",
"package": package,
"module": id
})
worker.onmessage = function (event) {
// request module text
// handle URL resolution
// console log
// read a url
if (event.data.type === "console") {
console[event.data.method].apply(console, event.data.args);
} else if (event.data.type === "read") {
require.read(event.data.url)
.then(function (content) {
worker.postMessage({
type: "read",
url: event.data.url,
content: content
})
}, function (error) {
worker.postMessage({
type: "read",
url: event.data.url,
error: error
});
})
.end();
} else if (event.data.type === "forward") {
if (proxy.onmessage) {
proxy.onmessage({
data: event.data.data
});
} else {
// XXX
}
} else {
// XXX
}
};
var proxy = {
postMessage: function (data) {
worker.postMessage({
type: "forward",
data: data
});
},
onmessage: null,
terminate: function () {
return worker.terminate();
}
};
return proxy;
};
|