Skip to content
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

(fix) Notify config system when app is fully loaded #1228

Merged
merged 3 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions packages/framework/esm-config/src/module-config/module-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,25 @@ export function defineConfigSchema(moduleName: string, schema: ConfigSchema) {
* @param moduleName
*/
export function registerModuleWithConfigSystem(moduleName: string) {
const state = configInternalStore.getState();
configInternalStore.setState({
configInternalStore.setState((state) => ({
...state,
schemas: { ...state.schemas, [moduleName]: implicitConfigSchema },
});
}));
}

/**
* This alerts the configuration system that a module has been loaded.
*
* This should only be used in esm-app-shell.
*
* @internal
* @param moduleName
*/
export function registerModuleLoad(moduleName: string) {
configInternalStore.setState((state) => ({
...state,
moduleLoaded: { ...state.moduleLoaded, [moduleName]: true },
}));
}

/**
Expand All @@ -201,10 +216,10 @@ export function registerModuleWithConfigSystem(moduleName: string) {
* @param namespace
*/
export function registerTranslationNamespace(namespace: string) {
const state = configInternalStore.getState();
configInternalStore.setState({
configInternalStore.setState((state) => ({
...state,
schemas: { ...state.schemas, [namespace]: translationOverridesSchema },
});
}));
}

/**
Expand Down Expand Up @@ -235,9 +250,10 @@ export function defineExtensionConfigSchema(extensionName: string, schema: Confi
);
}

configInternalStore.setState({
configInternalStore.setState((state) => ({
...state,
schemas: { ...state.schemas, [extensionName]: enhancedSchema },
});
}));
}

export function provide(config: Config, sourceName = 'provided') {
Expand Down
14 changes: 7 additions & 7 deletions packages/framework/esm-framework/docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -1736,7 +1736,7 @@ ___

• `Const` **MaybeIcon**: `MemoExoticComponent`<`ForwardRefExoticComponent`<{ `fallback?`: `ReactNode` ; `icon`: `string` } & [`IconProps`](API.md#iconprops) & `RefAttributes`<`SVGSVGElement`\>\>\>

This is a utility component that takes an `icon` and render it if the sprite for the icon
This is a utility component that takes an `icon` and renders it if the sprite for the icon
is available. The goal is to make it easier to conditionally render configuration-specified icons.

**`example`**
Expand All @@ -1754,12 +1754,12 @@ ___

• `Const` **MaybePictogram**: `MemoExoticComponent`<`ForwardRefExoticComponent`<{ `fallback?`: `ReactNode` ; `pictogram`: `string` } & [`PictogramProps`](API.md#pictogramprops) & `RefAttributes`<`SVGSVGElement`\>\>\>

This is a utility component that takes an `icon` and render it if the sprite for the icon
is available. The goal is to make it easier to conditionally render configuration-specified icons.
This is a utility component that takes an `pictogram` and render it if the sprite for the pictogram
is available. The goal is to make it easier to conditionally render configuration-specified pictograms.

**`example`**
```tsx
<MaybeIcon icon='omrs-icon-baby' className={styles.myIconStyles} />
<MaybePictogram pictogram='omrs-icon-baby' className={styles.myPictogramStyles} />
```

#### Defined in
Expand Down Expand Up @@ -3391,7 +3391,7 @@ for more information about defining a config schema.

#### Defined in

[packages/framework/esm-config/src/module-config/module-config.ts:227](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-config/src/module-config/module-config.ts#L227)
[packages/framework/esm-config/src/module-config/module-config.ts:242](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-config/src/module-config/module-config.ts#L242)

___

Expand Down Expand Up @@ -3423,7 +3423,7 @@ of the execution of a function.

#### Defined in

[packages/framework/esm-config/src/module-config/module-config.ts:259](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-config/src/module-config/module-config.ts#L259)
[packages/framework/esm-config/src/module-config/module-config.ts:275](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-config/src/module-config/module-config.ts#L275)

___

Expand All @@ -3444,7 +3444,7 @@ ___

#### Defined in

[packages/framework/esm-config/src/module-config/module-config.ts:243](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-config/src/module-config/module-config.ts#L243)
[packages/framework/esm-config/src/module-config/module-config.ts:259](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-config/src/module-config/module-config.ts#L259)

___

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
defineConfigSchema,
provide,
Type,
registerModuleLoad,
temporaryConfigStore,
configInternalStore,
getExtensionSlotsConfigStore,
Expand Down Expand Up @@ -46,6 +47,7 @@ describe('Interaction between configuration and extension systems', () => {
attach('A slot', 'Fred');
attach('A slot', 'Wilma');
defineConfigSchema('esm-flintstone', {});
registerModuleLoad('esm-flintstone');

provide({
'esm-flintstone': {
Expand Down Expand Up @@ -87,6 +89,8 @@ describe('Interaction between configuration and extension systems', () => {
defineConfigSchema('esm-flintstone', {
town: { _type: Type.String, _default: 'Bedrock' },
});
registerModuleLoad('esm-flintstone');

attach('Flintstone slot', 'Pebbles');
attach('Future slot', 'Pebbles');
provide({
Expand Down Expand Up @@ -136,6 +140,8 @@ describe('Interaction between configuration and extension systems', () => {
defineConfigSchema('esm-characters', {
name: { _type: Type.String, _default: '(no-name)' },
});
registerModuleLoad('esm-characters');

attach('Flintstone slot', 'pet#Dino');
attach('Flintstone slot', 'pet#BabyPuss');
provide({
Expand Down Expand Up @@ -183,6 +189,7 @@ describe('Interaction between configuration and extension systems', () => {
registerSimpleExtension('Pearl', 'esm-slaghoople');
attach('A slot', 'Pearl');
defineConfigSchema('esm-slaghoople', {});
registerModuleLoad('esm-flintstone');

const App = openmrsComponentDecorator({
moduleName: 'esm-slaghoople',
Expand Down Expand Up @@ -218,6 +225,7 @@ describe('Interaction between configuration and extension systems', () => {
registerSimpleExtension('Mr. Slate', 'esm-flintstone', true);
attach('A slot', 'Mr. Slate');
defineConfigSchema('esm-flintstone', { tie: { _default: 'green' } });
registerModuleLoad('esm-flintstone');

const App = openmrsComponentDecorator({
moduleName: 'esm-quarry',
Expand Down Expand Up @@ -256,6 +264,8 @@ describe('Interaction between configuration and extension systems', () => {
registerSimpleExtension('Bamm-Bamm', 'esm-flintstone', false);
attach('A slot', 'Bamm-Bamm');
defineConfigSchema('esm-flintstone', { clothes: { _default: 'leopard' } });
registerModuleLoad('esm-flintstone');

function RootComponent() {
const store = useExtensionStore();
return (
Expand Down Expand Up @@ -329,6 +339,8 @@ describe('Interaction between configuration and extension systems', () => {
attach('A slot', 'Wilma');
defineConfigSchema('esm-bedrock', {});
defineConfigSchema('esm-flintstones', {});
registerModuleLoad('esm-bedrock');
registerModuleLoad('esm-flintstones');
provide({
'esm-bedrock': {
'Display conditions': {
Expand Down Expand Up @@ -389,6 +401,7 @@ describe('Interaction between configuration and extension systems', () => {
registerSimpleExtension('Schmoo', 'esm-bedrock', true);
attach('A slot', 'Schmoo');
defineConfigSchema('esm-bedrock', {});
registerModuleLoad('esm-bedrock');
provide({
'esm-bedrock': {
'Display conditions': {
Expand Down Expand Up @@ -448,6 +461,8 @@ describe('Interaction between configuration and extension systems', () => {
attach('A slot', 'Wilma');
defineConfigSchema('esm-bedrock', {});
defineConfigSchema('esm-flintstones', {});
registerModuleLoad('esm-bedrock');
registerModuleLoad('esm-flintstones');
provide({ 'esm-bedrock': {} });
provide({ 'esm-flintstones': {} });

Expand Down
9 changes: 8 additions & 1 deletion packages/framework/esm-routes/src/loaders/app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { importDynamic } from '@openmrs/esm-dynamic-loading';
import { registerModuleLoad } from '@openmrs/esm-config';
import { type LifeCycles } from 'single-spa';
import { emptyLifecycle } from './helpers';

Expand Down Expand Up @@ -68,10 +69,16 @@ export async function initializeApp(appName: string, module?: Module) {
if (Object.hasOwn(_module, 'startupApp')) {
const startup = _module['startupApp'];
if (typeof startup === 'function') {
return Promise.resolve(startup()).then(resolve).catch(reject);
return Promise.resolve(startup())
.then(() => {
registerModuleLoad(appName);
resolve(null);
})
.catch(reject);
}
}

registerModuleLoad(appName);
resolve(null);
}));
} else {
Expand Down
Loading