最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why does the SharedWorker onConnect event have a Ports array? - Stack Overflow

programmeradmin1浏览0评论

In all examples I've seen, they're similar to this

onconnect = function(e) {
    var port = e.ports[0];

    port.onmessage = function(e) {
        var workerResult = 'Result: ' + (e.data[0] * e.data[1]);
        port.postMessage(workerResult);
    }

    port.start();
}

Is there an instance where the ports array will ever have more than one element? Using chrome://inspect on the SharedWorker and printing out e, I get

regardless of how many instances are spawned sharing the SharedWorker, where the length is always 1. Why isn't it just a MessageEvent instead of an array? What use case is there for it to be an array?

In all examples I've seen, they're similar to this

onconnect = function(e) {
    var port = e.ports[0];

    port.onmessage = function(e) {
        var workerResult = 'Result: ' + (e.data[0] * e.data[1]);
        port.postMessage(workerResult);
    }

    port.start();
}

Is there an instance where the ports array will ever have more than one element? Using chrome://inspect on the SharedWorker and printing out e, I get

regardless of how many instances are spawned sharing the SharedWorker, where the length is always 1. Why isn't it just a MessageEvent instead of an array? What use case is there for it to be an array?

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Sep 6, 2016 at 7:43 No_nameNo_name 2,8203 gold badges34 silver badges49 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

The reason is that it reuses the MessageEvent interface which can sometimes be dispatched with an array of multiple ports. That's all.

发布评论

评论列表(0)

  1. 暂无评论