From 9afd41182f389e4f83e5e57b02c4d576b90bf010 Mon Sep 17 00:00:00 2001 From: gustavo Date: Tue, 17 May 2016 02:11:02 +0200 Subject: [PATCH] Shared Workers improvements --- ecma6/sharedworkers/main.js | 8 ++++---- ecma6/sharedworkers/shared.js | 35 +++++++++++++++++++++++++++++------ 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/ecma6/sharedworkers/main.js b/ecma6/sharedworkers/main.js index 5c70ade..5c3687e 100644 --- a/ecma6/sharedworkers/main.js +++ b/ecma6/sharedworkers/main.js @@ -10,12 +10,12 @@ // Shared worker will be released once every tab using that worker is closed. // see: chrome://inspect/#workers var sharedWorker = new SharedWorker('shared.js', 'shared-worker-example'); + sharedWorker.onerror = onError; var sendButton = document.getElementById('sendButton'); sendButton.addEventListener('click', postMessage); messagePort = sharedWorker.port; - messagePort.onerror = onError; messagePort.onmessage = onMessage; // Starts the sending of messages queued on the port // (only needed when using EventTarget.addEventListener; it is implied when using MessagePort.onmessage.) @@ -30,8 +30,8 @@ log.insertAdjacentHTML('beforeend', 'Message posted to worker: ' + sendText.value + '
'); } - function onMessage(e) { - log.insertAdjacentHTML('beforeend', 'Message received from worker: ' + e.data + '
'); + function onMessage(event) { + log.insertAdjacentHTML('beforeend', 'Message received from worker: ' + event.data + '
'); } function onError() { @@ -39,7 +39,7 @@ } function close() { - // I think, calling close method is not required. Once all tabs are closed, shared worker will be released. + // When should I call close method? messagePort.close(); } diff --git a/ecma6/sharedworkers/shared.js b/ecma6/sharedworkers/shared.js index 2aae397..fa4005f 100644 --- a/ecma6/sharedworkers/shared.js +++ b/ecma6/sharedworkers/shared.js @@ -1,14 +1,37 @@ -onconnect = function(e) { - var port = e.ports[0]; +'use strict'; - port.onmessage = function(e) { - var workerResult = 'Result: ' + e.data; +var lastData; +var messagePorts = []; + +onconnect = function(event) { + var messagePort = event.ports[0]; + messagePort.onmessage = function(event) { + + var workerResult; + if (lastData) { + workerResult = 'Result: ' + event.data + ' Last data: ' + lastData; + } else { + workerResult = 'Result: ' + event.data; + } + lastData = event.data; console.log('SharedWorkerGlobalScope name: ' + name); console.log('SharedWorkerGlobalScope location: ' + location); + console.log('SharedWorkerGlobalScope result: ' + event.data); - // Data will be sent only to the current active tab. - port.postMessage(workerResult); + messagePorts.forEach(function (messagePort) { + messagePort.postMessage(workerResult); + }); }; + messagePorts.push(messagePort); + + // When and where should I call close method? + // Should I call messagePort.close() or SharedWorkerGlobalScope.close()? + // SharedWorker doesn't have close method but SharedWorkerGlobalScope has one :( + +}; + +onerror = function() { + console.log('SharedWorkerGlobalScope: There is an error with the shared worker!'); }; -- 2.1.4