@openmrs/esm-extensions 3.1.15-pre.751 → 3.1.15-pre.752

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,114 @@ 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;
59
+ }
60
+
61
+ export interface ExtensionStore {
62
+ slots: Record<string, ExtensionSlotState>;
63
+ }
64
+
65
+ export interface ExtensionSlotState {
66
+ moduleName: string;
67
+ assignedExtensions: Array<AssignedExtension>;
68
+ }
69
+
70
+ export interface AssignedExtension {
71
+ id: string;
72
+ name: string;
73
+ meta: ExtensionMeta;
74
+ online?: boolean | object;
75
+ offline?: boolean | object;
76
76
  }
77
77
 
78
- export const extensionStore = createGlobalStore<ExtensionStore>("extensions", {
79
- slots: {},
80
- extensions: {},
81
- });
78
+ export interface ConnectedExtension {
79
+ id: string;
80
+ name: string;
81
+ meta: ExtensionMeta;
82
+ }
82
83
 
84
+ const extensionInternalStore = createGlobalStore<ExtensionInternalStore>(
85
+ "extensionsInternal",
86
+ {
87
+ slots: {},
88
+ extensions: {},
89
+ }
90
+ );
91
+
92
+ /**
93
+ * This gets the extension system's internal store. It is subject
94
+ * to change radically and without warning. It should not be used
95
+ * outside esm-core.
96
+ * @internal
97
+ */
98
+ export const getExtensionInternalStore = () =>
99
+ getGlobalStore<ExtensionInternalStore>("extensionsInternal", {
100
+ slots: {},
101
+ extensions: {},
102
+ });
103
+
104
+ /** @internal */
83
105
  export type MaybeAsync<T> = T | Promise<T>;
84
106
 
85
107
  let storeUpdates: Promise<void> = Promise.resolve();
86
108
 
87
- export function updateExtensionStore(
88
- updater: (state: ExtensionStore) => MaybeAsync<ExtensionStore>
109
+ /** @internal */
110
+ export function updateInternalExtensionStore(
111
+ updater: (state: ExtensionInternalStore) => MaybeAsync<ExtensionInternalStore>
89
112
  ) {
90
113
  storeUpdates = storeUpdates.then(async () => {
91
- const state = extensionStore.getState();
114
+ const state = extensionInternalStore.getState();
92
115
  const newState = await updater(state);
93
116
 
94
117
  if (newState !== state) {
95
- extensionStore.setState(newState);
118
+ extensionInternalStore.setState(newState);
96
119
  }
97
120
  });
98
121
  }
99
122
 
123
+ /**
124
+ * This returns a [store](https://github.com/developit/unistore#store)
125
+ * that modules can use to get information about the state of the
126
+ * extension system.
127
+ */
128
+ export const getExtensionStore = () =>
129
+ getGlobalStore<ExtensionStore>("extensions", {
130
+ slots: {},
131
+ });
132
+
100
133
  /**
101
134
  * esm-config maintains its own store of the extension information it needs
102
135
  * to generate extension configs. We keep it updated based on what's in
103
136
  * `extensionStore`.
104
137
  */
105
138
 
106
- updateConfigExtensionStore(extensionStore.getState());
107
- extensionStore.subscribe(updateConfigExtensionStore);
139
+ updateConfigExtensionStore(extensionInternalStore.getState());
140
+ extensionInternalStore.subscribe(updateConfigExtensionStore);
108
141
 
109
- function updateConfigExtensionStore(extensionState: ExtensionStore) {
142
+ function updateConfigExtensionStore(extensionState: ExtensionInternalStore) {
110
143
  const configExtensionRecords: Array<ConfigExtensionStoreElement> = [];
111
144
 
112
145
  for (let extensionInfo of Object.values(extensionState.extensions)) {