2022-03-05 04:53:36 +00:00
|
|
|
import { createContext } from "react";
|
|
|
|
|
2022-03-05 04:49:14 +00:00
|
|
|
import {
|
2022-08-18 03:19:55 +00:00
|
|
|
addParsedPluginInOrder,
|
2022-03-05 04:49:14 +00:00
|
|
|
decrementPending,
|
|
|
|
PluginFile,
|
|
|
|
} from "../slices/plugins";
|
|
|
|
import store from "./store";
|
|
|
|
|
|
|
|
export interface Task {
|
|
|
|
skipParsing: boolean,
|
|
|
|
filename: string,
|
|
|
|
lastModified: number,
|
|
|
|
contents: Uint8Array,
|
|
|
|
}
|
|
|
|
|
|
|
|
export class WorkerPool {
|
|
|
|
public taskQueue: Task[];
|
|
|
|
public availableWorkers: Worker[];
|
|
|
|
|
|
|
|
public constructor() {
|
|
|
|
this.taskQueue = [];
|
|
|
|
this.availableWorkers = [];
|
|
|
|
}
|
|
|
|
|
2022-03-05 04:56:36 +00:00
|
|
|
public async init(count: number = window.navigator.hardwareConcurrency ?? 8): Promise<WorkerPool> {
|
2022-03-05 04:49:14 +00:00
|
|
|
this.availableWorkers = [];
|
|
|
|
for (let i = 0; i < count; i++) {
|
2022-03-19 05:20:24 +00:00
|
|
|
this.createWorker().then(worker => this.availableWorkers.push(worker));
|
2022-03-05 04:49:14 +00:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async addWorker() {
|
|
|
|
const worker = await this.createWorker();
|
|
|
|
this.availableWorkers.push(worker);
|
|
|
|
this.assignWorker();
|
|
|
|
}
|
|
|
|
|
|
|
|
public async createWorker(): Promise<Worker> {
|
|
|
|
return new Promise((resolve) => {
|
2022-03-19 05:15:25 +00:00
|
|
|
const worker = new Worker(new URL("../workers/PluginsLoader.worker.ts", import.meta.url));
|
2022-03-05 04:49:14 +00:00
|
|
|
worker.onmessage = (evt: {
|
2022-03-12 19:00:40 +00:00
|
|
|
data: string | PluginFile;
|
2022-03-05 04:49:14 +00:00
|
|
|
}) => {
|
|
|
|
const { data } = evt;
|
|
|
|
if (typeof data === "string" && data === "ready") {
|
|
|
|
resolve(worker);
|
|
|
|
} else if (typeof data !== "string") {
|
|
|
|
store.dispatch(decrementPending(1));
|
2022-08-18 03:19:55 +00:00
|
|
|
store.dispatch(addParsedPluginInOrder(data));
|
2022-03-05 04:49:14 +00:00
|
|
|
// Since web assembly memory cannot be shrunk, replace worker with a fresh one to avoid slow repeated
|
|
|
|
// invocations on the same worker instance. Repeated invocations are so slow that the delay in creating a
|
|
|
|
// new worker is worth it. In practice, there are usually more workers than tasks, so the delay does not slow
|
|
|
|
// down processing.
|
|
|
|
worker.terminate();
|
|
|
|
this.addWorker();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public pushTask(task: Task) {
|
|
|
|
this.taskQueue.push(task);
|
|
|
|
this.assignWorker();
|
|
|
|
}
|
|
|
|
|
|
|
|
public async assignWorker() {
|
|
|
|
if (this.taskQueue.length > 0 && this.availableWorkers.length > 0) {
|
|
|
|
const task = this.taskQueue.shift()!;
|
|
|
|
const worker = this.availableWorkers.shift()!;
|
|
|
|
worker.postMessage(task, [task.contents.buffer]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public async terminateAll() {
|
|
|
|
for (const worker of this.availableWorkers) {
|
|
|
|
worker.terminate();
|
|
|
|
}
|
|
|
|
this.availableWorkers = [];
|
|
|
|
}
|
2022-03-05 04:53:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const WorkerPoolContext = createContext<WorkerPool | null>(null);
|