2023-05-12 19:52:11 +02:00
|
|
|
import { EventEmitter } from 'stream';
|
|
|
|
import { Logger } from '../../logger';
|
|
|
|
import { IEventStore, IUnleashConfig, IUnleashStores } from '../../types';
|
|
|
|
|
|
|
|
export const UPDATE_REVISION = 'UPDATE_REVISION';
|
|
|
|
|
|
|
|
export default class ConfigurationRevisionService extends EventEmitter {
|
|
|
|
private logger: Logger;
|
|
|
|
|
|
|
|
private eventStore: IEventStore;
|
|
|
|
|
|
|
|
private revisionId: number;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
{ eventStore }: Pick<IUnleashStores, 'eventStore'>,
|
|
|
|
{ getLogger }: Pick<IUnleashConfig, 'getLogger'>,
|
|
|
|
) {
|
|
|
|
super();
|
|
|
|
this.logger = getLogger('configuration-revision-service.ts');
|
|
|
|
this.eventStore = eventStore;
|
2023-08-01 23:59:09 +02:00
|
|
|
this.revisionId = 0;
|
2023-05-12 19:52:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async getMaxRevisionId(): Promise<number> {
|
2023-08-01 23:59:09 +02:00
|
|
|
if (this.revisionId > 0) {
|
2023-05-12 19:52:11 +02:00
|
|
|
return this.revisionId;
|
|
|
|
} else {
|
|
|
|
return this.updateMaxRevisionId();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async updateMaxRevisionId(): Promise<number> {
|
|
|
|
const revisionId = await this.eventStore.getMaxRevisionId(
|
|
|
|
this.revisionId,
|
|
|
|
);
|
|
|
|
if (this.revisionId !== revisionId) {
|
|
|
|
this.logger.debug(
|
|
|
|
'Updating feature configuration with new revision Id',
|
|
|
|
revisionId,
|
|
|
|
);
|
|
|
|
this.emit(UPDATE_REVISION, revisionId);
|
|
|
|
this.revisionId = revisionId;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.revisionId;
|
|
|
|
}
|
|
|
|
}
|