Skip to content

Commit

Permalink
Worker onmessage / onconnect methods should be declared in barren glo…
Browse files Browse the repository at this point in the history
…bal scope. Implemented both SharedWorker and Worker interfaces.
  • Loading branch information
Johan Sundström authored and johan committed Jun 29, 2010
1 parent eefdd31 commit 256a5c4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
18 changes: 12 additions & 6 deletions rawdeflate.js
Expand Up @@ -6,12 +6,7 @@
*/

// if run as a web worker, respond to messages by deflating them
var onconnect = function(e) {
var port = e.ports[0];
port.onmessage = function(m) {
port.postMessage(deflate(m.data, 9));
};
}, deflate = (function() {
var deflate = (function() {

/* Copyright (C) 1999 Masanao Izumo <iz@onicos.co.jp>
* Version: 1.0.1
Expand Down Expand Up @@ -1671,3 +1666,14 @@ return function deflate(str, level) {
};

})();

onmessage = function worker(m) {
postMessage(deflate(m.data, 9));
};

onconnect = function sharedWorker(e) {
var port = e.ports[0];
port.onmessage = function(m) {
port.postMessage(deflate(m.data, 9));
};
};
19 changes: 13 additions & 6 deletions test.html
Expand Up @@ -17,10 +17,13 @@
decode: function(s) { return atob(s); }
};

var shared_deflater = window.SharedWorker && new SharedWorker('rawdeflate.js');
if (shared_deflater) {
shared_deflater.port.addEventListener('message', done_deflating, false);
shared_deflater.port.start();
var deflater = window.SharedWorker && new SharedWorker('rawdeflate.js');
if (deflater) {
deflater.port.addEventListener('message', done_deflating, false);
deflater.port.start();
} else if (window.Worker) {
deflater = new Worker('rawdeflate.js');
deflater.onmessage = done_deflating;
}

var t0;
Expand All @@ -34,8 +37,12 @@
s = UTF8.encode(s);

t0 = new Date;
if (shared_deflater) {
shared_deflater.port.postMessage(s);
if (deflater) {
if (deflater.port && deflater.port.postMessage) {
deflater.port.postMessage(s);
} else {
deflater.postMessage(s);
}
} else {
setTimeout(function() {
done_deflating({ data: deflate(s) });
Expand Down

0 comments on commit 256a5c4

Please sign in to comment.