@openmrs/esm-react-utils 8.0.1-pre.3485 → 8.0.1-pre.3498

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.
@@ -1,3 +1,3 @@
1
- [0] Successfully compiled: 52 files with swc (155.35ms)
1
+ [0] Successfully compiled: 52 files with swc (124.08ms)
2
2
  [0] swc --strip-leading-paths src -d dist exited with code 0
3
3
  [1] tsc --project tsconfig.build.json exited with code 0
package/dist/useStore.js CHANGED
@@ -1,5 +1,4 @@
1
- /** @module @category Store */ import { subscribeTo } from "@openmrs/esm-state";
2
- import { useEffect, useMemo, useState } from "react";
1
+ /** @module @category Store */ import { useCallback, useMemo, useSyncExternalStore } from "react";
3
2
  function bindActions(store, actions) {
4
3
  if (typeof actions == 'function') {
5
4
  actions = actions(store);
@@ -17,11 +16,18 @@ function bindActions(store, actions) {
17
16
  }
18
17
  const defaultSelectFunction = ()=>(x)=>x;
19
18
  function useStore(store, select = defaultSelectFunction(), actions) {
20
- const [state, setState] = useState(()=>select(store.getState()));
21
- useEffect(()=>subscribeTo(store, select, setState), [
19
+ // Use useSyncExternalStore to subscribe synchronously during render
20
+ // This ensures React can properly track all state updates
21
+ const subscribe = useCallback((callback)=>{
22
+ return store.subscribe(callback);
23
+ }, [
24
+ store
25
+ ]);
26
+ const getSnapshot = useCallback(()=>select(store.getState()), [
22
27
  store,
23
28
  select
24
29
  ]);
30
+ const state = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
25
31
  let boundActions = useMemo(()=>actions ? bindActions(store, actions) : {}, [
26
32
  store,
27
33
  actions
@@ -44,8 +50,9 @@ function useStore(store, select = defaultSelectFunction(), actions) {
44
50
  * custom hook for a specific store.
45
51
  */ function createUseStore(store) {
46
52
  function useStore(actions) {
47
- const [state, set] = useState(store.getState());
48
- useEffect(()=>store.subscribe((state)=>set(state)), []);
53
+ const subscribe = useCallback((callback)=>store.subscribe(callback), []);
54
+ const getSnapshot = useCallback(()=>store.getState(), []);
55
+ const state = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
49
56
  let boundActions = useMemo(()=>actions ? bindActions(store, actions) : {}, [
50
57
  actions
51
58
  ]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openmrs/esm-react-utils",
3
- "version": "8.0.1-pre.3485",
3
+ "version": "8.0.1-pre.3498",
4
4
  "license": "MPL-2.0",
5
5
  "description": "React utilities for OpenMRS.",
6
6
  "type": "module",
@@ -78,17 +78,17 @@
78
78
  "swr": "2.x"
79
79
  },
80
80
  "devDependencies": {
81
- "@openmrs/esm-api": "8.0.1-pre.3485",
82
- "@openmrs/esm-config": "8.0.1-pre.3485",
83
- "@openmrs/esm-context": "8.0.1-pre.3485",
84
- "@openmrs/esm-emr-api": "8.0.1-pre.3485",
85
- "@openmrs/esm-error-handling": "8.0.1-pre.3485",
86
- "@openmrs/esm-extensions": "8.0.1-pre.3485",
87
- "@openmrs/esm-feature-flags": "8.0.1-pre.3485",
88
- "@openmrs/esm-globals": "8.0.1-pre.3485",
89
- "@openmrs/esm-navigation": "8.0.1-pre.3485",
90
- "@openmrs/esm-state": "8.0.1-pre.3485",
91
- "@openmrs/esm-utils": "8.0.1-pre.3485",
81
+ "@openmrs/esm-api": "8.0.1-pre.3498",
82
+ "@openmrs/esm-config": "8.0.1-pre.3498",
83
+ "@openmrs/esm-context": "8.0.1-pre.3498",
84
+ "@openmrs/esm-emr-api": "8.0.1-pre.3498",
85
+ "@openmrs/esm-error-handling": "8.0.1-pre.3498",
86
+ "@openmrs/esm-extensions": "8.0.1-pre.3498",
87
+ "@openmrs/esm-feature-flags": "8.0.1-pre.3498",
88
+ "@openmrs/esm-globals": "8.0.1-pre.3498",
89
+ "@openmrs/esm-navigation": "8.0.1-pre.3498",
90
+ "@openmrs/esm-state": "8.0.1-pre.3498",
91
+ "@openmrs/esm-utils": "8.0.1-pre.3498",
92
92
  "@swc/cli": "^0.7.7",
93
93
  "@swc/core": "^1.11.29",
94
94
  "concurrently": "^9.1.2",
package/src/useStore.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  /** @module @category Store */
2
- import { subscribeTo } from '@openmrs/esm-state';
3
- import { useEffect, useMemo, useState } from 'react';
2
+ import { useCallback, useMemo, useSyncExternalStore } from 'react';
4
3
  import type { StoreApi } from 'zustand';
5
4
 
6
5
  export type ActionFunction<T> = (state: T, ...args: any[]) => Partial<T>;
@@ -67,8 +66,18 @@ function useStore<T, U, A extends Actions<T>>(
67
66
  select: (state: T) => U = defaultSelectFunction(),
68
67
  actions?: A,
69
68
  ) {
70
- const [state, setState] = useState<U>(() => select(store.getState()));
71
- useEffect(() => subscribeTo(store, select, setState), [store, select]);
69
+ // Use useSyncExternalStore to subscribe synchronously during render
70
+ // This ensures React can properly track all state updates
71
+ const subscribe = useCallback(
72
+ (callback: () => void) => {
73
+ return store.subscribe(callback);
74
+ },
75
+ [store],
76
+ );
77
+
78
+ const getSnapshot = useCallback(() => select(store.getState()), [store, select]);
79
+
80
+ const state = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
72
81
 
73
82
  let boundActions: BoundActions<T, Actions<T>> = useMemo(
74
83
  () => (actions ? bindActions(store, actions) : {}),
@@ -97,8 +106,10 @@ function createUseStore<T>(store: StoreApi<T>) {
97
106
  function useStore<A extends Actions<T>>(actions: A): T & BoundActions<T, A>;
98
107
  function useStore<A extends Actions<T>>(actions?: A): T & BoundActions<T, A>;
99
108
  function useStore<A extends Actions<T>>(actions?: A) {
100
- const [state, set] = useState(store.getState());
101
- useEffect(() => store.subscribe((state) => set(state)), []);
109
+ const subscribe = useCallback((callback: () => void) => store.subscribe(callback), []);
110
+ const getSnapshot = useCallback(() => store.getState(), []);
111
+ const state = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
112
+
102
113
  let boundActions: BoundActions<T, Actions<T>> = useMemo(
103
114
  () => (actions ? bindActions(store, actions) : {}),
104
115
  [actions],