Skip to content

Commit

Permalink
Fix always firing onConfigChanged hook (#87)
Browse files Browse the repository at this point in the history
* Fix always firing onConfigChanged hook

* Update ConfigServiceBase.ts

* Add test

* Bump version
  • Loading branch information
z4kn4fein authored Jul 7, 2023
1 parent 406d749 commit 24a36ab
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
13 changes: 6 additions & 7 deletions src/ConfigServiceBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,15 @@ export abstract class ConfigServiceBase<TOptions extends OptionsBase> {
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];
Expand Down
18 changes: 18 additions & 0 deletions test/ConfigCatClientTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 24a36ab

Please sign in to comment.