/**
 * Simple async mutex that ensures only one async operation executes at a time
 */
export class AsyncMutex {
    locked = false;
    queue = [];
    async lock(operation) {
        return new Promise((resolve, reject) => {
            const execute = async () => {
                this.locked = true;
                try {
                    const result = await operation();
                    resolve(result);
                }
                catch (error) {
                    reject(error);
                }
                finally {
                    this.locked = false;
                    const next = this.queue.shift();
                    if (next) {
                        next();
                    }
                }
            };
            if (this.locked) {
                this.queue.push(execute);
            }
            else {
                execute();
            }
        });
    }
}
//# sourceMappingURL=mutex.js.map