Permalink
Browse files

Worker onmessage / onconnect methods should be declared in barren glo…

…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 256a5c4d274b75fa0b35532319d3d893b204dbca
Showing with 25 additions and 12 deletions.
  1. +12 −6 rawdeflate.js
  2. +13 −6 test.html
View
@@ -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
@@ -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));
+ };
+};
View
@@ -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;
@@ -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) });

0 comments on commit 256a5c4

Please sign in to comment.