Skip to content

Commit 05cca27

Browse files
committed
Only shutdown providers that are not attached to any client
Signed-off-by: Lukas Reining <[email protected]>
1 parent 5d55ea1 commit 05cca27

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

packages/client/test/open-feature.spec.ts

+23
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,29 @@ describe('OpenFeature', () => {
8080

8181
expect(namedClient.metadata.providerMetadata.name).toEqual(fakeProvider.metadata.name);
8282
});
83+
84+
it('should close a provider if it is replaced and no other client uses it', async () => {
85+
const provider1 = { ...MOCK_PROVIDER, onClose: jest.fn() };
86+
const provider2 = { ...MOCK_PROVIDER, onClose: jest.fn() };
87+
88+
OpenFeature.setProvider('client1', provider1);
89+
expect(provider1.onClose).not.toHaveBeenCalled();
90+
OpenFeature.setProvider('client1', provider2);
91+
expect(provider1.onClose).toHaveBeenCalledTimes(1);
92+
});
93+
94+
it('should not close provider if it is used by another client', async () => {
95+
const provider1 = { ...MOCK_PROVIDER, onClose: jest.fn() };
96+
97+
OpenFeature.setProvider('client1', provider1);
98+
OpenFeature.setProvider('client2', provider1);
99+
100+
OpenFeature.setProvider('client1', { ...provider1 });
101+
expect(provider1.onClose).not.toHaveBeenCalled();
102+
103+
OpenFeature.setProvider('client2', { ...provider1 });
104+
expect(provider1.onClose).toHaveBeenCalledTimes(1);
105+
});
83106
});
84107

85108
describe('Requirement 1.1.4', () => {

packages/server/test/open-feature.spec.ts

+23
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,29 @@ describe('OpenFeature', () => {
8282

8383
expect(namedClient.metadata.providerMetadata.name).toEqual(fakeProvider.metadata.name);
8484
});
85+
86+
it('should close a provider if it is replaced and no other client uses it', async () => {
87+
const provider1 = { ...MOCK_PROVIDER, onClose: jest.fn() };
88+
const provider2 = { ...MOCK_PROVIDER, onClose: jest.fn() };
89+
90+
OpenFeature.setProvider('client1', provider1);
91+
expect(provider1.onClose).not.toHaveBeenCalled();
92+
OpenFeature.setProvider('client1', provider2);
93+
expect(provider1.onClose).toHaveBeenCalledTimes(1);
94+
});
95+
96+
it('should not close provider if it is used by another client', async () => {
97+
const provider1 = { ...MOCK_PROVIDER, onClose: jest.fn() };
98+
99+
OpenFeature.setProvider('client1', provider1);
100+
OpenFeature.setProvider('client2', provider1);
101+
102+
OpenFeature.setProvider('client1', { ...provider1 });
103+
expect(provider1.onClose).not.toHaveBeenCalled();
104+
105+
OpenFeature.setProvider('client2', { ...provider1 });
106+
expect(provider1.onClose).toHaveBeenCalledTimes(1);
107+
});
85108
});
86109

87110
describe('Requirement 1.1.4', () => {

packages/shared/src/open-feature.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,11 @@ export abstract class OpenFeatureCommonAPI<P extends CommonProvider = CommonProv
129129

130130
this.transferListeners(oldProvider, provider, clientName, clientEmitter);
131131

132-
// Do not close the default provider if a named client used the default provider
133-
if (!clientName || (clientName && oldProvider !== this._defaultProvider)) {
132+
// Do not close a provider, that is bound to any client
133+
if (![...this._clientProviders.values(), this._defaultProvider].includes(oldProvider)) {
134134
oldProvider?.onClose?.();
135135
}
136+
136137
return this;
137138
}
138139

0 commit comments

Comments
 (0)