@openmrs/esm-extensions 3.1.15-pre.892 → 3.2.1-pre.1012

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/store.ts CHANGED
@@ -2,8 +2,9 @@ import isEqual from "lodash-es/isEqual";
2
2
  import {
3
3
  configExtensionStore,
4
4
  ConfigExtensionStoreElement,
5
+ ExtensionSlotConfigObject,
5
6
  } from "@openmrs/esm-config";
6
- import { createGlobalStore } from "@openmrs/esm-state";
7
+ import { createGlobalStore, getGlobalStore } from "@openmrs/esm-state";
7
8
 
8
9
  export interface ExtensionMeta {
9
10
  [_: string]: any;
@@ -31,82 +32,115 @@ export interface ExtensionInstance {
31
32
  id: string;
32
33
  }
33
34
 
34
- export interface ExtensionStore {
35
+ export interface ExtensionInternalStore {
35
36
  /** Slots indexed by name */
36
37
  slots: Record<string, ExtensionSlotInfo>;
37
38
  /** Extensions indexed by name */
38
39
  extensions: Record<string, ExtensionInfo>;
39
40
  }
40
41
 
41
- export interface ExtensionSlotInstance {
42
- /**
43
- * A set of additional extension IDs which have been added to to this slot despite not being
44
- * explicitly `attach`ed to it.
45
- * An example may be an extension which is added to the slot via the configuration.
46
- */
47
- addedIds: Array<string>;
48
- /**
49
- * A set of extension IDs which have been removed/hidden from this slot, even though they have
50
- * previously been `attach`ed/added to it.
51
- * An example may be an extension which is removed from the slot via the configuration.
52
- */
53
- removedIds: Array<string>;
54
- /**
55
- * A set allowing explicit ordering of the `assignedIds`.
56
- */
57
- idOrder: Array<string>;
58
- }
59
-
60
42
  export interface ExtensionSlotInfo {
61
43
  /**
62
- * The name under which the extension slot has been registered.
44
+ * The module in which the extension slot exists. Undefined if the slot
45
+ * hasn't been registered yet (but it has been attached or assigned to
46
+ * an extension.
63
47
  */
48
+ moduleName?: string;
49
+ /** The name under which the extension slot has been registered. */
64
50
  name: string;
65
51
  /**
66
- * The mapping of modules / extension slot instances where the extension slot has been used.
67
- */
68
- instances: Record<string, ExtensionSlotInstance>;
69
- /**
70
- * The set of extension IDs which have been attached to this slot.
71
- * This is essentially a complete history of `attach` calls to this specific slot.
52
+ * The set of extension IDs which have been attached to this slot using `attach`.
72
53
  * However, not all of these extension IDs should be rendered.
73
54
  * `assignedIds` is the set defining those.
74
55
  */
75
56
  attachedIds: Array<string>;
57
+ /** The configuration provided for this extension slot. `null` if not yet loaded. */
58
+ config: ExtensionSlotConfigObject | null;
76
59
  }
77
60
 
78
- export const extensionStore = createGlobalStore<ExtensionStore>("extensions", {
79
- slots: {},
80
- extensions: {},
81
- });
61
+ export interface ExtensionStore {
62
+ slots: Record<string, ExtensionSlotState>;
63
+ }
64
+
65
+ export interface ExtensionSlotState {
66
+ moduleName: string;
67
+ assignedExtensions: Array<AssignedExtension>;
68
+ }
82
69
 
70
+ export interface AssignedExtension {
71
+ id: string;
72
+ name: string;
73
+ moduleName: string;
74
+ meta: ExtensionMeta;
75
+ online?: boolean | object;
76
+ offline?: boolean | object;
77
+ }
78
+
79
+ export interface ConnectedExtension {
80
+ id: string;
81
+ name: string;
82
+ meta: ExtensionMeta;
83
+ }
84
+
85
+ const extensionInternalStore = createGlobalStore<ExtensionInternalStore>(
86
+ "extensionsInternal",
87
+ {
88
+ slots: {},
89
+ extensions: {},
90
+ }
91
+ );
92
+
93
+ /**
94
+ * This gets the extension system's internal store. It is subject
95
+ * to change radically and without warning. It should not be used
96
+ * outside esm-core.
97
+ * @internal
98
+ */
99
+ export const getExtensionInternalStore = () =>
100
+ getGlobalStore<ExtensionInternalStore>("extensionsInternal", {
101
+ slots: {},
102
+ extensions: {},
103
+ });
104
+
105
+ /** @internal */
83
106
  export type MaybeAsync<T> = T | Promise<T>;
84
107
 
85
108
  let storeUpdates: Promise<void> = Promise.resolve();
86
109
 
87
- export function updateExtensionStore(
88
- updater: (state: ExtensionStore) => MaybeAsync<ExtensionStore>
110
+ /** @internal */
111
+ export function updateInternalExtensionStore(
112
+ updater: (state: ExtensionInternalStore) => MaybeAsync<ExtensionInternalStore>
89
113
  ) {
90
114
  storeUpdates = storeUpdates.then(async () => {
91
- const state = extensionStore.getState();
115
+ const state = extensionInternalStore.getState();
92
116
  const newState = await updater(state);
93
117
 
94
118
  if (newState !== state) {
95
- extensionStore.setState(newState);
119
+ extensionInternalStore.setState(newState);
96
120
  }
97
121
  });
98
122
  }
99
123
 
124
+ /**
125
+ * This returns a [store](https://github.com/developit/unistore#store)
126
+ * that modules can use to get information about the state of the
127
+ * extension system.
128
+ */
129
+ export const getExtensionStore = () =>
130
+ getGlobalStore<ExtensionStore>("extensions", {
131
+ slots: {},
132
+ });
133
+
100
134
  /**
101
135
  * esm-config maintains its own store of the extension information it needs
102
136
  * to generate extension configs. We keep it updated based on what's in
103
137
  * `extensionStore`.
104
138
  */
105
139
 
106
- updateConfigExtensionStore(extensionStore.getState());
107
- extensionStore.subscribe(updateConfigExtensionStore);
140
+ updateConfigExtensionStore(extensionInternalStore.getState());
141
+ extensionInternalStore.subscribe(updateConfigExtensionStore);
108
142
 
109
- function updateConfigExtensionStore(extensionState: ExtensionStore) {
143
+ function updateConfigExtensionStore(extensionState: ExtensionInternalStore) {
110
144
  const configExtensionRecords: Array<ConfigExtensionStoreElement> = [];
111
145
 
112
146
  for (let extensionInfo of Object.values(extensionState.extensions)) {