我正在尝试使用Chrome从Web Worker内部启动Web Worker。从历史上看,这是Chrome Host API的问题所在,但是:
根据ChromeStatus的数据,截至2018-10-22
幸运的我;我正在使用专门的 worker 。
我找不到可用的信息与Chrome在桌面版本69(我使用的是Chrome 72)中对支持的声明正式相抵触,而我的嵌套工作人员正坐在他的手上,拒绝让Janice喝咖啡。
我在玩三个 Activity 部分:一个SlaveDriver,一个Minion和一个Peon。 SlaveDriver委托(delegate)给Minion。奴才由Peon代表,Peon负责工作,每个接收者都承担功劳。
SlaveDriver
(
function(){
var minion = new Worker('./Features/Work/Work.Minion.js');
let crackTheWhip= ()=>
{
let message = {ID: 1};
minion.postMessage(
JSON.stringify(message)
);
console.log(message.ID);
};
let take_the_credit = (message)=>
{
let work = JSON.parse(message.data);
console.log("Hot Coffee!");
console.log(JSON.stringify(work));
};
minion.onmessage = take_the_credit;
return {
GetJaniceHerCoffee: crackTheWhip
};
})();
Minion ./Features/Work/Work.Minion.js
(
function(){
var self = this;
var peon = new Worker('./Features/Work/Work.Peon.js');
let receiveWorkOrder = (message)=>
{
console.log('Delegating to a Peon');
peon.postMessage(
message.data
);
};
let take_the_credit = (message)=>
{
console.log('Taking the credit');
let work = JSON.parse(message.data);
self.postMessage(JSON.stringify(
work
));
};
peon.onmessage = take_the_credit;
self.onmessage = receiveWorkOrder;
}
)();
Peon ./Features/Work/Work.Peon.js
(
function(){
var self = this;
let receiveWorkOrder = (message)=>
{
console.log("surfing LinkedIn.");
console.log("surfing stackoverflow");
console.log("...wth is this? *sigh*");
let work_result = {Value: "Coffee"};
self.postMessage(JSON.stringify(
work_result
));
};
self.onmessage = receiveWorkOrder;
}
)();
如果我停止委派小仆,让他去做牡丹的所有工作,一切都会很好。但是,当我尝试让小兵的代表参加这个 Activity 时,它就会蠕动地出现在我身上。在DevTools中,当我向它发布消息时,我可以在我的线程列表中看到该奴才被激活。我的调试上下文跳到了工作线程,当我将Message发送到peon时,Peon线程被添加到Threads列表中,但所有传播似乎都停止了。控制台日志不会发生。调试上下文不会跳转到Peon线程。我的奴才的
take_the_credit
回调未收到响应。牡丹只坐在那里,不做他的工作。
为什么我的牡丹拒绝给珍妮丝喝咖啡?
最佳答案
当前可以在使用Web Workers的MDN文档的 Spawning Subworkers 子节中找到我的问题的答案。
最终,我遇到的问题根源在于这样一个事实,即我的奴才 worker 试图生成其peon时,它试图引用与文档根目录相关的文件,而不是从 worker 的上下文中引用,但是没有输出指示文件不存在的控制台。
为了说明这个简单的例子:
从驱动程序
<script type="application/javascript">
var slave_driver = (
function(){
let minion = new Worker('/Features/Work/minion.js');
let crackTheWhip = ()=>
{
minion.postMessage(
JSON.stringify({SERVICE: "get"})
);
};
let take_the_credit = (message)=>
{
console.log("It's about time");
alert(message.data);
};
minion.onmessage = take_the_credit;
return {
GetJaniceHerCoffee: crackTheWhip
}
}
)();
slave_driver.GetJaniceHerCoffee();
</script>
奴才
./Features/Work/Work.Minion.js
(
function(){
var self = this;
let home = location;
let peon = new Worker('/peon.js');
let receiveWorkOrder = (message)=>
{
console.log("Delegating to a Peon");
peon.postMessage(
message.data
);
}
let take_the_credit = (message)=>
{
console.log("It's about time!");
console.log("Minion Taking the Credit");
let work = JSON.parse(message.data);
self.postMessage(JSON.stringify(work));
};
peon.onmessage = take_the_credit;
self.onmessage = receiveWorkOrder;
}
)();
Peon
./Features/Work/Work.Peon.js
(
function(){
var self = this;
let receiveWorkOrder = (message)=>
{
console.log("Peon surfing stackoverflow");
console.log("Peon surfing ebay for beanie babies, cuz they're coming back, yo");
console.log("...wth is this? *sigh*");
let work_result = {Value: "Coffee"};
self.postMessage(JSON.stringify(
work_result
));
};
self.onmessage = receiveWorkOrder;
})();
请注意,当小仆生成牡丹时,对文件引用进行的微小更改。
如果将“slavedriver”脚本放入index.html文件的主体中,并将相关模块放入其标识的位置,则打开索引时应收到显示
{"Value":"Coffee"}
的警报。