Skip to content

Commit

Permalink
fix: allow iteration over all event types (#844)
Browse files Browse the repository at this point in the history
This is an addendum to #816.

Since the `addHandler` overloads for the event types
(`ProviderEvents.ConfigurationChanged` and `NotChangeEvents`) are
mutually exclusive, it was impossible to iterate over them and pass the
same function...

I had to add one more "base" overload that tolerated both. This doesn't
reduce any of the nice type safety added in
#816, but allows users to
iterate over `ProviderEvents` collections as seen in the new tests.

Signed-off-by: Todd Baert <[email protected]>
  • Loading branch information
toddbaert authored Mar 1, 2024
1 parent 33508f1 commit 411c7b4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/client/test/events.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,16 @@ describe('Events', () => {
// emit a change event from the mock provider
provider.events?.emit(ProviderEvents.ConfigurationChanged, { flagsChanged: [changedFlag] });
});

it('It allows iteration over all event types', () => {
// just a typings test; it should be possible to iterate over a collection of ProviderEvents,
const client = OpenFeature.getClient(domain);
const providerEvents: ProviderEvents[] = [];

providerEvents.forEach((e) => {
client.addHandler(e, () => {});
});
});
});

describe('Requirement 5.3.3', () => {
Expand Down
10 changes: 10 additions & 0 deletions packages/server/test/events.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,16 @@ describe('Events', () => {
// emit a change event from the mock provider
provider.events?.emit(ProviderEvents.ConfigurationChanged, { flagsChanged: [changedFlag] });
});

it('It allows iteration over all event types', () => {
// just a typings test; it should be possible to iterate over a collection of ProviderEvents,
const client = OpenFeature.getClient(domain);
const providerEvents: ProviderEvents[] = [];

providerEvents.forEach((e) => {
client.addHandler(e, () => {});
});
});
});

describe('Requirement 5.3.3', () => {
Expand Down
4 changes: 4 additions & 0 deletions packages/shared/src/events/eventing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ export interface Eventing<T extends ServerProviderEvents | ClientProviderEvents>
eventType: T extends ClientProviderEvents ? ClientNotChangeEvents : ServerNotChangeEvents,
handler: EventHandler<T extends ClientProviderEvents ? ClientNotChangeEvents : ServerNotChangeEvents>,
): void;
addHandler(
eventType: T extends ClientProviderEvents ? ClientProviderEvents : ServerProviderEvents,
handler: EventHandler<T extends ClientProviderEvents ? ClientProviderEvents : ServerProviderEvents>,
): void;

/**
* Removes a handler for the given provider event type.
Expand Down

0 comments on commit 411c7b4

Please sign in to comment.