@openmrs/esm-react-utils 10.0.1-pre.5263 → 10.0.1-pre.5295

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 (38) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/dist/Extension.d.ts.map +1 -1
  3. package/dist/Extension.js +86 -44
  4. package/dist/index.d.ts +2 -0
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +2 -0
  7. package/dist/useAssignedExtensionIds.d.ts +1 -1
  8. package/dist/useAssignedExtensionIds.d.ts.map +1 -1
  9. package/dist/useAssignedExtensionIds.js +7 -14
  10. package/dist/useAssignedExtensions.d.ts +13 -1
  11. package/dist/useAssignedExtensions.d.ts.map +1 -1
  12. package/dist/useAssignedExtensions.js +31 -4
  13. package/dist/useCandidateExtensions.d.ts +11 -0
  14. package/dist/useCandidateExtensions.d.ts.map +1 -0
  15. package/dist/useCandidateExtensions.js +12 -0
  16. package/dist/useExtensionSlot.d.ts.map +1 -1
  17. package/dist/useExtensionSlot.js +8 -13
  18. package/dist/useExtensionSlotStore.d.ts +0 -1
  19. package/dist/useExtensionSlotStore.d.ts.map +1 -1
  20. package/dist/useExtensionSlotStore.js +19 -2
  21. package/dist/useShallowStableValue.d.ts +13 -0
  22. package/dist/useShallowStableValue.d.ts.map +1 -0
  23. package/dist/useShallowStableValue.js +19 -0
  24. package/dist/useStore.d.ts.map +1 -1
  25. package/dist/useStore.js +25 -11
  26. package/package.json +23 -23
  27. package/src/Extension.tsx +110 -54
  28. package/src/extensions.test.tsx +112 -0
  29. package/src/index.ts +2 -0
  30. package/src/useAssignedExtensionIds.ts +5 -15
  31. package/src/useAssignedExtensions.ts +30 -3
  32. package/src/useCandidateExtensions.ts +15 -0
  33. package/src/useExtensionSlot.ts +7 -17
  34. package/src/useExtensionSlotStore.ts +23 -3
  35. package/src/useShallowStableValue.test.ts +69 -0
  36. package/src/useShallowStableValue.ts +24 -0
  37. package/src/useStore.test.ts +39 -0
  38. package/src/useStore.ts +17 -10
package/src/useStore.ts CHANGED
@@ -44,10 +44,14 @@ function bindActions<T>(store: StoreApi<T>, actions: Actions<T>): BoundActions<T
44
44
  return bound;
45
45
  }
46
46
 
47
- const defaultSelectFunction =
48
- <T, U>() =>
49
- (x: T) =>
50
- x as unknown as U;
47
+ /**
48
+ * A single shared identity function, rather than one built per call: `getSnapshot` below takes
49
+ * the selector as a dependency, so a fresh default each render would leave it unstable for every
50
+ * caller that doesn't pass a selector of its own.
51
+ */
52
+ const identitySelect = (x: unknown) => x;
53
+
54
+ const defaultSelectFunction = <T, U>() => identitySelect as (x: T) => U;
51
55
 
52
56
  function useStore<T>(store: StoreApi<T>): T;
53
57
  function useStore<T, U>(store: StoreApi<T>, select: (state: T) => U): U;
@@ -79,12 +83,15 @@ function useStore<T, U, A extends Actions<T>>(
79
83
 
80
84
  const state = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
81
85
 
82
- let boundActions: BoundActions<T, Actions<T>> = useMemo(
83
- () => (actions ? bindActions(store, actions) : {}),
86
+ let boundActions: BoundActions<T, Actions<T>> | null = useMemo(
87
+ () => (actions ? bindActions(store, actions) : null),
84
88
  [store, actions],
85
89
  );
86
90
 
87
- return { ...state, ...boundActions };
91
+ // Returned as-is when there are no actions to merge in, so the reference stays stable for as
92
+ // long as the selected value does. That value is not a copy — it is whatever the selector
93
+ // returned, usually the store's own state.
94
+ return useMemo(() => (boundActions ? { ...state, ...boundActions } : state), [state, boundActions]);
88
95
  }
89
96
 
90
97
  /**
@@ -110,12 +117,12 @@ function createUseStore<T>(store: StoreApi<T>) {
110
117
  const getSnapshot = useCallback(() => store.getState(), []);
111
118
  const state = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
112
119
 
113
- let boundActions: BoundActions<T, Actions<T>> = useMemo(
114
- () => (actions ? bindActions(store, actions) : {}),
120
+ let boundActions: BoundActions<T, Actions<T>> | null = useMemo(
121
+ () => (actions ? bindActions(store, actions) : null),
115
122
  [actions],
116
123
  );
117
124
 
118
- return { ...state, ...boundActions };
125
+ return useMemo(() => (boundActions ? { ...state, ...boundActions } : state), [state, boundActions]);
119
126
  }
120
127
 
121
128
  return useStore;