2023-07-27 07:31:52 +02:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-05-19 02:44:06 +02:00
|
|
|
function defaultUseWorkerNumber(prev: number, totalWorkers: number) {
|
2023-07-27 07:31:52 +02:00
|
|
|
return prev + 1;
|
2023-05-19 02:44:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export class WorkerMultiDispatch<POST = any, RETURN = any> {
|
2023-07-27 07:31:52 +02:00
|
|
|
private symbol = Symbol('WorkerMultiDispatch');
|
|
|
|
private workers: Worker[] = [];
|
|
|
|
private terminated = false;
|
|
|
|
private prevWorkerNumber = 0;
|
|
|
|
private getUseWorkerNumber = defaultUseWorkerNumber;
|
|
|
|
private finalizationRegistry: FinalizationRegistry<symbol>;
|
2023-05-19 02:44:06 +02:00
|
|
|
|
2023-07-27 07:31:52 +02:00
|
|
|
constructor(workerConstructor: () => Worker, concurrency: number, getUseWorkerNumber = defaultUseWorkerNumber) {
|
|
|
|
this.getUseWorkerNumber = getUseWorkerNumber;
|
|
|
|
for (let i = 0; i < concurrency; i++) {
|
|
|
|
this.workers.push(workerConstructor());
|
|
|
|
}
|
2023-05-19 02:44:06 +02:00
|
|
|
|
2023-07-27 07:31:52 +02:00
|
|
|
this.finalizationRegistry = new FinalizationRegistry(() => {
|
|
|
|
this.terminate();
|
|
|
|
});
|
|
|
|
this.finalizationRegistry.register(this, this.symbol);
|
2023-05-19 02:44:06 +02:00
|
|
|
|
2023-07-27 07:31:52 +02:00
|
|
|
if (_DEV_) console.log('WorkerMultiDispatch: Created', this);
|
|
|
|
}
|
2023-05-19 02:44:06 +02:00
|
|
|
|
2023-07-27 07:31:52 +02:00
|
|
|
public postMessage(message: POST, options?: Transferable[] | StructuredSerializeOptions, useWorkerNumber: typeof defaultUseWorkerNumber = this.getUseWorkerNumber) {
|
|
|
|
let workerNumber = useWorkerNumber(this.prevWorkerNumber, this.workers.length);
|
|
|
|
workerNumber = Math.abs(Math.round(workerNumber)) % this.workers.length;
|
|
|
|
if (_DEV_) console.log('WorkerMultiDispatch: Posting message to worker', workerNumber, useWorkerNumber);
|
|
|
|
this.prevWorkerNumber = workerNumber;
|
2023-05-19 02:44:06 +02:00
|
|
|
|
2023-07-27 07:31:52 +02:00
|
|
|
// 不毛だがunionをoverloadに突っ込めない
|
|
|
|
// https://stackoverflow.com/questions/66507585/overload-signatures-union-types-and-no-overload-matches-this-call-error
|
|
|
|
// https://github.com/microsoft/TypeScript/issues/14107
|
|
|
|
if (Array.isArray(options)) {
|
|
|
|
this.workers[workerNumber].postMessage(message, options);
|
|
|
|
} else {
|
|
|
|
this.workers[workerNumber].postMessage(message, options);
|
|
|
|
}
|
|
|
|
return workerNumber;
|
|
|
|
}
|
2023-05-19 02:44:06 +02:00
|
|
|
|
2023-07-27 07:31:52 +02:00
|
|
|
public addListener(callback: (this: Worker, ev: MessageEvent<RETURN>) => any, options?: boolean | AddEventListenerOptions) {
|
|
|
|
this.workers.forEach(worker => {
|
|
|
|
worker.addEventListener('message', callback, options);
|
|
|
|
});
|
|
|
|
}
|
2023-05-19 02:44:06 +02:00
|
|
|
|
2023-07-27 07:31:52 +02:00
|
|
|
public removeListener(callback: (this: Worker, ev: MessageEvent<RETURN>) => any, options?: boolean | AddEventListenerOptions) {
|
|
|
|
this.workers.forEach(worker => {
|
|
|
|
worker.removeEventListener('message', callback, options);
|
|
|
|
});
|
|
|
|
}
|
2023-05-19 02:44:06 +02:00
|
|
|
|
2023-07-27 07:31:52 +02:00
|
|
|
public terminate() {
|
|
|
|
this.terminated = true;
|
|
|
|
if (_DEV_) console.log('WorkerMultiDispatch: Terminating', this);
|
|
|
|
this.workers.forEach(worker => {
|
|
|
|
worker.terminate();
|
|
|
|
});
|
|
|
|
this.workers = [];
|
|
|
|
this.finalizationRegistry.unregister(this);
|
|
|
|
}
|
2023-05-19 02:44:06 +02:00
|
|
|
|
2023-07-27 07:31:52 +02:00
|
|
|
public isTerminated() {
|
|
|
|
return this.terminated;
|
|
|
|
}
|
2023-10-30 11:38:27 +01:00
|
|
|
|
2023-07-27 07:31:52 +02:00
|
|
|
public getWorkers() {
|
|
|
|
return this.workers;
|
|
|
|
}
|
2023-10-30 11:38:27 +01:00
|
|
|
|
2023-07-27 07:31:52 +02:00
|
|
|
public getSymbol() {
|
|
|
|
return this.symbol;
|
|
|
|
}
|
2023-05-19 02:44:06 +02:00
|
|
|
}
|