mirror of
https://github.com/andy-stack/vaultkeeper-ai.git
synced 2026-07-22 16:30:27 +00:00
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.
41 lines
No EOL
943 B
TypeScript
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++;
|
|
}
|
|
}
|
|
}
|
|
} |