Skip to content

Commit

Permalink
(feat) Add "Enable Feature Flags" config option to the app-shell (#1249)
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmale authored Jan 16, 2025
1 parent 77414b1 commit 275b090
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
temporaryConfigStore,
getExtensionSlotsConfigStore,
} from './state';
import type {} from '@openmrs/esm-globals';
import { type TemporaryConfigStore } from '..';

/**
Expand Down
6 changes: 3 additions & 3 deletions packages/framework/esm-routes/src/loaders/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,10 @@ To fix this, ensure that you define the "name" field inside the workspace defini
}

/**
* This function registers a workspace definition with the framework so that it can be launched.
* This function registers a feature flag definition with the framework.
*
* @param appName The name of the app defining this workspace
* @param featureFlag An object that describes the workspace, derived from `routes.json`
* @param appName The name of the app defining this feature flag
* @param featureFlag An object that describes the feature flag, derived from `routes.json`
*/
export function tryRegisterFeatureFlag(appName: string, featureFlag: FeatureFlagDefinition) {
const name = featureFlag.flagName;
Expand Down
38 changes: 38 additions & 0 deletions packages/shell/esm-app-shell/src/core-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { defineConfigSchema, Type, getConfigStore } from '@openmrs/esm-framework';
import { registerModuleLoad, featureFlagsStore } from '@openmrs/esm-framework/src/internal';
import { appName } from './ui';

/**
* Sets up the app shell's configuration.
*/
export function setupCoreConfig() {
defineConfigSchema(appName, {
'Enabled feature flags': {
_description: 'The feature flags that will be enabled',
_type: Type.Array,
_default: [],
_elements: {
_type: Type.String,
},
},
});
registerModuleLoad(appName);

// process feature flags
getConfigStore(appName).subscribe(({ config }) => {
const flagsToEnable = new Set((config?.['Enabled feature flags'] as Array<string>) || []);
if (flagsToEnable.size) {
const newFlags = { ...featureFlagsStore.getState().flags };
for (const flag of flagsToEnable) {
// validate the flag exists
if (!newFlags[flag]) {
console.error(`Feature flag "${flag}" does not exist. Please define the flag first.`);
} else {
newFlags[flag] = { ...newFlags[flag], enabled: true };
}
}

featureFlagsStore.setState({ flags: newFlags });
}
});
}
2 changes: 2 additions & 0 deletions packages/shell/esm-app-shell/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
import { setupI18n } from './locale';
import { registerOptionalDependencyHandler } from './optionaldeps';
import { appName, getCoreExtensions } from './ui';
import { setupCoreConfig } from './core-config';

// @internal
// used to track when the window.installedModules global is finalised
Expand Down Expand Up @@ -408,6 +409,7 @@ export function run(configUrls: Array<string>) {
setupApiModule();
setupHistory();
registerCoreExtensions();
setupCoreConfig();

return setupApps()
.then(finishRegisteringAllApps)
Expand Down

0 comments on commit 275b090

Please sign in to comment.