-
Notifications
You must be signed in to change notification settings - Fork 52
feat: allow non-singleton access to OpenFeatureAPI #1303
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
Draft
MattIPv4
wants to merge
4
commits into
open-feature:main
Choose a base branch
from
MattIPv4:feat/non-global-singleton
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
cc66a5d
Provide a local singleton of OpenFeatureAPI
MattIPv4 6b1f7e9
Add rudimentary tests for isolated instance
MattIPv4 e90d007
Add rudimentary React provider option for local singleton
MattIPv4 3cd61b1
Expose creating new instances instead of an isolated singleton
MattIPv4 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,11 +39,16 @@ export class OpenFeatureAPI | |
| } | ||
|
|
||
| /** | ||
| * Gets a singleton instance of the OpenFeature API. | ||
| * Gets a instance of the OpenFeature API. | ||
| * @ignore | ||
| * @param {boolean} singleton Whether to get the global (window) singleton instance or an isolated non-singleton instance. | ||
| * @returns {OpenFeatureAPI} OpenFeature API | ||
| */ | ||
| static getInstance(): OpenFeatureAPI { | ||
| static getInstance(singleton = true): OpenFeatureAPI { | ||
| if (!singleton) { | ||
| return new OpenFeatureAPI(); | ||
| } | ||
|
|
||
| const globalApi = _globalThis[GLOBAL_OPENFEATURE_API_KEY]; | ||
| if (globalApi) { | ||
| return globalApi; | ||
|
|
@@ -421,8 +426,58 @@ export class OpenFeatureAPI | |
| } | ||
| } | ||
|
|
||
| interface OpenFeatureAPIWithIsolated extends OpenFeatureAPI { | ||
| /** | ||
| * Create a new isolated, non-singleton instance of the OpenFeature API. | ||
| * | ||
| * By default, the OpenFeature API is exposed as a global singleton instance (stored on `window` in browsers). | ||
| * While this can be very convenient as domains, providers, etc., are shared across an entire application, | ||
| * this can mean that in multi-frontend architectures (e.g. micro-frontends) different parts of an application | ||
| * can think they're loading different versions of OpenFeature, when they're actually all sharing the same instance. | ||
| * | ||
| * The `getIsolated` method allows different parts of a multi-frontend application to have their own isolated | ||
| * OpenFeature API instances, avoiding potential conflicts and ensuring they're using the expected version of the SDK, | ||
| * and don't risk colliding with any other usages of OpenFeature on the same page. | ||
| * @example | ||
| * import { OpenFeature } from '@openfeature/web-sdk'; | ||
| * | ||
| * OpenFeature.setProvider(new MyGlobalProvider()); // Sets the provider for the default domain on the global instance | ||
| * const globalClient = OpenFeature.getClient(); // Uses MyGlobalProvider, the provider for the default domain on the global instance | ||
| * | ||
| * export const OpenFeatureIsolated = OpenFeature.getIsolated(); // Create a new isolated instance of the OpenFeature API and export it | ||
| * OpenFeatureIsolated.setProvider(new MyIsolatedProvider()); // Sets the provider for the default domain on the isolated instance | ||
| * const isolatedClient = OpenFeatureIsolated.getClient(); // Uses MyIsolatedProvider, the provider for the default domain on the isolated instance | ||
| * | ||
| * // In the same micro-frontend, in a different file ... | ||
| * import { OpenFeature } from '@openfeature/web-sdk'; | ||
| * import { OpenFeatureIsolated } from './other-file'; | ||
| * | ||
| * const globalClient = OpenFeature.getClient(); // Uses MyGlobalProvider, the provider for the default domain on the global instance | ||
| * const isolatedClient = OpenFeatureIsolated.getClient(); // Uses MyIsolatedProvider, the provider for the default domain on the isolated instance | ||
| * | ||
| * const OpenFeatureIsolatedOther = OpenFeature.getIsolated(); // Create another new isolated instance of the OpenFeature API | ||
| * const isolatedOtherClient = OpenFeatureIsolatedOther.getClient(); // Uses the NOOP provider, as this is a different isolated instance | ||
| * | ||
| * // In another micro-frontend, after the above has executed ... | ||
| * import { OpenFeature } from '@openfeature/web-sdk'; | ||
| * | ||
| * const globalClient = OpenFeature.getClient(); // Uses MyGlobalProvider, the provider for the default domain on the global instance | ||
| * | ||
| * const OpenFeatureIsolated = OpenFeature.getIsolated(); // Create a new isolated instance of the OpenFeature API | ||
| * const isolatedClient = OpenFeatureIsolated.getClient(); // Uses the NOOP provider, as this is a different isolated instance | ||
| */ | ||
| getIsolated: () => OpenFeatureAPI; | ||
| } | ||
|
|
||
| const createOpenFeatureAPI = (): OpenFeatureAPIWithIsolated => | ||
| Object.assign(OpenFeatureAPI.getInstance(), { | ||
| getIsolated() { | ||
| return OpenFeatureAPI.getInstance(false); | ||
| }, | ||
| }); | ||
|
Comment on lines
+472
to
+477
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doing this via |
||
|
|
||
| /** | ||
| * A singleton instance of the OpenFeature API. | ||
| * @returns {OpenFeatureAPI} OpenFeature API | ||
| * @returns {OpenFeatureAPIWithIsolated} OpenFeature API | ||
| */ | ||
| export const OpenFeature = OpenFeatureAPI.getInstance(); | ||
| export const OpenFeature = createOpenFeatureAPI(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| import type { JsonValue, OpenFeatureAPI, Provider, ProviderMetadata, ResolutionDetails } from '../src'; | ||
|
|
||
| const GLOBAL_OPENFEATURE_API_KEY = Symbol.for('@openfeature/web-sdk/api'); | ||
|
|
||
| class MockProvider implements Provider { | ||
| readonly metadata: ProviderMetadata; | ||
|
|
||
| constructor(options?: { name?: string }) { | ||
| this.metadata = { name: options?.name ?? 'mock-provider' }; | ||
| } | ||
|
|
||
| resolveBooleanEvaluation(): ResolutionDetails<boolean> { | ||
| throw new Error('Not implemented'); | ||
| } | ||
|
|
||
| resolveNumberEvaluation(): ResolutionDetails<number> { | ||
| throw new Error('Not implemented'); | ||
| } | ||
|
|
||
| resolveObjectEvaluation<T extends JsonValue>(): ResolutionDetails<T> { | ||
| throw new Error('Not implemented'); | ||
| } | ||
|
|
||
| resolveStringEvaluation(): ResolutionDetails<string> { | ||
| throw new Error('Not implemented'); | ||
| } | ||
| } | ||
|
|
||
| const _globalThis = globalThis as { | ||
| [GLOBAL_OPENFEATURE_API_KEY]?: OpenFeatureAPI; | ||
| }; | ||
|
|
||
| describe('OpenFeature', () => { | ||
| beforeEach(() => { | ||
| Reflect.deleteProperty(_globalThis, GLOBAL_OPENFEATURE_API_KEY); | ||
| expect(_globalThis[GLOBAL_OPENFEATURE_API_KEY]).toBeUndefined(); | ||
| jest.resetModules(); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('should persist via globalThis (window in browsers)', async () => { | ||
| const firstInstance = (await import('../src')).OpenFeature; | ||
|
|
||
| jest.resetModules(); | ||
| const secondInstance = (await import('../src')).OpenFeature; | ||
|
|
||
| expect(firstInstance).toBe(secondInstance); | ||
| expect(_globalThis[GLOBAL_OPENFEATURE_API_KEY]).toBe(firstInstance); | ||
| }); | ||
|
|
||
| it('can also be accessed via OpenFeatureAPI.getInstance', async () => { | ||
| const { OpenFeature, OpenFeatureAPI } = await import('../src'); | ||
|
|
||
| expect(OpenFeature).toBe(OpenFeatureAPI.getInstance()); | ||
| }); | ||
|
|
||
| describe('OpenFeature.getIsolated', () => { | ||
| it('should not be the same instance as the global singleton', async () => { | ||
| const { OpenFeature } = await import('../src'); | ||
|
|
||
| expect(OpenFeature.getIsolated()).not.toBe(OpenFeature); | ||
| }); | ||
|
|
||
| it('should not be the same instance as another isolated instance', async () => { | ||
| const { OpenFeature } = await import('../src'); | ||
|
|
||
| expect(OpenFeature.getIsolated()).not.toBe(OpenFeature.getIsolated()); | ||
| }); | ||
|
|
||
| it('can also be created via OpenFeatureAPI.getInstance', async () => { | ||
| const { OpenFeature, OpenFeatureAPI } = await import('../src'); | ||
|
|
||
| expect(OpenFeatureAPI.getInstance(false)).not.toBe(OpenFeature); | ||
| }); | ||
|
|
||
| it('should not share state between global and isolated instances', async () => { | ||
| const { OpenFeature, NOOP_PROVIDER } = await import('../src'); | ||
| const isolatedInstance = OpenFeature.getIsolated(); | ||
|
|
||
| const globalProvider = new MockProvider({ name: 'global-provider' }); | ||
| OpenFeature.setProvider(globalProvider); | ||
|
|
||
| expect(OpenFeature.getProvider()).toBe(globalProvider); | ||
| expect(isolatedInstance.getProvider()).toBe(NOOP_PROVIDER); | ||
|
|
||
| const isolatedProvider = new MockProvider({ name: 'isolated-provider' }); | ||
| isolatedInstance.setProvider(isolatedProvider); | ||
|
|
||
| expect(OpenFeature.getProvider()).toBe(globalProvider); | ||
| expect(isolatedInstance.getProvider()).toBe(isolatedProvider); | ||
| }); | ||
|
|
||
| it('should not share state between two isolated instances', async () => { | ||
| const { OpenFeature, NOOP_PROVIDER } = await import('../src'); | ||
| const isolatedInstanceOne = OpenFeature.getIsolated(); | ||
| const isolatedInstanceTwo = OpenFeature.getIsolated(); | ||
|
|
||
| const isolatedProviderOne = new MockProvider({ name: 'isolated-provider-one' }); | ||
| isolatedInstanceOne.setProvider(isolatedProviderOne); | ||
|
|
||
| expect(isolatedInstanceOne.getProvider()).toBe(isolatedProviderOne); | ||
| expect(isolatedInstanceTwo.getProvider()).toBe(NOOP_PROVIDER); | ||
|
|
||
| const isolatedProviderTwo = new MockProvider({ name: 'isolated-provider-two' }); | ||
| isolatedInstanceTwo.setProvider(isolatedProviderTwo); | ||
|
|
||
| expect(isolatedInstanceOne.getProvider()).toBe(isolatedProviderOne); | ||
| expect(isolatedInstanceTwo.getProvider()).toBe(isolatedProviderTwo); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,7 @@ | ||
| { | ||
| "extends": "../tsconfig.json", | ||
| "include": ["."] | ||
| "include": ["."], | ||
| "compilerOptions": { | ||
| "module": "es2020" | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once #1317 is in, I suspect I can apply a similar approach to that here, where the client tracks a reference to the OpenFeatureAPI instance it came from, and we can use that here to infer the correct instance