@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.
- package/.turbo/turbo-build.log +1 -1
- package/dist/Extension.d.ts.map +1 -1
- package/dist/Extension.js +86 -44
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/useAssignedExtensionIds.d.ts +1 -1
- package/dist/useAssignedExtensionIds.d.ts.map +1 -1
- package/dist/useAssignedExtensionIds.js +7 -14
- package/dist/useAssignedExtensions.d.ts +13 -1
- package/dist/useAssignedExtensions.d.ts.map +1 -1
- package/dist/useAssignedExtensions.js +31 -4
- package/dist/useCandidateExtensions.d.ts +11 -0
- package/dist/useCandidateExtensions.d.ts.map +1 -0
- package/dist/useCandidateExtensions.js +12 -0
- package/dist/useExtensionSlot.d.ts.map +1 -1
- package/dist/useExtensionSlot.js +8 -13
- package/dist/useExtensionSlotStore.d.ts +0 -1
- package/dist/useExtensionSlotStore.d.ts.map +1 -1
- package/dist/useExtensionSlotStore.js +19 -2
- package/dist/useShallowStableValue.d.ts +13 -0
- package/dist/useShallowStableValue.d.ts.map +1 -0
- package/dist/useShallowStableValue.js +19 -0
- package/dist/useStore.d.ts.map +1 -1
- package/dist/useStore.js +25 -11
- package/package.json +23 -23
- package/src/Extension.tsx +110 -54
- package/src/extensions.test.tsx +112 -0
- package/src/index.ts +2 -0
- package/src/useAssignedExtensionIds.ts +5 -15
- package/src/useAssignedExtensions.ts +30 -3
- package/src/useCandidateExtensions.ts +15 -0
- package/src/useExtensionSlot.ts +7 -17
- package/src/useExtensionSlotStore.ts +23 -3
- package/src/useShallowStableValue.test.ts +69 -0
- package/src/useShallowStableValue.ts +24 -0
- package/src/useStore.test.ts +39 -0
- 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
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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
|
-
|
|
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;
|