UNPKG

@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.

41 lines 1.58 kB
/** * @module CircuitBreaker */ import {} from "../../../../circuit-breaker/contracts/_module.js"; import {} from "../../../../circuit-breaker/implementations/adapters/database-circuit-breaker-adapter/internal-circuit-breaker-policy.js"; import {} from "../../../../circuit-breaker/implementations/adapters/database-circuit-breaker-adapter/types.js"; import {} from "../../../../execution-context/contracts/_module.js"; /** * @internal */ export class CircuitBreakerStorage { adapter; circuitBreakerPolicy; constructor(adapter, circuitBreakerPolicy) { this.adapter = adapter; this.circuitBreakerPolicy = circuitBreakerPolicy; } async atomicUpdate(context, key, update) { const currentDate = new Date(); return await this.adapter.transaction(context, async (trx) => { const currentState = (await trx.find(context, key)) ?? this.circuitBreakerPolicy.initialState(); const newState = update(currentState, currentDate); if (!this.circuitBreakerPolicy.isEqual(currentState, newState)) { await trx.upsert(context, key, newState); } return { from: currentState.type, to: newState.type, }; }); } async find(context, key) { return ((await this.adapter.find(context, key)) ?? this.circuitBreakerPolicy.initialState()); } async remove(context, key) { await this.adapter.remove(context, key); } } //# sourceMappingURL=circuit-breaker-storage.js.map