Browse Source

Add Web Worker

20230320
MengYX 5 years ago
parent
commit
402fb184f7
No known key found for this signature in database GPG Key ID: E63F9C7303E8F604
  1. 9
      package-lock.json
  2. 3
      package.json
  3. 71
      src/App.vue
  4. 3
      src/decrypt/common.js

9
package-lock.json

@ -11215,6 +11215,15 @@
"errno": "~0.1.7"
}
},
"workerize-loader": {
"version": "1.1.0",
"resolved": "https://registry.npm.taobao.org/workerize-loader/download/workerize-loader-1.1.0.tgz",
"integrity": "sha1-06Y0OQ3LaFzB7iks0f/+7wpkYEQ=",
"dev": true,
"requires": {
"loader-utils": "^1.2.3"
}
},
"wrap-ansi": {
"version": "6.2.0",
"resolved": "https://registry.npm.taobao.org/wrap-ansi/download/wrap-ansi-6.2.0.tgz",

3
package.json

@ -21,6 +21,7 @@
"@vue/cli-service": "^4.1.2",
"babel-plugin-component": "^1.1.1",
"vue-cli-plugin-element": "^1.0.1",
"vue-template-compiler": "^2.6.11"
"vue-template-compiler": "^2.6.11",
"workerize-loader": "^1.1.0"
}
}

71
src/App.vue

@ -85,8 +85,11 @@
</template>
<script>
//
"use strict";
const dec = require("./decrypt/common");
const worker = require("workerize-loader!./decrypt/common");
const dec = require('./decrypt/common');
export default {
name: 'app',
components: {},
@ -109,12 +112,25 @@
return this.cacheQueue.length;
}
},
workers: [],
idle_workers: [],
thread_num: 1
}
},
mounted() {
this.$nextTick(function () {
this.finishLoad();
});
if (document.location.host !== "") {
this.thread_num = Math.max(navigator.hardwareConcurrency, 1);
for (let i = 0; i < this.thread_num; i++) {
this.workers.push(worker().CommonDecrypt);
this.idle_workers.push(i);
}
} else {
this.workers.push(dec.CommonDecrypt);
this.idle_workers.push(0)
}
},
methods: {
finishLoad() {
@ -130,43 +146,25 @@
});
},
handleFile(file) {
//
if (file) {
console.log("workCount", this.workCount);
// 100
if (this.workCount < 100) {
//
this.workCount++;
// 100
this.handleDoFile(file);
}
// 100
else {
this.cacheQueueOption.push(file);
}
// worker
if (this.idle_workers.length > 0) {
this.handleDoFile(file, this.idle_workers.shift());
}
//
// worker
else {
this.workCount++;
this.handleDoFile(this.cacheQueueOption.pop());
this.cacheQueueOption.push(file);
}
},
handleCacheQueue() {
// 50
if (this.cacheQueueOption.size() > 0 && this.workCount < 50) {
this.handleFile(null);
console.log("size", this.cacheQueueOption.size(), this.workCount);
handleCacheQueue(worker_id) {
//
if (this.cacheQueue.length === 0) {
this.idle_workers.push(worker_id);
return
}
this.handleDoFile(this.cacheQueueOption.pop(), worker_id);
},
handleDoFile(file) {
(async () => {
let data = await dec.CommonDecrypt(file);
//
this.workCount--;
this.handleCacheQueue();
handleDoFile(file, worker_id) {
this.workers[worker_id](file).then(data => {
if (data.status) {
this.tableData.push(data);
this.$notify.success({
@ -186,7 +184,13 @@
});
window._paq.push(["trackEvent", "Error", data.message, file.name]);
}
})();
// todo: call stack
this.handleCacheQueue(worker_id);
}).catch(err => {
console.error(err, file);
window._paq.push(["trackEvent", "Error", err, file.name]);
this.handleCacheQueue(worker_id);
})
},
handlePlay(index, row) {
this.playing_url = row.file;
@ -250,6 +254,7 @@
font-size: small;
}
/*noinspection CssUnusedSymbol*/
.el-upload-dragger {
width: 80vw !important;
}

3
src/decrypt/common.js

@ -4,9 +4,8 @@ const RawDecrypt = require("./raw");
const MFlacDecrypt = require("./mflac");
const TmDecrypt = require("./tm");
export {CommonDecrypt}
async function CommonDecrypt(file) {
export async function CommonDecrypt(file) {
let raw_ext = file.name.substring(file.name.lastIndexOf(".") + 1, file.name.length).toLowerCase();
let raw_filename = file.name.substring(0, file.name.lastIndexOf("."));
let rt_data;

Loading…
Cancel
Save