file-worker
Advanced tools
Comparing version 0.0.10 to 0.0.11
@@ -100,5 +100,20 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
workerFunctionString = workerFunctionString.replace('__PROCESSOR__', processor.toString()); | ||
var blobURL = URL.createObjectURL(new Blob(["(" + workerFunctionString + ")()"], { type: 'application/javascript' })); | ||
workerFunctionString = "(" + workerFunctionString + ")()"; | ||
// http://stackoverflow.com/questions/10343913/how-to-create-a-web-worker-from-a-string | ||
var blob; | ||
try { | ||
blob = new Blob([workerFunctionString], { type: 'application/javascript' }); | ||
} | ||
catch (e) { | ||
// Backwards-compatibility | ||
window.BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder; | ||
blob = new BlobBuilder(); | ||
blob.append(workerFunctionString); | ||
blob = blob.getBlob(); | ||
} | ||
window.URL = window.URL || window.webkitURL; // Backwards-compatibility | ||
var blobURL = URL.createObjectURL(blob); | ||
var worker = new Worker(blobURL); | ||
URL.revokeObjectURL(blobURL); | ||
// Do not revoke object URL. otherwise IE and edge doesn't work. | ||
// URL.revokeObjectURL(blobURL); | ||
return worker; | ||
@@ -118,2 +133,3 @@ } | ||
return __generator(this, function (_a) { | ||
console.log(file); | ||
result = new Promise(function (resolve, reject) { | ||
@@ -120,0 +136,0 @@ var worker = WebWorkerHelper.createWebWorker(processor); |
@@ -186,5 +186,20 @@ ;(function(global) { | ||
workerFunctionString = workerFunctionString.replace('__PROCESSOR__', processor.toString()); | ||
var blobURL = URL.createObjectURL(new Blob(['(' + workerFunctionString + ')()'], { type: 'application/javascript' })); | ||
workerFunctionString = '(' + workerFunctionString + ')()'; | ||
// http://stackoverflow.com/questions/10343913/how-to-create-a-web-worker-from-a-string | ||
var blob; | ||
try { | ||
blob = new Blob([workerFunctionString], { type: 'application/javascript' }); | ||
} catch (e) { | ||
// Backwards-compatibility | ||
window.BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder; | ||
blob = new BlobBuilder(); | ||
blob.append(workerFunctionString); | ||
blob = blob.getBlob(); | ||
} | ||
window.URL = window.URL || window.webkitURL; | ||
// Backwards-compatibility | ||
var blobURL = URL.createObjectURL(blob); | ||
var worker = new Worker(blobURL); | ||
URL.revokeObjectURL(blobURL); | ||
// Do not revoke object URL. otherwise IE and edge doesn't work. | ||
// URL.revokeObjectURL(blobURL); | ||
return worker; | ||
@@ -204,2 +219,3 @@ } | ||
return __generator(this, function (_a) { | ||
console.log(file); | ||
result = new Promise(function (resolve, reject) { | ||
@@ -206,0 +222,0 @@ var worker = WebWorkerHelper.createWebWorker(processor); |
@@ -1,1 +0,1 @@ | ||
!function(e){var t={},r={},n={},i={},o=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))(function(i,o){function a(e){try{s(n.next(e))}catch(e){o(e)}}function u(e){try{s(n.throw(e))}catch(e){o(e)}}function s(e){e.done?i(e.value):new r(function(t){t(e.value)}).then(a,u)}s((n=n.apply(e,t||[])).next())})},a=this&&this.__generator||function(e,t){function r(e){return function(t){return n([e,t])}}function n(r){if(i)throw new TypeError("Generator is already executing.");for(;u;)try{if(i=1,o&&(a=o[2&r[0]?"return":r[0]?"throw":"next"])&&!(a=a.call(o,r[1])).done)return a;switch(o=0,a&&(r=[0,a.value]),r[0]){case 0:case 1:a=r;break;case 4:return u.label++,{value:r[1],done:!1};case 5:u.label++,o=r[1],r=[0];continue;case 7:r=u.ops.pop(),u.trys.pop();continue;default:if(a=u.trys,!(a=a.length>0&&a[a.length-1])&&(6===r[0]||2===r[0])){u=0;continue}if(3===r[0]&&(!a||r[1]>a[0]&&r[1]<a[3])){u.label=r[1];break}if(6===r[0]&&u.label<a[1]){u.label=a[1],a=r;break}if(a&&u.label<a[2]){u.label=a[2],u.ops.push(r);break}a[2]&&u.ops.pop(),u.trys.pop();continue}r=t.call(e,u)}catch(e){r=[6,e],o=0}finally{i=a=0}if(5&r[0])throw r[1];return{value:r[0]?r[1]:void 0,done:!0}}var i,o,a,u={label:0,sent:function(){if(1&a[0])throw a[1];return a[1]},trys:[],ops:[]};return{next:r(0),throw:r(1),return:r(2)}};t=function(e){return Object.defineProperty(e,"__esModule",{value:!0}),e}(t),r=function(e){function t(){var e=__PROCESSOR__,t=function(){function e(e){this.file=e,this.currentReader=0}return e.prototype.read=function(e){var t=Math.min(this.file.size,this.currentReader+e);if(this.currentReader==this.file.size)this.onFinished();else{var r=this.file.slice(this.currentReader,t);this.currentReader=t;var n=new FileReaderSync,i=n.readAsArrayBuffer(r);this.onData(i),this.currentReader==this.file.size&&this.onFinished()}},e.prototype.readAll=function(){var e=new FileReaderSync,t=e.readAsArrayBuffer(this.file);this.onData(t),this.onFinished()},e}();self.addEventListener("message",function(r){var n=r.data,i=[],o=new t(n),a={write:function(e){i.push(e)},writeOnce:function(e){self.postMessage(e)},finish:function(){self.postMessage(i)}};try{e(o,a)}catch(e){self.postMessage({error:e.toString()})}})}function r(e){var r=t.toString();r=r.replace("__PROCESSOR__",e.toString());var n=URL.createObjectURL(new Blob(["("+r+")()"],{type:"application/javascript"})),i=new Worker(n);return URL.revokeObjectURL(n),i}return Object.defineProperty(e,"__esModule",{value:!0}),e.createWebWorker=r,e}(r),n=function(e,t){Object.defineProperty(e,"__esModule",{value:!0});var r=function(){function e(){}return e.readFile=function(e,r){return o(this,void 0,void 0,function(){var n;return a(this,function(i){return n=new Promise(function(n,i){var o=t.createWebWorker(r);o.postMessage(e),o.onmessage=function(e){n(e.data)}}),[2,n]})})},e}();return e.FileWorker=r,e}(n,r),i=function(e,t){return Object.defineProperty(e,"__esModule",{value:!0}),e.default=t.FileWorker,e}(i,n),e.FileWorker=i.default}(window); | ||
!function(e){var t={},n={},r={},i={},o=this&&this.__awaiter||function(e,t,n,r){return new(n||(n=Promise))(function(i,o){function a(e){try{s(r.next(e))}catch(e){o(e)}}function u(e){try{s(r.throw(e))}catch(e){o(e)}}function s(e){e.done?i(e.value):new n(function(t){t(e.value)}).then(a,u)}s((r=r.apply(e,t||[])).next())})},a=this&&this.__generator||function(e,t){function n(e){return function(t){return r([e,t])}}function r(n){if(i)throw new TypeError("Generator is already executing.");for(;u;)try{if(i=1,o&&(a=o[2&n[0]?"return":n[0]?"throw":"next"])&&!(a=a.call(o,n[1])).done)return a;switch(o=0,a&&(n=[0,a.value]),n[0]){case 0:case 1:a=n;break;case 4:return u.label++,{value:n[1],done:!1};case 5:u.label++,o=n[1],n=[0];continue;case 7:n=u.ops.pop(),u.trys.pop();continue;default:if(a=u.trys,!(a=a.length>0&&a[a.length-1])&&(6===n[0]||2===n[0])){u=0;continue}if(3===n[0]&&(!a||n[1]>a[0]&&n[1]<a[3])){u.label=n[1];break}if(6===n[0]&&u.label<a[1]){u.label=a[1],a=n;break}if(a&&u.label<a[2]){u.label=a[2],u.ops.push(n);break}a[2]&&u.ops.pop(),u.trys.pop();continue}n=t.call(e,u)}catch(e){n=[6,e],o=0}finally{i=a=0}if(5&n[0])throw n[1];return{value:n[0]?n[1]:void 0,done:!0}}var i,o,a,u={label:0,sent:function(){if(1&a[0])throw a[1];return a[1]},trys:[],ops:[]};return{next:n(0),throw:n(1),return:n(2)}};t=function(e){return Object.defineProperty(e,"__esModule",{value:!0}),e}(t),n=function(e){function t(){var e=__PROCESSOR__,t=function(){function e(e){this.file=e,this.currentReader=0}return e.prototype.read=function(e){var t=Math.min(this.file.size,this.currentReader+e);if(this.currentReader==this.file.size)this.onFinished();else{var n=this.file.slice(this.currentReader,t);this.currentReader=t;var r=new FileReaderSync,i=r.readAsArrayBuffer(n);this.onData(i),this.currentReader==this.file.size&&this.onFinished()}},e.prototype.readAll=function(){var e=new FileReaderSync,t=e.readAsArrayBuffer(this.file);this.onData(t),this.onFinished()},e}();self.addEventListener("message",function(n){var r=n.data,i=[],o=new t(r),a={write:function(e){i.push(e)},writeOnce:function(e){self.postMessage(e)},finish:function(){self.postMessage(i)}};try{e(o,a)}catch(e){self.postMessage({error:e.toString()})}})}function n(e){var n=t.toString();n=n.replace("__PROCESSOR__",e.toString()),n="("+n+")()";var r;try{r=new Blob([n],{type:"application/javascript"})}catch(e){window.BlobBuilder=window.BlobBuilder||window.WebKitBlobBuilder||window.MozBlobBuilder,r=new BlobBuilder,r.append(n),r=r.getBlob()}window.URL=window.URL||window.webkitURL;var i=URL.createObjectURL(r);return new Worker(i)}return Object.defineProperty(e,"__esModule",{value:!0}),e.createWebWorker=n,e}(n),r=function(e,t){Object.defineProperty(e,"__esModule",{value:!0});var n=function(){function e(){}return e.readFile=function(e,n){return o(this,void 0,void 0,function(){var r;return a(this,function(i){return console.log(e),r=new Promise(function(r,i){var o=t.createWebWorker(n);o.postMessage(e),o.onmessage=function(e){r(e.data)}}),[2,r]})})},e}();return e.FileWorker=n,e}(r,n),i=function(e,t){return Object.defineProperty(e,"__esModule",{value:!0}),e.default=t.FileWorker,e}(i,r),e.FileWorker=i.default}(window); |
@@ -46,2 +46,3 @@ "use strict"; | ||
return __generator(this, function (_a) { | ||
console.log(file); | ||
result = new Promise(function (resolve, reject) { | ||
@@ -48,0 +49,0 @@ var worker = WebWorkerHelper.createWebWorker(processor); |
@@ -60,7 +60,22 @@ "use strict"; | ||
workerFunctionString = workerFunctionString.replace('__PROCESSOR__', processor.toString()); | ||
var blobURL = URL.createObjectURL(new Blob(["(" + workerFunctionString + ")()"], { type: 'application/javascript' })); | ||
workerFunctionString = "(" + workerFunctionString + ")()"; | ||
// http://stackoverflow.com/questions/10343913/how-to-create-a-web-worker-from-a-string | ||
var blob; | ||
try { | ||
blob = new Blob([workerFunctionString], { type: 'application/javascript' }); | ||
} | ||
catch (e) { | ||
// Backwards-compatibility | ||
window.BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder; | ||
blob = new BlobBuilder(); | ||
blob.append(workerFunctionString); | ||
blob = blob.getBlob(); | ||
} | ||
window.URL = window.URL || window.webkitURL; // Backwards-compatibility | ||
var blobURL = URL.createObjectURL(blob); | ||
var worker = new Worker(blobURL); | ||
URL.revokeObjectURL(blobURL); | ||
// Do not revoke object URL. otherwise IE and edge doesn't work. | ||
// URL.revokeObjectURL(blobURL); | ||
return worker; | ||
} | ||
exports.createWebWorker = createWebWorker; |
{ | ||
"name": "file-worker", | ||
"version": "0.0.10", | ||
"version": "0.0.11", | ||
"description": "Process files asynchronous at browser with web worker.", | ||
@@ -5,0 +5,0 @@ "main": "dist/commonjs/index.js", |
JavaScript File Worker | ||
====================== | ||
![JavaScript File Worker](misc/banner.png) | ||
***Process files asynchronously by Web Worker in browsers.*** | ||
@@ -5,0 +7,0 @@ |
@@ -6,2 +6,3 @@ import {FileWorkerProcessor, FileWorkerConfig} from "./types"; | ||
static async readFile(file : File, processor : FileWorkerProcessor) : Promise<any> { | ||
console.log(file); | ||
const result : Promise<any> = new Promise<any>((resolve, reject) => { | ||
@@ -8,0 +9,0 @@ const worker : Worker = WebWorkerHelper.createWebWorker(processor); |
@@ -6,2 +6,4 @@ import {FileWorkerWriter, FileWorkerProcessor, FileWorkerReader, FileWorkerOnDataHandler} from "./types"; | ||
declare const FileReaderSync : any; | ||
declare const window : any; | ||
declare const BlobBuilder : any; | ||
@@ -71,8 +73,23 @@ function workerScript() { | ||
export function createWebWorker(processor: Function) : Worker { | ||
var workerFunctionString: string = workerScript.toString(); | ||
let workerFunctionString: string = workerScript.toString(); | ||
workerFunctionString = workerFunctionString.replace('__PROCESSOR__', processor.toString()); | ||
const blobURL = URL.createObjectURL(new Blob([`(${workerFunctionString})()`], {type: 'application/javascript'})); | ||
workerFunctionString = `(${workerFunctionString})()`; | ||
// http://stackoverflow.com/questions/10343913/how-to-create-a-web-worker-from-a-string | ||
let blob; | ||
try { | ||
blob = new Blob([workerFunctionString], {type: 'application/javascript'}); | ||
} catch (e) { | ||
// Backwards-compatibility | ||
window.BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder; | ||
blob = new BlobBuilder(); | ||
blob.append(workerFunctionString); | ||
blob = blob.getBlob(); | ||
} | ||
window.URL = window.URL || window.webkitURL; // Backwards-compatibility | ||
const blobURL = URL.createObjectURL(blob); | ||
const worker = new Worker(blobURL); | ||
URL.revokeObjectURL(blobURL); | ||
// Do not revoke object URL. otherwise IE and edge doesn't work. | ||
// URL.revokeObjectURL(blobURL); | ||
return worker; | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
37153
748
122