 

let shuttingDown = false;
let activeTransactions = 0;

export const isShuttingDown = () => shuttingDown;
export const setShuttingDown = (value: boolean) => {
    shuttingDown = value;
};

export const incrementTransactions = () => {
    activeTransactions++;
};

export const decrementTransactions = () => {
    activeTransactions--;
};

export const getActiveTransactions = () => activeTransactions;

export const waitForTransactions = async (timeoutMs: number = 10000) => {
    const start = Date.now();
    while (activeTransactions > 0) {
        if (Date.now() - start > timeoutMs) {
            console.warn(`Shutdown timed out waiting for ${activeTransactions} transactions after ${timeoutMs}ms`);
            break;
        }
        await new Promise(resolve => setTimeout(resolve, 100));
    }
};
