Vault.#instances.push(this)
}
- prioritize<T extends Data> (data: NamedData): Promise<NamedData<T>> {
- return this.#assign<T>(data, task => this.#queue.unshift(task))
- }
-
request<T extends Data> (data: NamedData): Promise<NamedData<T>> {
- return this.#assign<T>(data, task => this.#queue.push(task))
- }
-
- terminate (): void {
- this.#job = undefined
- this.#worker.terminate()
- this.#isTerminated = true
- }
-
- #assign<T extends Data> (data: NamedData, enqueue: (task: Task) => number): Promise<NamedData<T>> {
return new Promise((resolve, reject): void => {
if (this.#isTerminated) {
- reject('Worker terminated')
+ return reject('Worker terminated')
}
const task: Task = {
id: performance.now(),
resolve,
reject
}
- enqueue(task)
+ this.#queue.push(task)
if (this.#isIdle) this.#process()
})
}
+ terminate (): void {
+ this.#job = undefined
+ this.#worker.terminate()
+ this.#isTerminated = true
+ }
+
#process = (): void => {
this.#job = this.#queue.shift()
this.#isIdle = this.#job == null