add TaskRunner and worker utils

This commit is contained in:
Hamcha 2019-09-18 14:42:17 +02:00
parent 5b8cb75921
commit 94e1eef91e
Signed by: hamcha
GPG key ID: 44AD3571EB09A39E
6 changed files with 53 additions and 41 deletions

View file

@ -16,7 +16,7 @@
<script lang="ts"> <script lang="ts">
import { Component, Vue } from "vue-property-decorator"; import { Component, Vue } from "vue-property-decorator";
import TopNav from "@/components/Navigation/TopNav.vue"; import TopNav from "@/components/Navigation/TopNav.vue";
import DownloadWorker from "worker-loader!@/workers/tasks/downloadCardImages"; import { TaskRunner } from "@/workers";
@Component({ @Component({
components: { components: {
@ -25,13 +25,21 @@ import DownloadWorker from "worker-loader!@/workers/tasks/downloadCardImages";
}) })
export default class Lobby extends Vue { export default class Lobby extends Vue {
private mounted() { private mounted() {
/* const worker = new TaskRunner("downloadCardImages");
const worker = new DownloadWorker(); worker.on("dl-progress", progress => {
worker.addEventListener("message", (ev: MessageEvent) => { console.log(
const data = JSON.parse(ev.data); `Download progress: ${progress.progress}/${
console.log(data); progress.total
}b (${Math.round((progress.progress * 100) / progress.total)}%)`
);
});
worker.on("ex-progress", progress => {
console.log(
`Extraction progress: ${progress.progress}/${
progress.total
}b (${Math.round((progress.progress * 100) / progress.total)}%)`
);
}); });
*/
} }
} }
</script> </script>

1
src/workers/index.ts Normal file
View file

@ -0,0 +1 @@
export * from "./runner";

17
src/workers/runner.ts Normal file
View file

@ -0,0 +1,17 @@
import EventEmitter from "eventemitter3";
export class TaskRunner extends EventEmitter {
public class: any;
public instance: Worker;
constructor(taskName: string) {
super();
this.class = require(`worker-loader!@/workers/tasks/${taskName}`);
this.instance = new this.class() as Worker;
this.instance.addEventListener("error", ev => this.emit("error", ev));
this.instance.addEventListener("message", ev => {
const message = JSON.parse(ev.data);
this.emit(message.type, message.data);
});
}
}

View file

@ -1,12 +1,7 @@
import { Database, initDB } from "@/mlpccg"; import { Database, initDB } from "@/mlpccg";
import axios from "axios"; import axios from "axios";
import JSZip from "jszip"; import JSZip from "jszip";
import { send, runAsync } from "../worker-utils";
const ctx: Worker = self as any;
function send(data: object) {
ctx.postMessage(JSON.stringify(data));
}
async function downloadImages() { async function downloadImages() {
if (!Database) { if (!Database) {
@ -18,18 +13,14 @@ async function downloadImages() {
const itemcount = await table.count(); const itemcount = await table.count();
if (itemcount > 1900) { if (itemcount > 1900) {
// DB already filled, exit early // DB already filled, exit early
send({ return "already-done";
type: "svc-already-done"
});
return;
} }
const zipdata = await axios({ const zipdata = await axios({
url: "https://mcg.zyg.ovh/cards.zip", url: "https://mcg.zyg.ovh/cards.zip",
responseType: "blob", responseType: "blob",
onDownloadProgress: (progressEvent: ProgressEvent) => { onDownloadProgress: (progressEvent: ProgressEvent) => {
send({ send("dl-progress", {
type: "svc-dl-progress",
progress: progressEvent.loaded, progress: progressEvent.loaded,
total: progressEvent.total total: progressEvent.total
}); });
@ -55,41 +46,27 @@ async function downloadImages() {
cards.forEach(async (filename, filedata) => { cards.forEach(async (filename, filedata) => {
const data = await filedata.async("blob"); const data = await filedata.async("blob");
loadingState += 1; loadingState += 1;
send({ send("ex-progress", {
type: "svc-ex-progress",
progress: loadingState, progress: loadingState,
total: totalLoading total: totalLoading
}); });
const result = await table.put({ id: filename, image: data }); const result = await table.put({ id: filename, image: data });
loadingState += 1; loadingState += 1;
send({ send("ex-progress", {
type: "svc-ex-progress",
progress: loadingState, progress: loadingState,
total: totalLoading total: totalLoading
}); });
}); });
await waitgroup; await waitgroup;
return "downloaded";
} }
async function run() { runAsync(async () => {
send({
type: "svc-start"
});
try { try {
await downloadImages(); return await downloadImages();
} catch (e) { } catch (e) {
console.error(e); console.error(e);
send({ return new Error(e.message);
type: "svc-err",
error: e.message
});
return;
} }
send({
type: "svc-end"
}); });
}
run();

View file

@ -0,0 +1,9 @@
export function send(type: string, data?: any) {
const ctx: Worker = self as any;
ctx.postMessage(JSON.stringify({ type, data }));
}
export async function runAsync(fn: () => Promise<any>) {
const val = await fn();
send("finish", val);
}