@daiso-tech/core
Version:
The library offers flexible, framework-agnostic solutions for modern web applications, built on adaptable components that integrate seamlessly with popular frameworks like Next Js.
33 lines • 1.29 kB
JavaScript
/**
* @module RateLimiter
*/
import {} from "../../../../backoff-policies/contracts/_module.js";
import { RATE_LIMITER_STATE } from "../../../../rate-limiter/contracts/_module.js";
import {} from "../../../../rate-limiter/implementations/adapters/database-rate-limiter-adapter/internal-rate-limiter-policy.js";
/**
* @internal
*/
export class RateLimiterStateManager {
rateLimiterPolicy;
backoffPolicy;
constructor(rateLimiterPolicy, backoffPolicy) {
this.rateLimiterPolicy = rateLimiterPolicy;
this.backoffPolicy = backoffPolicy;
}
updateState = (limit, currentDate) => (currentState) => {
if (currentState.type === RATE_LIMITER_STATE.ALLOWED) {
return this.rateLimiterPolicy.whenAllowed(currentState, limit, currentDate);
}
return this.rateLimiterPolicy.whenBlocked(currentState, {
currentDate,
backoffPolicy: this.backoffPolicy,
});
};
track = (currentDate) => (currentState) => {
if (currentState.type === RATE_LIMITER_STATE.ALLOWED) {
return this.rateLimiterPolicy.trackWhenAllowed(currentState, currentDate);
}
return this.rateLimiterPolicy.trackWhenBlocked(currentState);
};
}
//# sourceMappingURL=rate-limiter-state-manager.js.map