js-crc32/demo/worker.flow.js

70 lines
2.1 KiB
JavaScript
Raw Permalink Normal View History

2017-04-27 21:29:43 +00:00
/*jshint browser:true */
function lpad(s/*:string*/, len/*:number*/, chr/*:?string*/)/*:string*/{
var L/*:number*/ = len - s.length, C/*:string*/ = chr || " ";
if(L <= 0) return s;
return new Array(L+1).join(C) + s;
}
function is_defined(val/*:any*/, keys/*:Array<string>*/)/*:boolean*/ {
if(typeof val === "undefined") return false;
return keys.length === 0 || is_defined(val[keys[0]], keys.slice(1));
}
/*## Process Result */
/*:: declare class HTMLPreElement extends HTMLElement { innerText?:string; } */
function process_value(val/*:CRC32Type*/, progress/*:number*/) {
var output = [];
output[0] = "Progress : %" + lpad(progress.toFixed(2), 6, " ");
output[1] = "Signed : " + val;
output[2] = "Unsigned : " + (val>>>0);
output[3] = "Hex value : " + lpad((val>>>0).toString(16),8,'0');
var out/*:HTMLPreElement*/ = (document.getElementById('out')/*:any*/);
var o = output.join("\n");
if(typeof out.innerText == "undefined") out.textContent = o;
else out.innerText = o;
}
2017-06-12 20:12:23 +00:00
/*::
type WMessage = {
t:string;
f:File;
crc:CRC32Type;
l:number;
sz:number;
bytes:number;
};
*/
2017-04-27 21:29:43 +00:00
/*# Drag and Drop File */
var handle_drop/*:EventHandler*/ = (function(e/*:DragEvent*/) {
e.stopPropagation();
e.preventDefault();
if(!e.dataTransfer) return;
var files/*:FileList*/ = e.dataTransfer.files;
var f/*:File*/ = files[0];
2017-06-12 20:12:23 +00:00
var worker = new Worker("demo/work.js");
2017-04-27 21:29:43 +00:00
worker.postMessage(f);
2017-06-12 20:12:23 +00:00
worker.onmessage = function(M) { var m/*:WMessage*/ = (M.data/*:any*/); switch(m.t) {
2017-04-27 21:29:43 +00:00
case 'ready': break;
case 'start': break;
case 'data': process_value(m.crc, 100 * m.bytes / f.size); break;
case 'done': break;
} };
}/*:any*/);
var handle_drag/*:EventHandler*/ = (function (e/*:DragEvent*/) {
e.stopPropagation();
e.preventDefault();
if(e.dataTransfer) e.dataTransfer.dropEffect = 'copy';
}/*:any*/);
var drop/*:HTMLDivElement*/ = (document.getElementById('drop')/*:any*/);
if(drop.addEventListener) {
drop.addEventListener('dragenter', handle_drag, false);
drop.addEventListener('dragover', handle_drag, false);
drop.addEventListener('drop', handle_drop, false);
}