renovate/lib/workers/global/limits.ts
Sergio Zharinov 831c45397f
refactor(limits): Concise implementation for workers/global/limits.ts (#7140)
Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
2020-09-01 14:47:12 +02:00

44 lines
958 B
TypeScript

import { logger } from '../../logger';
export enum Limit {
Commits = 'Commits',
}
interface LimitValue {
max: number | null;
current: number;
}
const limits = new Map<Limit, LimitValue>();
export function resetAllLimits(): void {
limits.clear();
}
export function setMaxLimit(key: Limit, max: unknown): void {
const maxVal = typeof max === 'number' && max > 0 ? max : null;
logger.debug(`${key} limit = ${maxVal}`);
const limit = limits.get(key);
limits.set(key, {
current: 0,
...limit,
max: maxVal,
});
}
export function incLimitedValue(key: Limit, incBy = 1): void {
const limit = limits.get(key) || { max: null, current: 0 };
limits.set(key, {
...limit,
current: limit.current + incBy,
});
}
export function isLimitReached(key: Limit): boolean {
const limit = limits.get(key);
if (!limit || limit.max === null) {
return false;
}
const { max, current } = limit;
return max - current <= 0;
}