andy-stack_vaultkeeper-ai/Helpers/Semaphore.ts
Andrew Beal 167a2b13a5 refactor: standardize error handling with Exception helper and improve return types
Add Exception helper class for consistent error handling and logging. Replace throw statements and console.error calls with Exception methods. Update service methods to return Error | T instead of mixed success/failure objects. Improve type safety in Claude.extractContents with explicit return type.

Add WikiLinks helper to VaultCacheService for managing wiki link references.

Update unit tests.
2025-11-17 19:02:15 +00:00

41 lines
No EOL
943 B
TypeScript

export class Semaphore {
private max: number;
private count: number;
private readonly waitAsync: boolean;
private readonly queue: ((value: boolean) => void)[];
constructor(max: number, waitAsync: boolean) {
this.max = max;
this.count = max;
this.waitAsync = waitAsync;
this.queue = [];
}
async wait(): Promise<boolean> {
if (this.count > 0) {
this.count--;
return true;
}
if (!this.waitAsync) {
return false;
}
return new Promise<boolean>((resolve) => {
this.queue.push(resolve);
});
}
release() {
if (this.queue.length > 0) {
const resolve = this.queue.shift();
if (resolve) {
resolve(true);
}
} else {
if (this.count < this.max) {
this.count++;
}
}
}
}