import { Optional } from '../typings/standard-types' export enum Limit { Commits = 'Commits', PullRequests = 'PullRequests', Branches = 'Branches', } interface LimitValue { max: Optional current: number } const limits = new Map() export function resetAllLimits(): void { limits.clear() } export function setMaxLimit(key: Limit, val: unknown): void { const max = typeof val === 'number' ? Math.max(0, val) : null limits.set(key, { current: 0, max }) console.debug(`${key} limit = ${max}`) } 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 ? max - current <= 0 : false }