forked from misskey-dev/misskey
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
92 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
function defaultUseWorkerNumber(prev: number, totalWorkers: number) { | ||
return prev + 1; | ||
} | ||
|
||
export class WorkerMultiDispatch<POST = any, RETURN = any> { | ||
private sym = Symbol('WorkerMultiDispatch'); | ||
public workers: Worker[] = []; | ||
private prevWorkerNumber = 0; | ||
private getUseWorkerNumber = defaultUseWorkerNumber; | ||
private finalizationRegistry: FinalizationRegistry<symbol>; | ||
|
||
constructor(workerConstructor: () => Worker, concurrency: number, getUseWorkerNumber = defaultUseWorkerNumber) { | ||
this.getUseWorkerNumber = getUseWorkerNumber; | ||
for (let i = 0; i < concurrency; i++) { | ||
this.workers.push(workerConstructor()); | ||
} | ||
|
||
this.finalizationRegistry = new FinalizationRegistry(() => { | ||
this.terminate(); | ||
}); | ||
this.finalizationRegistry.register(this, this.sym); | ||
|
||
if (_DEV_) console.log('WorkerMultiDispatch: Created', this); | ||
} | ||
|
||
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; | ||
|
||
// 不毛だが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; | ||
} | ||
|
||
public addListener(callback: (this: Worker, ev: MessageEvent<RETURN>) => any, options?: boolean | AddEventListenerOptions) { | ||
this.workers.forEach(worker => { | ||
worker.addEventListener('message', callback, options); | ||
}); | ||
} | ||
|
||
public removeListener(callback: (this: Worker, ev: MessageEvent<RETURN>) => any, options?: boolean | AddEventListenerOptions) { | ||
this.workers.forEach(worker => { | ||
worker.removeEventListener('message', callback, options); | ||
}); | ||
} | ||
|
||
public terminate() { | ||
this.workers.forEach(worker => { | ||
worker.terminate(); | ||
}); | ||
this.workers = []; | ||
this.finalizationRegistry.unregister(this); | ||
} | ||
} |