Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix always firing onConfigChanged hook #87

Merged
merged 4 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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