@uxland/primary-shell 8.2.3 → 8.3.0

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.
Files changed (47) hide show
  1. package/dist/{component-CpGMxTfT.js → component-SjR4v7lM.js} +2 -2
  2. package/dist/{component-CpGMxTfT.js.map → component-SjR4v7lM.js.map} +1 -1
  3. package/dist/{index-pgOwovhb.js → index-vk3Ud0yY.js} +84 -90
  4. package/dist/index-vk3Ud0yY.js.map +1 -0
  5. package/dist/index.js +19 -27
  6. package/dist/{plugin-DEXZvNv6.js → plugin-CiWzf-ag.js} +2 -2
  7. package/dist/{plugin-DEXZvNv6.js.map → plugin-CiWzf-ag.js.map} +1 -1
  8. package/dist/primary/shell/src/api/api.d.ts +15 -1
  9. package/dist/primary/shell/src/api/api.test.d.ts +1 -0
  10. package/dist/primary/shell/src/api/broker/factory.d.ts +11 -1
  11. package/dist/primary/shell/src/api/broker/primaria-broker.d.ts +0 -2
  12. package/dist/primary/shell/src/api/global-state/global-state.d.ts +0 -7
  13. package/dist/primary/shell/src/api/interaction-service/interaction-service-impl.d.ts +4 -0
  14. package/dist/primary/shell/src/api/interaction-service/interaction-service.d.ts +0 -1
  15. package/dist/primary/shell/src/api/quick-action-busy-manager/quick-action-busy-manager.d.ts +0 -2
  16. package/dist/primary/shell/src/api/region-manager/region-manager.d.ts +17 -2
  17. package/dist/primary/shell/src/api/token-manager/token-manager.d.ts +4 -12
  18. package/dist/primary/shell/src/bootstrapper.d.ts +12 -0
  19. package/dist/primary/shell/src/features/bootstrapper.d.ts +1 -1
  20. package/dist/primary/shell/src/handle-plugins.d.ts +7 -5
  21. package/dist/primary/shell/src/index.d.ts +4 -5
  22. package/package.json +1 -1
  23. package/src/api/api.test.ts +37 -0
  24. package/src/api/api.ts +37 -6
  25. package/src/api/broker/factory.test.ts +12 -7
  26. package/src/api/broker/factory.ts +23 -12
  27. package/src/api/broker/primaria-broker.ts +0 -2
  28. package/src/api/ecap-event-manager/ecap-event-manager.test.ts +0 -1
  29. package/src/api/global-state/global-state.test.ts +0 -8
  30. package/src/api/global-state/global-state.ts +1 -12
  31. package/src/api/http-client/http-client.test.ts +1 -1
  32. package/src/api/interaction-service/interaction-service-impl.tsx +4 -0
  33. package/src/api/interaction-service/interaction-service.ts +0 -1
  34. package/src/api/quick-action-busy-manager/quick-action-busy-manager.test.ts +0 -8
  35. package/src/api/quick-action-busy-manager/quick-action-busy-manager.ts +0 -5
  36. package/src/api/region-manager/region-manager.test.ts +8 -8
  37. package/src/api/region-manager/region-manager.ts +24 -28
  38. package/src/api/region-manager/remove-view-idempotency.test.ts +1 -1
  39. package/src/api/token-manager/token-manager.ts +4 -13
  40. package/src/bootstrapper.ts +23 -2
  41. package/src/disposer.ts +5 -3
  42. package/src/features/bootstrapper.ts +4 -4
  43. package/src/features/exit/handler.test.ts +11 -7
  44. package/src/features/exit/handler.ts +2 -2
  45. package/src/handle-plugins.ts +16 -6
  46. package/src/index.ts +18 -9
  47. package/dist/index-pgOwovhb.js.map +0 -1
@@ -25,8 +25,6 @@ export interface PrimariaRegionManager extends HarmonixRegionManager {
25
25
  activatePluginView(regionName: string, pluginId: string, viewId: string): Promise<void>;
26
26
  getCurrentMainViewActive(): string | undefined;
27
27
  getRegisteredMainViews(): RegisteredMainView[];
28
- _removeAllViews(): Promise<void>;
29
- _destroy(): Promise<void>;
30
28
  }
31
29
 
32
30
  /** Totes les regions que coneix el shell, per poder-les recórrer al tancar. */
@@ -221,37 +219,35 @@ class RegionManagerProxy implements PrimariaRegionManager {
221
219
  }
222
220
  }
223
221
 
224
- _notifyMainViewChanged(viewId: string) {
222
+ private _notifyMainViewChanged(viewId: string) {
225
223
  this.broker.publish(BROKER_EVENTS.shell.mainViewChanged, { viewId });
226
224
  }
225
+ }
227
226
 
228
- /**
229
- * Desmunta totes les vistes de totes les regions, i espera que acabin.
230
- *
231
- * Es crida en tancar, ABANS de fer el `dispose` dels plugins: els cleanups dels `useEffect` que
232
- * s'executen en desmuntar-se poden enviar comandes pel broker, i han de trobar el handler encara
233
- * registrat. Cada plugin ja ho fa pel seu compte al `dispose`, però això només cobreix els
234
- * plugins d'aquest repositori; els de tercers (econsulta, allergies…) no els controlem.
235
- *
236
- * Fa servir `unregisterViewFromRegion` i no `destroy()`/`clear()` del regionManager perquè
237
- * aquests fan `forEach` amb crides asíncrones sense esperar-les, així que retornen abans que les
238
- * vistes s'hagin desmuntat de debò.
239
- *
240
- * És idempotent: desregistrar una vista que ja no hi és no fa res.
241
- */
242
- async _removeAllViews(): Promise<void> {
243
- for (const regionName of ALL_REGION_NAMES) {
244
- const registered = this.regionManager.getRegisteredViews(regionName) ?? [];
245
- for (const { key } of [...registered]) {
246
- await this.regionManager.unregisterViewFromRegion(regionName, key);
247
- }
227
+ /**
228
+ * Desmunta totes les vistes de totes les regions, i espera que acabin.
229
+ *
230
+ * Es crida en tancar, ABANS de fer el `dispose` dels plugins: els cleanups dels `useEffect` que
231
+ * s'executen en desmuntar-se poden enviar comandes pel broker, i han de trobar el handler encara
232
+ * registrat. Cada plugin ja ho fa pel seu compte al `dispose`, però això només cobreix els
233
+ * plugins d'aquest repositori; els de tercers (econsulta, allergies…) no els controlem.
234
+ *
235
+ * Fa servir `unregisterViewFromRegion` i no `destroy()`/`clear()` del regionManager perquè
236
+ * aquests fan `forEach` amb crides asíncrones sense esperar-les, així que retornen abans que les
237
+ * vistes s'hagin desmuntat de debò.
238
+ *
239
+ * És idempotent: desregistrar una vista que ja no hi és no fa res.
240
+ *
241
+ * Host-only: és una funció i no un mètode del proxy perquè cap plugin la rebi amb la seva `api`.
242
+ */
243
+ export const removeAllViews = async (regionManager: IRegionManager): Promise<void> => {
244
+ for (const regionName of ALL_REGION_NAMES) {
245
+ const registered = regionManager.getRegisteredViews(regionName) ?? [];
246
+ for (const { key } of [...registered]) {
247
+ await regionManager.unregisterViewFromRegion(regionName, key);
248
248
  }
249
249
  }
250
-
251
- async _destroy() {
252
- await this.regionManager.destroy();
253
- }
254
- }
250
+ };
255
251
 
256
252
  /**
257
253
  * Creates a proxy for the region manager with the given plugin info and region manager instance.
@@ -7,7 +7,7 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
7
7
  * Aquest test NO fa servir dobles del regionManager: munta una `Region` de `@uxland/regions` i un
8
8
  * `RegionManager` de `@uxland/harmonix` de debo.
9
9
  *
10
- * El motiu: des que el shell desmunta totes les vistes al tancar (`_removeAllViews`), l'
10
+ * El motiu: des que el shell desmunta totes les vistes al tancar (`removeAllViews`), l'
11
11
  * `unregisterViews` de cada plugin s'executa DESPRES, sobre vistes que ja no hi son. Tot el
12
12
  * disseny es sosté en que aquesta segona passada sigui inofensiva, i aixo depen del comportament
13
13
  * intern d'aquestes dues llibreries, no del nostre codi. Amb dobles no es provaria res.
@@ -4,29 +4,20 @@ import { PrimariaBroker } from "../broker/primaria-broker";
4
4
 
5
5
  /**
6
6
  * API pública de tokens disponible per als plugins via `api.tokenManager`.
7
- * Els plugins poden llegir el token actual o forçar-ne el refresc; NO han de
8
- * fixar els tokens inicials (això és una responsabilitat del host de bootstrap
9
- * — vegeu {@link InternalTokenManager}).
7
+ * Els plugins poden llegir el token actual o forçar-ne el refresc; NO poden
8
+ * fixar els tokens inicials: això ho fa el host amb `setInitialTokens`, que
9
+ * s'exporta com a funció i només funciona una vegada.
10
10
  */
11
11
  export interface TokenManager {
12
12
  getToken: () => string;
13
13
  refreshToken: () => Promise<string>;
14
14
  }
15
15
 
16
- /**
17
- * Extensió host-only de {@link TokenManager}. Només el host del shell
18
- * (`primary/app`) usa `setInitialTokens` durant el bootstrap; és deliberadament
19
- * absent del tipus `TokenManager` que veuen els plugins.
20
- */
21
- export interface InternalTokenManager extends TokenManager {
22
- setInitialTokens: (access_token: string, refresh_token: string) => void;
23
- }
24
-
25
16
  let token: string;
26
17
  let refreshToken: string;
27
18
  let tokenInitialized = false;
28
19
 
29
- export class TokenManagerImpl implements InternalTokenManager {
20
+ export class TokenManagerImpl implements TokenManager {
30
21
  constructor(private readonly broker: PrimariaBroker) {}
31
22
 
32
23
  getUrlParams = (): URLSearchParams => {
@@ -1,13 +1,22 @@
1
1
  import { PrimariaShell } from "./UI/components/primaria-shell/primaria-shell";
2
- import { activityMonitor, sessionRefreshTimer, shellApi } from "./api/api";
2
+ import { activityMonitor, sessionRefreshTimer, shellApi, shellInternals } from "./api/api";
3
3
  import { EcapContext } from "./api/context-manager/context-manager";
4
4
  import { useFeatures } from "./features/bootstrapper";
5
5
  import { useLocalization } from "./locales";
6
6
  import { useUI } from "./UI/bootstrapper";
7
7
 
8
+ let shellInitialized = false;
9
+
10
+ /**
11
+ * Arrenca el shell. L'exporta el paquet perquè el host (`primary/app`, el sandbox) l'ha de cridar,
12
+ * i per això també el pot importar un plugin: només funciona una vegada, abans de carregar cap
13
+ * plugin, i no torna a funcionar fins que el shell s'ha desmuntat.
14
+ */
8
15
  export const initializeShell = (hostAppElement: HTMLElement, ecapContext?: EcapContext) => {
16
+ if (shellInitialized) throw new Error("Shell already initialized");
17
+ shellInitialized = true;
9
18
  if (ecapContext) {
10
- (shellApi.contextManager as any).initializeContext(ecapContext);
19
+ shellInternals.initializeContext(ecapContext);
11
20
  }
12
21
 
13
22
  activityMonitor.start();
@@ -18,3 +27,15 @@ export const initializeShell = (hostAppElement: HTMLElement, ecapContext?: EcapC
18
27
  const shell = new PrimariaShell();
19
28
  hostAppElement.appendChild(shell as any);
20
29
  };
30
+
31
+ /** Permet tornar a arrencar el shell un cop desmuntat. Només el crida `disposeShell`. */
32
+ export const resetShellInitialization = () => {
33
+ shellInitialized = false;
34
+ };
35
+
36
+ /**
37
+ * Fixa els tokens inicials quan el host els rep d'ECAP en lloc de la URL. Només funciona una
38
+ * vegada: a partir del primer token, llegit o fixat, llança `Token already initialized`.
39
+ */
40
+ export const setInitialTokens = (accessToken: string, refreshToken: string) =>
41
+ shellInternals.setInitialTokens(accessToken, refreshToken);
package/src/disposer.ts CHANGED
@@ -1,11 +1,13 @@
1
- import { activityMonitor, sessionRefreshTimer, shellApi } from "./api/api";
1
+ import { activityMonitor, sessionRefreshTimer, shellApi, shellInternals } from "./api/api";
2
+ import { resetShellInitialization } from "./bootstrapper";
2
3
  import { disposeFeatures } from "./features/bootstrapper";
3
4
  import { ExitShellPayload } from "./features/exit/request";
4
5
  export const disposeShell = async () => {
5
- await disposeFeatures(shellApi);
6
- shellApi.interactionService.dispose();
6
+ await disposeFeatures();
7
+ shellInternals.closeAllDialogs();
7
8
  sessionRefreshTimer.stop();
8
9
  activityMonitor.stop();
10
+ resetShellInitialization();
9
11
  };
10
12
 
11
13
  export const raiseCloseEvent = () => {
@@ -1,4 +1,4 @@
1
- import { PrimariaApi } from "../api/api";
1
+ import { PrimariaApi, shellInternals } from "../api/api";
2
2
  import { container } from "../infrastructure/ioc/container";
3
3
  import { TYPES } from "../infrastructure/ioc/types";
4
4
  import { disposeExitShell, useExitShell } from "./exit/bootstrapper";
@@ -12,10 +12,10 @@ export const useFeatures = (api: PrimariaApi) => {
12
12
  api.broker.send(new GetUserInfo());
13
13
  };
14
14
 
15
- export const disposeFeatures = async (api: PrimariaApi) => {
15
+ export const disposeFeatures = async () => {
16
16
  disposeGetUserInfo();
17
17
  disposeExitShell();
18
18
  container.unbindAll();
19
- await api.regionManager._destroy();
20
- api.broker._clear();
19
+ await shellInternals.destroyRegions();
20
+ shellInternals.clearBroker();
21
21
  };
@@ -1,10 +1,16 @@
1
1
  import { beforeEach, describe, expect, it, vi } from "vitest";
2
- import { PrimariaApi } from "../../api/api";
2
+ import { PrimariaApi, shellInternals } from "../../api/api";
3
3
  import { disposeShell, raiseCloseEvent, raiseCustomCloseEvent } from "../../disposer";
4
4
  import { disposePlugins } from "../../handle-plugins";
5
5
  import { ExitShellHandler } from "./handler";
6
6
  import { ExitShell } from "./request";
7
7
 
8
+ vi.mock("../../api/api", () => ({
9
+ shellInternals: {
10
+ removeAllViews: vi.fn(),
11
+ },
12
+ }));
13
+
8
14
  vi.mock("../../handle-plugins", () => ({
9
15
  disposePlugins: vi.fn(),
10
16
  }));
@@ -23,9 +29,6 @@ const createMockApi = (): PrimariaApi =>
23
29
  notificationService: {
24
30
  error: vi.fn(),
25
31
  },
26
- regionManager: {
27
- _removeAllViews: vi.fn().mockResolvedValue(undefined),
28
- },
29
32
  }) as any;
30
33
 
31
34
  describe("ExitShellHandler", () => {
@@ -36,6 +39,7 @@ describe("ExitShellHandler", () => {
36
39
  mockApi = createMockApi();
37
40
  handler = new ExitShellHandler(mockApi);
38
41
  vi.clearAllMocks();
42
+ vi.mocked(shellInternals.removeAllViews).mockReset().mockResolvedValue(undefined);
39
43
  });
40
44
 
41
45
  it("disposes and raises custom close event when canExit resolves true and message has ecapEvent", async () => {
@@ -133,7 +137,7 @@ describe("ExitShellHandler", () => {
133
137
  */
134
138
  it("desmunta totes les vistes abans de disposar els plugins", async () => {
135
139
  const order: string[] = [];
136
- (mockApi.regionManager._removeAllViews as any).mockImplementation(async () => {
140
+ vi.mocked(shellInternals.removeAllViews).mockImplementation(async () => {
137
141
  order.push("removeAllViews");
138
142
  });
139
143
  (disposePlugins as any).mockImplementation(async () => {
@@ -147,7 +151,7 @@ describe("ExitShellHandler", () => {
147
151
 
148
152
  it("no comenca a disposar fins que el desmuntatge ha acabat", async () => {
149
153
  let finishUnmount: () => void = () => {};
150
- (mockApi.regionManager._removeAllViews as any).mockImplementation(
154
+ vi.mocked(shellInternals.removeAllViews).mockImplementation(
151
155
  () =>
152
156
  new Promise<void>((resolve) => {
153
157
  finishUnmount = resolve;
@@ -168,7 +172,7 @@ describe("ExitShellHandler", () => {
168
172
 
169
173
  it("el limit de temps del tancament segueix cobrint tot el proces", async () => {
170
174
  vi.useFakeTimers();
171
- (mockApi.regionManager._removeAllViews as any).mockImplementation(() => new Promise<void>(() => {}));
175
+ vi.mocked(shellInternals.removeAllViews).mockImplementation(() => new Promise<void>(() => {}));
172
176
 
173
177
  const handled = handler.handle({} as ExitShell);
174
178
  await vi.advanceTimersByTimeAsync(10000);
@@ -1,5 +1,5 @@
1
1
  import { inject } from "inversify";
2
- import { PrimariaApi } from "../../api/api";
2
+ import { PrimariaApi, shellInternals } from "../../api/api";
3
3
  import { disposeShell, raiseCloseEvent, raiseCustomCloseEvent } from "../../disposer";
4
4
  import { disposePlugins } from "../../handle-plugins";
5
5
  import { TYPES } from "../../infrastructure/ioc/types";
@@ -40,7 +40,7 @@ export class ExitShellHandler {
40
40
  * no canvia.
41
41
  */
42
42
  private async unmountViewsAndDisposePlugins(): Promise<void> {
43
- await this.api.regionManager._removeAllViews();
43
+ await shellInternals.removeAllViews();
44
44
  await disposePlugins();
45
45
  }
46
46
 
@@ -34,7 +34,17 @@ const bundledActivityHistory: PluginDefinition = {
34
34
  importer: () => import("../../../plugins/activity-history/src/plugin"),
35
35
  };
36
36
 
37
+ let pluginsBootstrapped = false;
38
+
39
+ /**
40
+ * Carrega i inicialitza els plugins. L'exporta el paquet perquè el host l'ha de cridar, i per
41
+ * això també el pot importar un plugin: només funciona una vegada, i no torna a funcionar fins
42
+ * que `disposePlugins` els ha desmuntat. Així un plugin no pot carregar-ne d'altres amb la
43
+ * identitat que vulgui.
44
+ */
37
45
  export const bootstrapPlugins = async (plugins: PluginDefinition[], startup?: PrimariaStartupPlugin) => {
46
+ if (pluginsBootstrapped) throw new Error("Plugins already bootstrapped");
47
+ pluginsBootstrapped = true;
38
48
  const incoming = plugins ?? [];
39
49
  const provided = incoming.filter((plugin) => plugin.pluginId === CORE_FIRST_ID);
40
50
  const rest = incoming.filter((plugin) => plugin.pluginId !== CORE_FIRST_ID);
@@ -44,10 +54,7 @@ export const bootstrapPlugins = async (plugins: PluginDefinition[], startup?: Pr
44
54
  // rest, which run in parallel as before.
45
55
  const corePlugins = provided.length ? provided : [bundledActivityHistory];
46
56
 
47
- const coreBootstrapped = (await pluginBootstrapper(
48
- corePlugins,
49
- primariaApiFactory,
50
- )) as BootstrappedPlugin<PrimariaApi>[];
57
+ const coreBootstrapped = (await pluginBootstrapper(corePlugins, primariaApiFactory)) as BootstrappedPlugin<PrimariaApi>[];
51
58
  const restBootstrapped = rest.length
52
59
  ? ((await pluginBootstrapper(rest, primariaApiFactory)) as BootstrappedPlugin<PrimariaApi>[])
53
60
  : [];
@@ -56,7 +63,7 @@ export const bootstrapPlugins = async (plugins: PluginDefinition[], startup?: Pr
56
63
  handleStartupPlugin(bootstrappedPlugins as PrimariaBootstrappedPlugin[], startup);
57
64
  };
58
65
 
59
- export const handleStartupPlugin = (plugins: PrimariaBootstrappedPlugin[], startupPlugin?: PrimariaStartupPlugin) => {
66
+ const handleStartupPlugin = (plugins: PrimariaBootstrappedPlugin[], startupPlugin?: PrimariaStartupPlugin) => {
60
67
  if (plugins && plugins.length > 0 && startupPlugin?.pluginId) {
61
68
  const plugin = plugins.find(
62
69
  (plugin: PrimariaBootstrappedPlugin) => plugin?.apiInstance.pluginInfo.pluginId === startupPlugin.pluginId,
@@ -66,7 +73,10 @@ export const handleStartupPlugin = (plugins: PrimariaBootstrappedPlugin[], start
66
73
  };
67
74
 
68
75
  export const disposePlugins = async () => {
69
- return Promise.all(bootstrappedPlugins.map((plugin: PrimariaBootstrappedPlugin) => plugin?.dispose()));
76
+ const disposed = await Promise.all(bootstrappedPlugins.map((plugin: PrimariaBootstrappedPlugin) => plugin?.dispose()));
77
+ bootstrappedPlugins = [];
78
+ pluginsBootstrapped = false;
79
+ return disposed;
70
80
  };
71
81
 
72
82
  export type Plugin = PluginType<PrimariaApi>;
package/src/index.ts CHANGED
@@ -8,24 +8,33 @@ if (typeof document !== "undefined") {
8
8
  style.textContent = `${fontsCSS}\n${dssCSS}`;
9
9
  document.head.appendChild(style);
10
10
  }
11
- export * from "./bootstrapper";
12
- export * from "./disposer";
13
- export * from "./api/region-manager/regions";
11
+ // Arrencada del host. Tot el que s'exporta aquí ho pot importar també un plugin (és el mateix
12
+ // mòdul), així que només surt el que el host necessita, i només funciona una vegada:
13
+ // `initializeShell`, `setInitialTokens` i `bootstrapPlugins`. El tancament (`disposeShell`,
14
+ // `disposePlugins`, els events de tancament a ECAP) no s'exporta: un plugin que vol tancar envia
15
+ // `ExitShell`, que passa per l'`exitGuardManager`.
16
+ export { initializeShell, setInitialTokens } from "./bootstrapper";
14
17
  // Tipus de retorn de `regionManager.getRegisteredMainViews()`. El regionManager
15
18
  // arriba als plugins tipat via `PrimariaApi`; això només és per poder-lo anomenar.
16
19
  export type { RegisteredMainView } from "./api/region-manager/region-manager";
17
- export * from "./handle-plugins";
20
+ export { bootstrapPlugins } from "./handle-plugins";
21
+ export type {
22
+ Plugin,
23
+ PluginDefinition,
24
+ PluginInfo,
25
+ PrimariaPlugin,
26
+ PrimariaStartupPlugin,
27
+ } from "./handle-plugins";
18
28
  // Superfície pública explícita: `primariaApiFactory`, `activityMonitor` i
19
29
  // `sessionRefreshTimer` són singletons interns del shell i no s'exposen.
20
- // Les constants de regió (`shellRegions`, `clinicalMonitoringRegions`...) tampoc:
21
- // des de la v8 els plugins han d'usar `api.regionManager.regions.*`.
30
+ // Les constants de regió (`shellRegions`, `clinicalMonitoringRegions`, `activityHistoryRegions` i
31
+ // `administrativeManagementRegions`) tampoc: es van retirar a la v8 i els plugins fan servir
32
+ // `api.regionManager.regions.*`.
22
33
  // `shellApi` es manté només perquè els hosts (app/sandbox) l'usen per arrencar;
23
34
  // els plugins han d'usar l'`api` que reben a `initialize(api)`.
24
35
  export type { PrimariaApi } from "./api/api";
25
36
  export { PrimariaRegionHost, shellApi } from "./api/api";
26
- // `TokenManager` és el tipus públic per a plugins; `InternalTokenManager`
27
- // (amb `setInitialTokens`) és host-only, per al bootstrap de `primary/app`.
28
- export type { TokenManager, InternalTokenManager } from "./api/token-manager/token-manager";
37
+ export type { TokenManager } from "./api/token-manager/token-manager";
29
38
  export * from "./api/broker/primaria-broker";
30
39
  export type { PrimariaContextManager, EcapContext } from "./api/context-manager/context-manager";
31
40
  // Contractes de l'importador de dades estructurades. Una iniciativa que crida