From c2d074132b8f113957258cc409816d503aa09ec3 Mon Sep 17 00:00:00 2001 From: FredrikOseberg Date: Fri, 13 Dec 2024 09:10:00 +0100 Subject: [PATCH 1/2] fix: refactor pollEvents --- .../cache/client-feature-toggle-cache.ts | 63 ++++++++----------- 1 file changed, 26 insertions(+), 37 deletions(-) diff --git a/src/lib/features/client-feature-toggles/cache/client-feature-toggle-cache.ts b/src/lib/features/client-feature-toggles/cache/client-feature-toggle-cache.ts index b879f6370dc2..cb970fc78413 100644 --- a/src/lib/features/client-feature-toggles/cache/client-feature-toggle-cache.ts +++ b/src/lib/features/client-feature-toggles/cache/client-feature-toggle-cache.ts @@ -182,48 +182,37 @@ export class ClientFeatureToggleCache { } public async pollEvents() { + const keys = Object.keys(this.cache); + + if (keys.length === 0) return; const latestRevision = await this.configurationRevisionService.getMaxRevisionId(); - if (this.currentRevisionId === 0) { - await this.populateBaseCache(latestRevision); - } else { - const changeEvents = await this.eventStore.getRevisionRange( - this.currentRevisionId, - latestRevision, - ); - - const changedToggles = [ - ...new Set( - changeEvents - .filter((event) => event.featureName) - .map((event) => event.featureName!), - ), - ]; - const newToggles = await this.getChangedToggles( - changedToggles, - latestRevision, // TODO: this should come back from the same query to not be out of sync - ); - - if (this.cache.development) { - this.cache.development.addRevision(newToggles); - } - } - this.currentRevisionId = latestRevision; - } + const changeEvents = await this.eventStore.getRevisionRange( + this.currentRevisionId, + latestRevision, + ); - private async populateBaseCache(latestRevisionId: number) { - const features = await this.getClientFeatures({ - environment: 'development', - }); - if (this.cache.development) { - this.cache.development.addRevision({ - updated: features as any, //impressionData is not on the type but should be - removed: [], - revisionId: latestRevisionId, - }); + const changedToggles = [ + ...new Set( + changeEvents + .filter((event) => event.featureName) + .map((event) => event.featureName!), + ), + ]; + const newToggles = await this.getChangedToggles( + changedToggles, + latestRevision, // TODO: this should come back from the same query to not be out of sync + ); + + // TODO: Discussion point. Should we filter events by environment and only add revisions in the cache + // for the environment that changed? If we do that we can also save CPU and memory, because we don't need + // to talk to the database if the cache is not initialized for that environment + for (const key of keys) { + this.cache[key].addRevision(newToggles); } - console.log(`Populated base cache with ${features.length} features`); + + this.currentRevisionId = latestRevision; } async getChangedToggles( From 04ba982e46715d9feb6331a785367bcdb9fb10cf Mon Sep 17 00:00:00 2001 From: FredrikOseberg Date: Fri, 13 Dec 2024 09:10:25 +0100 Subject: [PATCH 2/2] fix: rename pollEvents --- .../cache/client-feature-toggle-cache.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/features/client-feature-toggles/cache/client-feature-toggle-cache.ts b/src/lib/features/client-feature-toggles/cache/client-feature-toggle-cache.ts index cb970fc78413..249190077079 100644 --- a/src/lib/features/client-feature-toggles/cache/client-feature-toggle-cache.ts +++ b/src/lib/features/client-feature-toggles/cache/client-feature-toggle-cache.ts @@ -177,11 +177,11 @@ export class ClientFeatureToggleCache { private async onUpdateRevisionEvent() { if (this.flagResolver.isEnabled('deltaApi')) { - await this.pollEvents(); + await this.listenToRevisionChange(); } } - public async pollEvents() { + public async listenToRevisionChange() { const keys = Object.keys(this.cache); if (keys.length === 0) return;