"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.setSafeInterval = void 0;
const ErrorUtil_1 = require("./errors/ErrorUtil");
/**
 * Wraps the callback for {@link setInterval} so errors get caught and logged.
 * Parameters are identical to the {@link setInterval} parameters starting from the 3rd argument.
 * The logger and message will be used when the callback throws an error.
 * Supports asynchronous callback functions.
 */
function setSafeInterval(logger, message, callback, ms, ...args) {
    async function safeCallback(...cbArgs) {
        try {
            // We don't know if the callback is async or not so this way we make sure
            // the full function execution is done in the try block.
            return await callback(...cbArgs);
        }
        catch (error) {
            logger.error(`Error during interval callback: ${message} - ${(0, ErrorUtil_1.createErrorMessage)(error)}`);
        }
    }
    // eslint-disable-next-line ts/no-misused-promises
    return setInterval(safeCallback, ms, ...args);
}
exports.setSafeInterval = setSafeInterval;
//# sourceMappingURL=TimerUtil.js.map