Skip to content

Commit

Permalink
feat!: add events to in-memory provider (#436)
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Reining <lukas.reining@codecentric.de>
Co-authored-by: Todd Baert <todd.baert@dynatrace.com>
  • Loading branch information
lukas-reining and toddbaert committed Jul 6, 2023
1 parent d2a3109 commit 626cda8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
2 changes: 1 addition & 1 deletion libs/providers/in-memory/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"current-version": "echo $npm_package_version"
},
"peerDependencies": {
"@openfeature/js-sdk": "^1.0.0"
"@openfeature/js-sdk": "^1.3.0"
}
}
34 changes: 34 additions & 0 deletions libs/providers/in-memory/src/lib/in-memory-provider.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
FlagNotFoundError,
GeneralError,
ProviderEvents,
ResolutionDetails,
StandardResolutionReasons,
TypeMismatchError,
Expand Down Expand Up @@ -112,11 +113,44 @@ describe(InMemoryProvider, () => {
const resolution = await provider.resolveStringEvaluation('some-flag');
verifyResolution(resolution, { expectedValue: 'initial-value' });
});

describe('events', () => {
it('emits provider changed event if a new value is added', (done) => {
const configuration = {
'some-flag': 'initial-value',
};
const provider = new InMemoryProvider(configuration);

provider.events.addHandler(ProviderEvents.ConfigurationChanged, (details) => {
expect(details?.flagsChanged).toEqual(['some-other-flag']);
done();
});

const newConfiguration = { ...configuration, 'some-other-flag': 'value' };
provider.replaceConfiguration(newConfiguration);
});

it('emits provider changed event if an existing value is changed', (done) => {
const configuration = {
'some-flag': 'initial-value',
};
const provider = new InMemoryProvider(configuration);

provider.events.addHandler(ProviderEvents.ConfigurationChanged, (details) => {
expect(details?.flagsChanged).toEqual(['some-flag']);
done();
});

const newConfiguration = { 'some-flag': 'new-value' };
provider.replaceConfiguration(newConfiguration);
});
});
});

type VerifyResolutionParams<U> = {
expectedValue: U;
};

function verifyResolution<U>(resolution: ResolutionDetails<U>, { expectedValue }: VerifyResolutionParams<U>) {
expect(resolution.value).toBe(expectedValue);
expect(resolution.reason).toBe(StandardResolutionReasons.STATIC);
Expand Down
19 changes: 13 additions & 6 deletions libs/providers/in-memory/src/lib/in-memory-provider.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import {
EvaluationContext,
Provider,
FlagNotFoundError,
FlagValue,
GeneralError,
JsonValue,
OpenFeatureEventEmitter,
Provider,
ProviderEvents,
ResolutionDetails,
FlagNotFoundError,
TypeMismatchError,
StandardResolutionReasons,
GeneralError,
FlagValue,
TypeMismatchError,
} from '@openfeature/js-sdk';
import { FlagConfiguration } from './flag-configuration';

export class InMemoryProvider implements Provider {
public readonly events = new OpenFeatureEventEmitter();
readonly metadata = {
name: 'In-Memory Provider',
} as const;
Expand All @@ -22,7 +24,12 @@ export class InMemoryProvider implements Provider {
}

replaceConfiguration(flagConfiguration: FlagConfiguration) {
const flagsChanged = Object.entries(flagConfiguration)
.filter(([key, value]) => this._flagConfiguration[key] !== value)
.map(([key]) => key);

this._flagConfiguration = { ...flagConfiguration };
this.events.emit(ProviderEvents.ConfigurationChanged, { flagsChanged });
}

async resolveBooleanEvaluation(flagKey: string): Promise<ResolutionDetails<boolean>> {
Expand Down

0 comments on commit 626cda8

Please sign in to comment.