@@ -19,7 +19,6 @@ type DomainRecord = {
1919} ;
2020
2121const _globalThis = globalThis as OpenFeatureGlobal ;
22- const _localThis = { } as OpenFeatureGlobal ;
2322
2423export class OpenFeatureAPI
2524 extends OpenFeatureCommonAPI < ClientProviderStatus , Provider , Hook >
@@ -40,21 +39,23 @@ export class OpenFeatureAPI
4039 }
4140
4241 /**
43- * Gets a singleton instance of the OpenFeature API.
42+ * Gets a instance of the OpenFeature API.
4443 * @ignore
45- * @param {boolean } global Whether to get the global (window) singleton instance or a package-local singleton instance.
44+ * @param {boolean } singleton Whether to get the global (window) singleton instance or an isolated non- singleton instance.
4645 * @returns {OpenFeatureAPI } OpenFeature API
4746 */
48- static getInstance ( global = true ) : OpenFeatureAPI {
49- const store = global ? _globalThis : _localThis ;
47+ static getInstance ( singleton = true ) : OpenFeatureAPI {
48+ if ( ! singleton ) {
49+ return new OpenFeatureAPI ( ) ;
50+ }
5051
51- const globalApi = store [ GLOBAL_OPENFEATURE_API_KEY ] ;
52+ const globalApi = _globalThis [ GLOBAL_OPENFEATURE_API_KEY ] ;
5253 if ( globalApi ) {
5354 return globalApi ;
5455 }
5556
5657 const instance = new OpenFeatureAPI ( ) ;
57- store [ GLOBAL_OPENFEATURE_API_KEY ] = instance ;
58+ _globalThis [ GLOBAL_OPENFEATURE_API_KEY ] = instance ;
5859 return instance ;
5960 }
6061
@@ -427,55 +428,53 @@ export class OpenFeatureAPI
427428
428429interface OpenFeatureAPIWithIsolated extends OpenFeatureAPI {
429430 /**
430- * A package-local singleton instance of the OpenFeature API.
431+ * Create a new isolated, non- singleton instance of the OpenFeature API.
431432 *
432433 * By default, the OpenFeature API is exposed as a global singleton instance (stored on `window` in browsers).
433434 * While this can be very convenient as domains, providers, etc., are shared across an entire application,
434435 * this can mean that in multi-frontend architectures (e.g. micro-frontends) different parts of an application
435436 * can think they're loading different versions of OpenFeature, when they're actually all sharing the same instance.
436437 *
437- * The `isolated` property provides access to a package-local singleton instance of the OpenFeature API,
438- * which is not shared globally, isolated from the global singleton. As such, it will not share domains, providers,
439- * etc., with the global singleton instance, and uses its own version of the SDK.
440- *
441- * The `isolated` property allows different parts of a multi-frontend application to have their own isolated
442- * OpenFeature API instances, avoiding potential conflicts and ensuring they're using the expected version of the SDK.
443- * However, it is still a singleton within the package though, so it will share state with other uses of the
444- * `isolated` instance imported from the same package within the same micro-frontend.
438+ * The `getIsolated` method allows different parts of a multi-frontend application to have their own isolated
439+ * OpenFeature API instances, avoiding potential conflicts and ensuring they're using the expected version of the SDK,
440+ * and don't risk colliding with any other usages of OpenFeature on the same page.
445441 * @example
446442 * import { OpenFeature } from '@openfeature/web-sdk';
447443 *
448444 * OpenFeature.setProvider(new MyGlobalProvider()); // Sets the provider for the default domain on the global instance
449- * OpenFeature.isolated.setProvider(new MyIsolatedProvider()); // Sets the provider for the default domain on the isolated instance
450- *
451445 * const globalClient = OpenFeature.getClient(); // Uses MyGlobalProvider, the provider for the default domain on the global instance
452- * const isolatedClient = OpenFeature.isolated.getClient(); // Uses MyIsolatedProvider, the provider for the default domain on the isolated instance
446+ *
447+ * export const OpenFeatureIsolated = OpenFeature.getIsolated(); // Create a new isolated instance of the OpenFeature API and export it
448+ * OpenFeatureIsolated.setProvider(new MyIsolatedProvider()); // Sets the provider for the default domain on the isolated instance
449+ * const isolatedClient = OpenFeatureIsolated.getClient(); // Uses MyIsolatedProvider, the provider for the default domain on the isolated instance
453450 *
454451 * // In the same micro-frontend, in a different file ...
455452 * import { OpenFeature } from '@openfeature/web-sdk';
453+ * import { OpenFeatureIsolated } from './other-file';
456454 *
457455 * const globalClient = OpenFeature.getClient(); // Uses MyGlobalProvider, the provider for the default domain on the global instance
458- * const isolatedClient = OpenFeature.isolated.getClient(); // Uses MyIsolatedProvider, the provider for the default domain on the isolated instance
456+ * const isolatedClient = OpenFeatureIsolated.getClient(); // Uses MyIsolatedProvider, the provider for the default domain on the isolated instance
457+ *
458+ * const OpenFeatureIsolatedOther = OpenFeature.getIsolated(); // Create another new isolated instance of the OpenFeature API
459+ * const isolatedOtherClient = OpenFeatureIsolatedOther.getClient(); // Uses the NOOP provider, as this is a different isolated instance
459460 *
460461 * // In another micro-frontend, after the above has executed ...
461462 * import { OpenFeature } from '@openfeature/web-sdk';
462463 *
463464 * const globalClient = OpenFeature.getClient(); // Uses MyGlobalProvider, the provider for the default domain on the global instance
464- * const isolatedClient = OpenFeature.isolated.getClient(); // Returns the NOOP provider, as this is a different isolated instance
465+ *
466+ * const OpenFeatureIsolated = OpenFeature.getIsolated(); // Create a new isolated instance of the OpenFeature API
467+ * const isolatedClient = OpenFeatureIsolated.getClient(); // Uses the NOOP provider, as this is a different isolated instance
465468 */
466- readonly isolated : OpenFeatureAPI ;
469+ getIsolated : ( ) => OpenFeatureAPI ;
467470}
468471
469- const createOpenFeatureAPI = ( ) : OpenFeatureAPIWithIsolated => {
470- const globalInstance = OpenFeatureAPI . getInstance ( ) ;
471- const localInstance = OpenFeatureAPI . getInstance ( false ) ;
472-
473- return Object . assign ( globalInstance , {
474- get isolated ( ) {
475- return localInstance ;
472+ const createOpenFeatureAPI = ( ) : OpenFeatureAPIWithIsolated =>
473+ Object . assign ( OpenFeatureAPI . getInstance ( ) , {
474+ getIsolated ( ) {
475+ return OpenFeatureAPI . getInstance ( false ) ;
476476 } ,
477477 } ) ;
478- } ;
479478
480479/**
481480 * A singleton instance of the OpenFeature API.
0 commit comments