Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | 111x 111x 111x 111x 111x 111x 111x 111x 2x 2x 2x 129x 128x 128x 128x 7x 7x 7x 6x 6x 7x 7x 7x 7x 7x 10x 4x 4x 1x 1x 125x 9x 9x 9x 8x 8x 8x 8x 8x 7x 8x 8x 9x 1x 1x 1x 1x 1x 1x 1x 9x 9x 9x 9x 1x 1x 118x 118x 118x 117x 117x 117x 1x 1x 139x 134x 134x 134x 124x 117x 117x 7x 10x 10x 10x 10x | import { yieldToTask } from './yield.ts';
/** A bounded, lazy pool shared by loading, audio preparation, and submission tasks. */
export interface TaskWorker {
postMessage(message: unknown): void;
terminate(): void;
onmessage: ((event: MessageEvent) => void) | null;
onerror: ((event: ErrorEvent) => void) | null;
onmessageerror: ((event: MessageEvent) => void) | null;
}
interface Job {
id: number;
name: string;
input: unknown;
fallback: () => unknown;
resolve: (value: unknown) => void;
reject: (error: unknown) => void;
}
interface Slot {
worker: TaskWorker;
job?: Job;
}
export class BrowserTaskPool {
private slots: Slot[] = [];
private queue: Job[] = [];
private nextId = 0;
private localJobs = new Set<Job>();
private unavailable = false;
private closed = false;
private size: number;
private factory: (() => TaskWorker) | undefined;
public constructor(size: number, factory?: () => TaskWorker) {
this.size = size;
this.factory = factory;
}
public resize(size: number): void {
this.size = size;
this.trim();
this.drain();
}
public run<T>(name: string, input: unknown, fallback: () => T | Promise<T>): Promise<T> {
if (this.closed) return Promise.reject(new Error('Worker pool is closed'));
return new Promise<T>((resolve, reject) => {
this.queue.push({ id: ++this.nextId, name, input, fallback, resolve: (value) => resolve(value as T), reject });
this.drain();
});
}
public dispose(): void {
this.closed = true;
const error = new Error('Worker pool is closed');
for (const slot of this.slots) {
slot.worker.terminate();
slot.job?.reject(error);
}
for (const job of this.queue) job.reject(error);
for (const job of this.localJobs) job.reject(error);
this.localJobs.clear();
this.slots = [];
this.queue = [];
}
private trim(): void {
for (let i = this.slots.length - 1; i >= 0 && this.slots.length > this.size; i--) {
const slot = this.slots[i]!;
if (!slot.job) {
slot.worker.terminate();
this.slots.splice(i, 1);
}
}
}
private create(): Slot | undefined {
if (!this.factory || this.unavailable) return undefined;
try {
const slot: Slot = { worker: this.factory() };
slot.worker.onmessage = (event) => {
const result = event.data as { id: number; value?: unknown; error?: string };
const job = slot.job;
Iif (!job || result.id !== job.id) return;
slot.job = undefined;
if (result.error !== undefined) job.reject(new Error(result.error));
else job.resolve(result.value);
this.trim();
this.drain();
};
const failed = () => {
// Infrastructure failures use the untouched input locally. Task errors arrive as messages instead.
const job = slot.job;
slot.job = undefined;
slot.worker.terminate();
this.slots = this.slots.filter((value) => value !== slot);
this.unavailable = true;
Eif (job) this.local(job);
this.drain();
};
slot.worker.onerror = failed;
slot.worker.onmessageerror = failed;
this.slots.push(slot);
return slot;
} catch {
this.unavailable = true;
return undefined;
}
}
private local(job: Job): void {
// Yield before the fallback so a loading indicator can paint, including in non-Worker hosts.
this.localJobs.add(job);
void yieldToTask()
.then(() => {
if (this.closed) throw new Error('Worker pool is closed');
return job.fallback();
})
.then(
(value) => {
this.localJobs.delete(job);
job.resolve(value);
},
(error) => {
this.localJobs.delete(job);
job.reject(error);
},
);
}
private drain(): void {
while (this.queue.length) {
let slot = this.slots.find((value) => !value.job);
if (!slot && this.slots.length < this.size) slot = this.create();
if (!slot) {
if (!this.factory || this.unavailable) {
this.local(this.queue.shift()!);
continue;
}
return;
}
const job = this.queue.shift()!;
slot.job = job;
try {
slot.worker.postMessage({ id: job.id, name: job.name, input: job.input });
} catch (error) {
slot.job = undefined;
job.reject(error);
}
}
}
}
|