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

(feat) O3-2258: Extension system support for feature flags #733

Merged
merged 2 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions packages/framework/esm-extensions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@
"peerDependencies": {
"@openmrs/esm-api": "5.x",
"@openmrs/esm-config": "5.x",
"@openmrs/esm-feature-flags": "5.x",
"@openmrs/esm-state": "5.x",
"single-spa": "5.x"
},
"devDependencies": {
"@openmrs/esm-api": "^5.0.2",
"@openmrs/esm-config": "^5.0.2",
"@openmrs/esm-feature-flags": "^5.0.2",
"@openmrs/esm-state": "^5.0.2",
"single-spa": "^5.9.2"
},
Expand Down
21 changes: 17 additions & 4 deletions packages/framework/esm-extensions/src/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
getExtensionSlotConfigFromStore,
getExtensionSlotsConfigStore,
} from "@openmrs/esm-config";
import { featureFlagsStore } from "@openmrs/esm-feature-flags";
import isEqual from "lodash-es/isEqual";
import isUndefined from "lodash-es/isUndefined";
import {
Expand Down Expand Up @@ -282,16 +283,27 @@ function getOrder(
*
* @param assignedExtensions The list of extensions to filter.
* @param online Whether the app is currently online. If `null`, uses `navigator.onLine`.
* @param enabledFeatureFlags The names of all enabled feature flags. If `null`, looks
* up the feature flags using the feature flags API.
* @returns A list of extensions that should be rendered
*/
export function getConnectedExtensions(
assignedExtensions: Array<AssignedExtension>,
online: boolean | null = null
online: boolean | null = null,
enabledFeatureFlags: Array<string> | null = null
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure whether the featureFlag check should happen in the step where we determine assigned extensions or connected extensions. But if I had done it in the assigned extensions phase, I'd have needed to create a subscription on the feature flags store which updated the extension output store. I opted for the less complex option, which unfortunately creates a bit of ambiguity around the meaning of the word "connected."

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At some point, we should probably look into refactoring these anyways (I've never been a big fan of having hooks explicitly named after "connection status" rather than, say, some sort of lifecycle phase).

): Array<ConnectedExtension> {
const isOnline = online ?? navigator.onLine;
return assignedExtensions.filter((e) =>
checkStatusFor(isOnline, e.online, e.offline)
);
const featureFlags =
enabledFeatureFlags ??
Object.entries(featureFlagsStore.getState().flags)
.filter(([, { enabled }]) => enabled)
.map(([name]) => name);
return assignedExtensions
.filter((e) => checkStatusFor(isOnline, e.online, e.offline))
.filter(
(e) =>
e.featureFlag === undefined || featureFlags?.includes(e.featureFlag)
);
}

function getAssignedExtensionsFromSlotData(
Expand Down Expand Up @@ -343,6 +355,7 @@ function getAssignedExtensionsFromSlotData(
name,
moduleName: extension.moduleName,
config: extensionConfig,
featureFlag: extension.featureFlag,
meta: extension.meta,
});
}
Expand Down
2 changes: 2 additions & 0 deletions packages/framework/esm-extensions/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface ExtensionRegistration {
online?: boolean;
offline?: boolean;
privileges?: string | Array<string>;
featureFlag?: string;
}

export interface ExtensionInfo extends ExtensionRegistration {
Expand Down Expand Up @@ -81,6 +82,7 @@ export interface AssignedExtension {
config: ConfigObject | null;
online?: boolean | object;
offline?: boolean | object;
featureFlag?: string;
}

export interface ConnectedExtension {
Expand Down
25 changes: 13 additions & 12 deletions packages/framework/esm-framework/docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ ___

### ExtensionDefinition

Ƭ **ExtensionDefinition**: { `meta?`: { `[k: string]`: `unknown`; } ; `name`: `string` ; `offline?`: `boolean` ; `online?`: `boolean` ; `order?`: `number` ; `privileges?`: `string` \| `string`[] ; `slot?`: `string` ; `slots?`: `string`[] } & { `component`: `string` } \| { `component?`: `never` }
Ƭ **ExtensionDefinition**: { `featureFlag?`: `string` ; `meta?`: { `[k: string]`: `unknown`; } ; `name`: `string` ; `offline?`: `boolean` ; `online?`: `boolean` ; `order?`: `number` ; `privileges?`: `string` \| `string`[] ; `slot?`: `string` ; `slots?`: `string`[] } & { `component`: `string` } \| { `component?`: `never` }

A definition of an extension as extracted from an app's routes.json

Expand Down Expand Up @@ -444,7 +444,7 @@ Basically, this is the same as the app routes, with each routes definition keyed

#### Defined in

[packages/framework/esm-globals/src/types.ts:251](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-globals/src/types.ts#L251)
[packages/framework/esm-globals/src/types.ts:255](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-globals/src/types.ts#L255)

___

Expand Down Expand Up @@ -2257,7 +2257,7 @@ writing a module for a specific implementation.

#### Defined in

[packages/framework/esm-extensions/src/extensions.ts:174](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-extensions/src/extensions.ts#L174)
[packages/framework/esm-extensions/src/extensions.ts:175](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-extensions/src/extensions.ts#L175)

___

Expand All @@ -2280,7 +2280,7 @@ Avoid using this. Extension attachments should be considered declarative.

#### Defined in

[packages/framework/esm-extensions/src/extensions.ts:205](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-extensions/src/extensions.ts#L205)
[packages/framework/esm-extensions/src/extensions.ts:206](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-extensions/src/extensions.ts#L206)

___

Expand All @@ -2302,7 +2302,7 @@ Avoid using this. Extension attachments should be considered declarative.

#### Defined in

[packages/framework/esm-extensions/src/extensions.ts:229](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-extensions/src/extensions.ts#L229)
[packages/framework/esm-extensions/src/extensions.ts:230](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-extensions/src/extensions.ts#L230)

___

Expand All @@ -2326,13 +2326,13 @@ An array of extensions assigned to the named slot

#### Defined in

[packages/framework/esm-extensions/src/extensions.ts:360](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-extensions/src/extensions.ts#L360)
[packages/framework/esm-extensions/src/extensions.ts:373](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-extensions/src/extensions.ts#L373)

___

### getConnectedExtensions

▸ **getConnectedExtensions**(`assignedExtensions`, `online?`): [`ConnectedExtension`](interfaces/ConnectedExtension.md)[]
▸ **getConnectedExtensions**(`assignedExtensions`, `online?`, `enabledFeatureFlags?`): [`ConnectedExtension`](interfaces/ConnectedExtension.md)[]

Filters a list of extensions according to whether they support the
current connectivity status.
Expand All @@ -2343,6 +2343,7 @@ current connectivity status.
| :------ | :------ | :------ | :------ |
| `assignedExtensions` | [`AssignedExtension`](interfaces/AssignedExtension.md)[] | `undefined` | The list of extensions to filter. |
| `online` | ``null`` \| `boolean` | `null` | Whether the app is currently online. If `null`, uses `navigator.onLine`. |
| `enabledFeatureFlags` | ``null`` \| `string`[] | `null` | The names of all enabled feature flags. If `null`, looks up the feature flags using the feature flags API. |

#### Returns

Expand All @@ -2352,7 +2353,7 @@ A list of extensions that should be rendered

#### Defined in

[packages/framework/esm-extensions/src/extensions.ts:287](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-extensions/src/extensions.ts#L287)
[packages/framework/esm-extensions/src/extensions.ts:290](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-extensions/src/extensions.ts#L290)

___

Expand Down Expand Up @@ -2384,7 +2385,7 @@ getExtensionNameFromId("baz")

#### Defined in

[packages/framework/esm-extensions/src/extensions.ts:118](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-extensions/src/extensions.ts#L118)
[packages/framework/esm-extensions/src/extensions.ts:119](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-extensions/src/extensions.ts#L119)

___

Expand All @@ -2401,7 +2402,7 @@ state of the extension system.

#### Defined in

[packages/framework/esm-extensions/src/store.ts:130](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-extensions/src/store.ts#L130)
[packages/framework/esm-extensions/src/store.ts:132](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-extensions/src/store.ts#L132)

___

Expand Down Expand Up @@ -2487,7 +2488,7 @@ ___
▸ **useConnectedExtensions**(`slotName`): [`ConnectedExtension`](interfaces/ConnectedExtension.md)[]

Gets the assigned extension for a given extension slot name.
Considers if offline or online.
Considers if offline or online, and what feature flags are enabled.

#### Parameters

Expand All @@ -2501,7 +2502,7 @@ Considers if offline or online.

#### Defined in

[packages/framework/esm-react-utils/src/useConnectedExtensions.ts:15](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-react-utils/src/useConnectedExtensions.ts#L15)
[packages/framework/esm-react-utils/src/useConnectedExtensions.ts:17](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-react-utils/src/useConnectedExtensions.ts#L17)

___

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
### Extension Properties

- [config](AssignedExtension.md#config)
- [featureFlag](AssignedExtension.md#featureflag)
- [id](AssignedExtension.md#id)
- [meta](AssignedExtension.md#meta)
- [moduleName](AssignedExtension.md#modulename)
Expand All @@ -24,7 +25,17 @@ The extension's config. Note that this will be `null` until the slot is mounted.

#### Defined in

[packages/framework/esm-extensions/src/store.ts:81](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-extensions/src/store.ts#L81)
[packages/framework/esm-extensions/src/store.ts:82](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-extensions/src/store.ts#L82)

___

### featureFlag

• `Optional` **featureFlag**: `string`

#### Defined in

[packages/framework/esm-extensions/src/store.ts:85](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-extensions/src/store.ts#L85)

___

Expand All @@ -34,7 +45,7 @@ ___

#### Defined in

[packages/framework/esm-extensions/src/store.ts:76](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-extensions/src/store.ts#L76)
[packages/framework/esm-extensions/src/store.ts:77](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-extensions/src/store.ts#L77)

___

Expand All @@ -44,7 +55,7 @@ ___

#### Defined in

[packages/framework/esm-extensions/src/store.ts:79](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-extensions/src/store.ts#L79)
[packages/framework/esm-extensions/src/store.ts:80](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-extensions/src/store.ts#L80)

___

Expand All @@ -54,7 +65,7 @@ ___

#### Defined in

[packages/framework/esm-extensions/src/store.ts:78](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-extensions/src/store.ts#L78)
[packages/framework/esm-extensions/src/store.ts:79](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-extensions/src/store.ts#L79)

___

Expand All @@ -64,7 +75,7 @@ ___

#### Defined in

[packages/framework/esm-extensions/src/store.ts:77](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-extensions/src/store.ts#L77)
[packages/framework/esm-extensions/src/store.ts:78](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-extensions/src/store.ts#L78)

___

Expand All @@ -74,7 +85,7 @@ ___

#### Defined in

[packages/framework/esm-extensions/src/store.ts:83](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-extensions/src/store.ts#L83)
[packages/framework/esm-extensions/src/store.ts:84](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-extensions/src/store.ts#L84)

___

Expand All @@ -84,4 +95,4 @@ ___

#### Defined in

[packages/framework/esm-extensions/src/store.ts:82](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-extensions/src/store.ts#L82)
[packages/framework/esm-extensions/src/store.ts:83](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-extensions/src/store.ts#L83)
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ The extension's config. Note that this will be `null` until the slot is mounted.

#### Defined in

[packages/framework/esm-extensions/src/store.ts:91](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-extensions/src/store.ts#L91)
[packages/framework/esm-extensions/src/store.ts:93](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-extensions/src/store.ts#L93)

___

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

#### Defined in

[packages/framework/esm-extensions/src/store.ts:87](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-extensions/src/store.ts#L87)
[packages/framework/esm-extensions/src/store.ts:89](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-extensions/src/store.ts#L89)

___

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

#### Defined in

[packages/framework/esm-extensions/src/store.ts:89](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-extensions/src/store.ts#L89)
[packages/framework/esm-extensions/src/store.ts:91](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-extensions/src/store.ts#L91)

___

Expand All @@ -51,4 +51,4 @@ ___

#### Defined in

[packages/framework/esm-extensions/src/store.ts:88](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-extensions/src/store.ts#L88)
[packages/framework/esm-extensions/src/store.ts:90](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-extensions/src/store.ts#L90)
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

### Extension Properties

- [featureFlag](ExtensionRegistration.md#featureflag)
- [meta](ExtensionRegistration.md#meta)
- [moduleName](ExtensionRegistration.md#modulename)
- [name](ExtensionRegistration.md#name)
Expand All @@ -20,6 +21,16 @@

## Extension Properties

### featureFlag

• `Optional` **featureFlag**: `string`

#### Defined in

[packages/framework/esm-extensions/src/store.ts:25](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-extensions/src/store.ts#L25)

___

### meta

• **meta**: [`ExtensionMeta`](ExtensionMeta.md)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

#### Defined in

[packages/framework/esm-extensions/src/store.ts:72](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-extensions/src/store.ts#L72)
[packages/framework/esm-extensions/src/store.ts:73](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-extensions/src/store.ts#L73)

___

Expand All @@ -27,4 +27,4 @@ ___

#### Defined in

[packages/framework/esm-extensions/src/store.ts:71](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-extensions/src/store.ts#L71)
[packages/framework/esm-extensions/src/store.ts:72](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-extensions/src/store.ts#L72)
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@

#### Defined in

[packages/framework/esm-extensions/src/store.ts:67](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-extensions/src/store.ts#L67)
[packages/framework/esm-extensions/src/store.ts:68](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-extensions/src/store.ts#L68)
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ A list of backend modules necessary for this frontend module and the correspondi

#### Defined in

[packages/framework/esm-globals/src/types.ts:236](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-globals/src/types.ts#L236)
[packages/framework/esm-globals/src/types.ts:240](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-globals/src/types.ts#L240)

___

Expand All @@ -35,7 +35,7 @@ An array of all extensions supported by this frontend module. Extensions can be

#### Defined in

[packages/framework/esm-globals/src/types.ts:244](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-globals/src/types.ts#L244)
[packages/framework/esm-globals/src/types.ts:248](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-globals/src/types.ts#L248)

___

Expand All @@ -47,7 +47,7 @@ An array of all pages supported by this frontend module. Pages are automatically

#### Defined in

[packages/framework/esm-globals/src/types.ts:240](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-globals/src/types.ts#L240)
[packages/framework/esm-globals/src/types.ts:244](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-globals/src/types.ts#L244)

___

Expand All @@ -59,4 +59,4 @@ The version of this frontend module.

#### Defined in

[packages/framework/esm-globals/src/types.ts:232](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-globals/src/types.ts#L232)
[packages/framework/esm-globals/src/types.ts:236](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-globals/src/types.ts#L236)
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@

#### Defined in

[packages/framework/esm-globals/src/types.ts:254](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-globals/src/types.ts#L254)
[packages/framework/esm-globals/src/types.ts:258](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-globals/src/types.ts#L258)
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ describe("Interaction between configuration and extension systems", () => {
});

await waitFor(() => {
expect(screen.queryByText("green")).not.toBeInTheDocument();
expect(screen.getByTestId("slot")).toHaveTextContent(/black/);
expect(screen.queryByText("green")).not.toBeInTheDocument();
});
});

Expand Down
Loading
Loading