Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the SharedWorker onConnect event have a Ports 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

MessageEvent

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?

like image 356
No_name Avatar asked Sep 06 '16 07:09

No_name


1 Answers

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

like image 126
Anne Avatar answered Sep 28 '22 05:09

Anne