UNPKG

unleash-server

Version:

Unleash is an enterprise ready feature flag service. It provides different strategies for handling feature flags.

67 lines 2.78 kB
import EventEmitter from 'events'; export const UPDATE_REVISION = 'UPDATE_REVISION'; export default class ConfigurationRevisionService extends EventEmitter { constructor({ eventStore }, { getLogger, flagResolver, }) { super(); this.maxRevisionId = new Map(); this.logger = getLogger('configuration-revision-service.ts'); this.eventStore = eventStore; this.flagResolver = flagResolver; this.revisionId = 0; } static getInstance({ eventStore }, { getLogger, flagResolver, }) { if (!ConfigurationRevisionService.instance) { ConfigurationRevisionService.instance = new ConfigurationRevisionService({ eventStore }, { getLogger, flagResolver }); } return ConfigurationRevisionService.instance; } async getMaxRevisionId(environment) { if (environment) { let maxEnvRevisionId = this.maxRevisionId.get(environment) ?? 0; if (maxEnvRevisionId === 0) { maxEnvRevisionId = await this.updateMaxEnvironmentRevisionId(environment); } if (maxEnvRevisionId > 0) { return maxEnvRevisionId; } } if (this.revisionId > 0) { return this.revisionId; } else { return this.updateMaxRevisionId(); } } async updateMaxEnvironmentRevisionId(environment) { let maxRevId = this.maxRevisionId.get(environment) ?? 0; const actualMax = await this.eventStore.getMaxRevisionId(maxRevId, environment); if (maxRevId < actualMax) { this.maxRevisionId.set(environment, actualMax); maxRevId = actualMax; } return maxRevId; } async updateMaxRevisionId(emit = true) { if (this.flagResolver.isEnabled('disableUpdateMaxRevisionId')) { return 0; } const revisionId = await this.eventStore.getMaxRevisionId(this.revisionId); if (this.revisionId !== revisionId) { const knownEnvironments = [...this.maxRevisionId.keys()]; this.logger.debug(`Updating feature configuration with new revision Id ${revisionId} and all envs: ${knownEnvironments.join(', ')}`); await Promise.allSettled(knownEnvironments.map((environment) => this.updateMaxEnvironmentRevisionId(environment))); this.revisionId = revisionId; if (emit) { this.emit(UPDATE_REVISION, revisionId); } } return this.revisionId; } destroy() { ConfigurationRevisionService.instance?.removeAllListeners(); ConfigurationRevisionService.instance = undefined; } } //# sourceMappingURL=configuration-revision-service.js.map