1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
let ports = 0; self.onconnect = e => { ++ports; e.ports[0].onmessage = event => { if (event.data === "count") { e.ports[0].postMessage(ports); return; } if (event.data === "close") { self.close(); return; } // Error. e.ports[0].postMessage(-1); }; };