From 24a36abf7b27739dc2c666d8c036ff35e9d16cdb Mon Sep 17 00:00:00 2001 From: Peter Csajtai Date: Fri, 7 Jul 2023 12:44:28 +0200 Subject: [PATCH] Fix always firing onConfigChanged hook (#87) * Fix always firing onConfigChanged hook * Update ConfigServiceBase.ts * Add test * Bump version --- package-lock.json | 4 ++-- package.json | 2 +- src/ConfigServiceBase.ts | 13 ++++++------- test/ConfigCatClientTests.ts | 18 ++++++++++++++++++ 4 files changed, 27 insertions(+), 10 deletions(-) diff --git a/package-lock.json b/package-lock.json index 19598db..6042742 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "configcat-common", - "version": "8.0.1", + "version": "8.0.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "configcat-common", - "version": "8.0.1", + "version": "8.0.2", "license": "MIT", "dependencies": { "tslib": "^2.4.1" diff --git a/package.json b/package.json index 5fe9690..c0805d4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "configcat-common", - "version": "8.0.1", + "version": "8.0.2", "description": "ConfigCat is a configuration as a service that lets you manage your features and configurations without actually deploying new code.", "main": "lib/index.js", "types": "lib/index.d.ts", diff --git a/src/ConfigServiceBase.ts b/src/ConfigServiceBase.ts index 52b876c..714efe9 100644 --- a/src/ConfigServiceBase.ts +++ b/src/ConfigServiceBase.ts @@ -99,16 +99,15 @@ export abstract class ConfigServiceBase { const success = fetchResult.status === FetchStatus.Fetched; if (success || fetchResult.config.timestamp > latestConfig.timestamp && (!fetchResult.config.isEmpty || latestConfig.isEmpty)) { + await this.options.cache.set(this.cacheKey, fetchResult.config); - latestConfig = fetchResult.config; - - await this.options.cache.set(this.cacheKey, latestConfig); - - this.onConfigUpdated(latestConfig); + this.onConfigUpdated(fetchResult.config); - if (success) { - this.onConfigChanged(latestConfig); + if (success && (fetchResult.config.httpETag != latestConfig.httpETag || fetchResult.config.configJson != latestConfig.configJson)) { + this.onConfigChanged(fetchResult.config); } + + latestConfig = fetchResult.config; } return [fetchResult, latestConfig]; diff --git a/test/ConfigCatClientTests.ts b/test/ConfigCatClientTests.ts index d27b683..988185c 100644 --- a/test/ConfigCatClientTests.ts +++ b/test/ConfigCatClientTests.ts @@ -496,6 +496,24 @@ describe("ConfigCatClient", () => { assert.equal(configChangedEventCount, 3); }); + it("Initialization With AutoPollOptions - config doesn't change - should fire configChanged only once", async () => { + + const configCatKernel: FakeConfigCatKernel = { configFetcher: new FakeConfigFetcher(), sdkType: "common", sdkVersion: "1.0.0" }; + let configChangedEventCount = 0; + const pollIntervalSeconds = 1; + const userOptions: IAutoPollOptions = { + logger: null, + pollIntervalSeconds, + setupHooks: hooks => hooks.on("configChanged", () => configChangedEventCount++) + }; + const options: AutoPollOptions = new AutoPollOptions("APIKEY", "common", "1.0.0", userOptions, null); + new ConfigCatClient(options, configCatKernel); + + await delay(2.5 * pollIntervalSeconds * 1000); + + assert.equal(configChangedEventCount, 1); + }); + it("Initialization With AutoPollOptions - with maxInitWaitTimeSeconds - getValueAsync should wait", async () => { const maxInitWaitTimeSeconds = 2;