/* eslint-disable no-restricted-globals */
import SparkMD5 from 'spark-md5';
self.addEventListener('message', function (e) {
  var chunksArr = e.data.chunksArr; // 收到主线程传递来的文件分片
  console.log('监听\'message\'收到主线程传递来的文件分片', chunksArr);
  var spark = new SparkMD5.ArrayBuffer();
  var fileReader = new FileReader();
  fileReader.readAsArrayBuffer(chunksArr.pop());
  console.log('创建文件读取', fileReader);
  fileReader.onload = function (e) {
    spark.append(e.target.result);
    if (chunksArr.length === 0) {
      self.postMessage({
        hash: spark.end()
      });
    } else {
      fileReader.readAsArrayBuffer(chunksArr.pop());
    }
  };
}, false);