@cripty2001/utils 0.0.81 → 0.0.83

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.
@@ -104,23 +104,33 @@ function useSynced(def, value, setValue) {
104
104
  if ((value !== undefined && setValue === undefined) ||
105
105
  (value === undefined && setValue !== undefined))
106
106
  throw new Error('Either value and setValue must be provided, or both must be undefined');
107
- if (value !== undefined && setValue !== undefined) {
108
- (0, react_1.useEffect)(() => {
109
- // console.log('syncing value downstream', value, '->', v);
110
- if ((0, lodash_1.isEqual)(v, value))
111
- return;
112
- // console.log("SYNCED")
113
- setV(value);
114
- }, [value, v, setV]);
115
- (0, react_1.useEffect)(() => {
116
- // console.log('syncing value upstream', v, '->', value);
117
- if ((0, lodash_1.isEqual)(v, value))
118
- return;
119
- // console.log("SYNCED");
120
- setValue(v);
121
- }, [v, value, setValue]);
122
- }
123
- return [v, setV];
107
+ const setValueRef = (0, react_1.useRef)(setValue);
108
+ (0, react_1.useEffect)(() => {
109
+ setValueRef.current = setValue;
110
+ }, [setValue]);
111
+ // Only sync downstream (external -> local)
112
+ (0, react_1.useEffect)(() => {
113
+ if (value === undefined)
114
+ return;
115
+ if ((0, lodash_1.isEqual)(v, value))
116
+ return;
117
+ console.log('SYNC DOWNSTREAM', value);
118
+ setV(value);
119
+ }, [value]);
120
+ // Return a setter that updates both
121
+ const syncedSetter = (0, react_1.useCallback)((newValue) => {
122
+ setV(prev => {
123
+ const resolved = typeof newValue === 'function'
124
+ ? newValue(prev)
125
+ : newValue;
126
+ // Update external immediately if available
127
+ if (setValueRef.current) {
128
+ setValueRef.current(resolved);
129
+ }
130
+ return resolved;
131
+ });
132
+ }, []);
133
+ return [v, syncedSetter];
124
134
  }
125
135
  /**
126
136
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cripty2001/utils",
3
- "version": "0.0.81",
3
+ "version": "0.0.83",
4
4
  "description": "Internal Set of utils. If you need them use them, otherwise go to the next package ;)",
5
5
  "homepage": "https://github.com/cripty2001/utils#readme",
6
6
  "bugs": {