Files
standardnotes-app-web/packages/snjs/lib/Services/Sync/SyncFrequencyGuard.ts
2024-01-11 15:26:09 +01:00

41 lines
1.3 KiB
TypeScript

import { SyncFrequencyGuardInterface } from './SyncFrequencyGuardInterface'
export class SyncFrequencyGuard implements SyncFrequencyGuardInterface {
private callsPerMinuteMap: Map<string, number>
constructor(private syncCallsThresholdPerMinute: number) {
this.callsPerMinuteMap = new Map<string, number>()
}
isSyncCallsThresholdReachedThisMinute(): boolean {
const stringDateToTheMinute = this.getCallsPerMinuteKey()
const persistedCallsCount = this.callsPerMinuteMap.get(stringDateToTheMinute) || 0
return persistedCallsCount >= this.syncCallsThresholdPerMinute
}
incrementCallsPerMinute(): void {
const stringDateToTheMinute = this.getCallsPerMinuteKey()
const persistedCallsCount = this.callsPerMinuteMap.get(stringDateToTheMinute)
const newMinuteStarted = persistedCallsCount === undefined
if (newMinuteStarted) {
this.clear()
this.callsPerMinuteMap.set(stringDateToTheMinute, 1)
} else {
this.callsPerMinuteMap.set(stringDateToTheMinute, persistedCallsCount + 1)
}
}
clear(): void {
this.callsPerMinuteMap.clear()
}
private getCallsPerMinuteKey(): string {
const now = new Date()
return `${now.getFullYear()}-${now.getMonth()}-${now.getDate()}T${now.getHours()}:${now.getMinutes()}`
}
}