Skip to content

Commit

Permalink
Merge pull request #113 from duanyao/config_back_comp
Browse files Browse the repository at this point in the history
Add z-worker.js to configuration options and resolve worker scripts against base URI
  • Loading branch information
Rob--W committed Dec 24, 2014
2 parents c9039af + 7a4949d commit bfd76c6
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 18 deletions.
17 changes: 11 additions & 6 deletions WebContent/tests/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@

zip.useWebWorkers = true;

// to test third party deflate implementations, comment out 'zip.workerScriptsPath =...' and uncomment 'zip.workerScripts = ...'
zip.workerScriptsPath = '../';

/*
zip.workerScripts = {
// default zip.js implementation
deflater: ['deflate.js'],
inflater: ['inflate.js'],
deflater: ['../z-worker.js', '../deflate.js'],
inflater: ['../z-worker.js', '../inflate.js'],
// zlib-asm
// deflater: ['zlib-asm/zlib.js', 'zlib-asm/codecs.js'],
// inflater: ['zlib-asm/zlib.js', 'zlib-asm/codecs.js'],
// deflater: ['../z-worker.js', '../zlib-asm/zlib.js', '../zlib-asm/codecs.js'],
// inflater: ['../z-worker.js', '../zlib-asm/zlib.js', '../zlib-asm/codecs.js'],
// pako
// deflater: ['pako/pako.min.js', 'pako/codecs.js'],
// inflater: ['pako/pako.min.js', 'pako/codecs.js'],
// deflater: ['../z-worker.js', '../pako/pako.min.js', '../pako/codecs.js'],
// inflater: ['../z-worker.js', '../pako/pako.min.js', '../pako/codecs.js'],
};
*/
4 changes: 4 additions & 0 deletions WebContent/z-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
(function main(global) {
"use strict";

if (global.zWorkerInitialized)
throw new Error('z-worker.js should be run only once');
global.zWorkerInitialized = true;

addEventListener("message", function(event) {
var message = event.data, type = message.type, sn = message.sn;
var handler = handlers[type];
Expand Down
69 changes: 57 additions & 12 deletions WebContent/zip.js
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@
if (!obj.zip.useWebWorkers)
callback(zipReader);
else {
createWorker(obj.zip.workerScripts.inflater,
createWorker('inflater',
function(worker) {
zipReader._worker = worker;
callback(zipReader);
Expand Down Expand Up @@ -842,7 +842,7 @@
if (!obj.zip.useWebWorkers)
callback(zipWriter);
else {
createWorker(obj.zip.workerScripts.deflater,
createWorker('deflater',
function(worker) {
zipWriter._worker = worker;
callback(zipWriter);
Expand All @@ -854,8 +854,36 @@
}
}

function createWorker(scripts, callback, onerror) {
var worker = new Worker(obj.zip.workerScriptsPath + 'z-worker.js');
function resolveURLs(urls) {
var a = document.createElement('a');
return urls.map(function(url) {
a.href = url;
return a.href;
});
}

var DEFAULT_WORKER_SCRIPTS = {
deflater: ['z-worker.js', 'deflate.js'],
inflater: ['z-worker.js', 'inflate.js']
};
function createWorker(type, callback, onerror) {
if (obj.zip.workerScripts !== null && obj.zip.workerScriptsPath !== null) {
onerror(new Error('Either zip.workerScripts or zip.workerScriptsPath may be set, not both.'));
return;
}
var scripts;
if (obj.zip.workerScripts) {
scripts = obj.zip.workerScripts[type];
if (!Array.isArray(scripts)) {
onerror(new Error('zip.workerScripts.' + type + ' is not an array!'));
return;
}
scripts = resolveURLs(scripts);
} else {
scripts = DEFAULT_WORKER_SCRIPTS[type].slice(0);
scripts[0] = (obj.zip.workerScriptsPath || '') + scripts[0];
}
var worker = new Worker(scripts[0]);
// record total consumed time by inflater/deflater/crc32 in this worker
worker.codecTime = worker.crcTime = 0;
worker.postMessage({ type: 'importScripts', scripts: scripts.slice(1) });
Expand All @@ -869,9 +897,16 @@
}
if (msg.type === 'importScripts') {
worker.removeEventListener('message', onmessage);
worker.removeEventListener('error', errorHandler);
callback(worker);
}
}
// catch entry script loading error and other unhandled errors
worker.addEventListener('error', errorHandler);
function errorHandler(err) {
worker.terminate();
onerror(err);
}
}

function onerror_default(error) {
Expand Down Expand Up @@ -901,14 +936,24 @@
createZipWriter(writer, callback, onerror, dontDeflate);
}, onerror);
},
// Path to the directory containing z-worker.js (defaults to location of this script).
workerScriptsPath: '',
// Scripts to be loaded in the Web Worker using importScripts(). These are resolved relative to z-worker.js
workerScripts : {
deflater: ['deflate.js'],
inflater: ['inflate.js']
},
useWebWorkers : true
useWebWorkers : true,
/**
* Directory containing the default worker scripts (z-worker.js, deflate.js, and inflate.js), relative to current base url.
* E.g.: zip.workerScripts = './';
*/
workerScriptsPath : null,
/**
* Advanced option to control which scripts are loaded in the Web worker. If this option is specified, then workerScriptsPath must not be set.
* workerScripts.deflater/workerScripts.inflater should be arrays of urls to scripts for deflater/inflater, respectively.
* Scripts in the array are executed in order, and the first one should be z-worker.js, which is used to start the worker.
* All urls are relative to current base url.
* E.g.:
* zip.workerScripts = {
* deflater: ['z-worker.js', 'deflate.js'],
* inflater: ['z-worker.js', 'inflate.js']
* };
*/
workerScripts : null,
};

})(this);

0 comments on commit bfd76c6

Please sign in to comment.