@openmrs/esm-react-utils 4.0.3-pre.367 → 4.0.3-pre.371

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/jest.config.js CHANGED
@@ -4,7 +4,6 @@ module.exports = {
4
4
  },
5
5
  setupFilesAfterEnv: ["<rootDir>/src/setup-tests.js"],
6
6
  moduleNameMapper: {
7
- "lodash-es": "lodash",
8
7
  "^lodash-es/(.*)$": "lodash/$1",
9
8
  "@openmrs/esm-error-handling":
10
9
  "<rootDir>/__mocks__/openmrs-esm-error-handling.mock.ts",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openmrs/esm-react-utils",
3
- "version": "4.0.3-pre.367",
3
+ "version": "4.0.3-pre.371",
4
4
  "license": "MPL-2.0",
5
5
  "description": "React utilities for OpenMRS.",
6
6
  "browser": "dist/openmrs-esm-react-utils.js",
@@ -55,18 +55,21 @@
55
55
  "react-i18next": "11.x"
56
56
  },
57
57
  "devDependencies": {
58
- "@openmrs/esm-api": "^4.0.3-pre.367",
59
- "@openmrs/esm-config": "^4.0.3-pre.367",
60
- "@openmrs/esm-error-handling": "^4.0.3-pre.367",
61
- "@openmrs/esm-extensions": "^4.0.3-pre.367",
62
- "@openmrs/esm-globals": "^4.0.3-pre.367",
58
+ "@openmrs/esm-api": "^4.0.3-pre.371",
59
+ "@openmrs/esm-config": "^4.0.3-pre.371",
60
+ "@openmrs/esm-error-handling": "^4.0.3-pre.371",
61
+ "@openmrs/esm-extensions": "^4.0.3-pre.371",
62
+ "@openmrs/esm-globals": "^4.0.3-pre.371",
63
63
  "dayjs": "^1.10.8",
64
64
  "i18next": "^19.6.0",
65
+ "jest": "28.1.0",
66
+ "jest-cli": "28.1.0",
67
+ "jest-environment-jsdom": "28.1.0",
65
68
  "react": "^18.1.0",
66
69
  "react-dom": "^18.1.0",
67
70
  "react-i18next": "^11.16.9",
68
71
  "rxjs": "^6.5.3",
69
72
  "unistore": "^3.5.2"
70
73
  },
71
- "gitHead": "221f83fbf13c71ba941d4db5526369ea5e1e10ad"
74
+ "gitHead": "ad139e1b9853124a8328b443b309ad3a869fe9f3"
72
75
  }
package/src/Extension.tsx CHANGED
@@ -1,15 +1,14 @@
1
1
  import { renderExtension } from "@openmrs/esm-extensions";
2
2
  import React, {
3
- PropsWithChildren,
4
3
  useCallback,
5
4
  useContext,
6
5
  useEffect,
7
6
  useRef,
8
7
  useState,
9
8
  } from "react";
10
- import { Parcel } from "single-spa";
9
+ import type { Parcel } from "single-spa";
11
10
  import { ComponentContext } from ".";
12
- import { ExtensionData } from "./ComponentContext";
11
+ import type { ExtensionData } from "./ComponentContext";
13
12
 
14
13
  export type ExtensionProps = {
15
14
  state?: Record<string, any>;
@@ -41,7 +40,8 @@ export const Extension: React.FC<ExtensionProps> = ({
41
40
  }) => {
42
41
  const [domElement, setDomElement] = useState<HTMLDivElement>();
43
42
  const { extension } = useContext(ComponentContext);
44
- const parcel = useRef<Parcel | null>();
43
+ const parcel = useRef<Parcel | null>(null);
44
+ const updatePromise = useRef<Promise<null> | null>(null);
45
45
 
46
46
  useEffect(() => {
47
47
  if (wrap) {
@@ -58,7 +58,7 @@ export const Extension: React.FC<ExtensionProps> = ({
58
58
  }, []);
59
59
 
60
60
  const ref = useCallback(
61
- (node) => {
61
+ (node: HTMLDivElement) => {
62
62
  setDomElement(node);
63
63
  },
64
64
  [setDomElement]
@@ -66,30 +66,69 @@ export const Extension: React.FC<ExtensionProps> = ({
66
66
 
67
67
  useEffect(() => {
68
68
  if (domElement != null && extension && !parcel.current) {
69
- parcel.current = renderExtension(
69
+ renderExtension(
70
70
  domElement,
71
71
  extension.extensionSlotName,
72
72
  extension.extensionSlotModuleName,
73
73
  extension.extensionId,
74
74
  undefined,
75
75
  state
76
- );
76
+ ).then((newParcel) => {
77
+ parcel.current = newParcel;
78
+ });
79
+
77
80
  return () => {
78
- parcel.current && parcel.current.unmount();
81
+ if (parcel && parcel.current) {
82
+ const status = parcel.current.getStatus();
83
+ switch (status) {
84
+ case "MOUNTING":
85
+ parcel.current.mountPromise.then(parcel.current.unmount);
86
+ break;
87
+ case "MOUNTED":
88
+ parcel.current.unmount();
89
+ break;
90
+ case "UPDATING":
91
+ if (updatePromise.current) {
92
+ updatePromise.current.then(() => {
93
+ if (
94
+ parcel.current &&
95
+ parcel.current.getStatus() === "MOUNTED"
96
+ ) {
97
+ parcel.current.unmount();
98
+ }
99
+ });
100
+ }
101
+ }
102
+ }
79
103
  };
80
104
  }
105
+
106
+ // we intentionally do not re-run this hook if state gets updated
107
+ // state updates are handled in the next useEffect hook
108
+ // eslint-disable-next-line eslintreact-hooks/exhaustive-deps
81
109
  }, [
82
110
  extension?.extensionSlotName,
83
111
  extension?.extensionId,
84
112
  extension?.extensionSlotModuleName,
85
- state,
86
113
  domElement,
87
114
  ]);
88
115
 
89
116
  useEffect(() => {
90
117
  if (parcel.current && parcel.current.update) {
91
- parcel.current.update({ ...state });
118
+ if (parcel.current.getStatus() === "MOUNTED") {
119
+ parcel.current.update({ ...state });
120
+ } else if (parcel.current.getStatus() === "MOUNTING") {
121
+ parcel.current.mountPromise.then(() => {
122
+ if (parcel.current && parcel.current.update) {
123
+ updatePromise.current = parcel.current.update({ ...state });
124
+ }
125
+ });
126
+ }
92
127
  }
128
+
129
+ return () => {
130
+ updatePromise.current = null;
131
+ };
93
132
  }, [parcel.current, state]);
94
133
 
95
134
  // The extension is rendered into the `<div>`. The `<div>` has relative
@@ -1,12 +1,5 @@
1
1
  import "@testing-library/jest-dom/extend-expect";
2
2
 
3
- window.System = {
4
- import: (name) => import(name),
5
- resolve: jest.fn().mockImplementation(() => {
6
- throw new Error("file not found");
7
- }),
8
- };
9
-
10
3
  window.openmrsBase = "/openmrs";
11
4
  window.spaBase = "/spa";
12
5
  window.getOpenmrsSpaBase = () => "/openmrs/spa/";