/**
 * A queue for operations that are executed one at a time.
 *
 * This is useful for ensuring that operations are executed in order, and that
 * they are not executed concurrently.
 */
export class OperationQueue {
    // Invariant: `lastOperation` is a promise that never rejects. This is needed to ensure that the queue
    // can always enqueue a new operation, even if the previous operation rejected.
    lastOperation = Promise.resolve();
    /**
     * Enqueue an operation to be executed one at a time.
     *
     * @param operation - The operation to execute.
     * @returns A promise that resolves to the result of the operation.
     */
    enqueue(operation) {
        const promise = this.lastOperation.then(operation);
        // If the operation throws (rejects), `promise` will reject with the same error.
        // But for the promise that we store in `lastOperation`, we don't care about the result,
        // we just want to ensure that it doesn't reject.
        this.lastOperation = promise.then(() => { }, () => { });
        // Return the raw promise for `operation`, so that the caller can handle the result or error.
        return promise;
    }
}
export class OperationPassthrough {
    enqueue(operation) {
        return operation();
    }
}
//# sourceMappingURL=operation-queue.js.map