mobx-vue-bridge 1.2.0 → 1.3.0
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/package.json +1 -1
- package/src/mobxVueBridge.js +27 -7
package/package.json
CHANGED
package/src/mobxVueBridge.js
CHANGED
|
@@ -214,17 +214,27 @@ export function useMobxBridge(mobxObject, options = {}) {
|
|
|
214
214
|
* array operations like shift(), unshift(), splice() which modify multiple indices.
|
|
215
215
|
* This ensures data correctness at the cost of a microtask delay.
|
|
216
216
|
*
|
|
217
|
+
* IMPORTANT: The proxy wraps the value stored in propertyRefs[prop].value, which is
|
|
218
|
+
* a clone. When nested mutations occur, we update the clone in-place, then trigger
|
|
219
|
+
* a sync back to MobX by re-assigning the entire cloned structure.
|
|
220
|
+
*
|
|
217
221
|
* @param {object|array} value - The nested value to wrap in a proxy
|
|
218
|
-
* @param {string} prop - The parent property name for error messages
|
|
222
|
+
* @param {string} prop - The parent property name for error messages and sync
|
|
223
|
+
* @param {function} getRoot - Function that returns the current root value from propertyRef
|
|
219
224
|
* @returns {Proxy} Proxied object/array with reactive mutation handling
|
|
220
225
|
*/
|
|
221
|
-
const createDeepProxy = (value, prop) => {
|
|
226
|
+
const createDeepProxy = (value, prop, getRoot = null) => {
|
|
222
227
|
// Don't proxy built-in objects that should remain unchanged
|
|
223
228
|
if (value instanceof Date || value instanceof RegExp || value instanceof Map ||
|
|
224
229
|
value instanceof Set || value instanceof WeakMap || value instanceof WeakSet) {
|
|
225
230
|
return value;
|
|
226
231
|
}
|
|
227
232
|
|
|
233
|
+
// If no getRoot provided, use the default which gets from propertyRefs
|
|
234
|
+
if (!getRoot) {
|
|
235
|
+
getRoot = () => propertyRefs[prop].value;
|
|
236
|
+
}
|
|
237
|
+
|
|
228
238
|
// Track pending updates to batch array mutations
|
|
229
239
|
let updatePending = false;
|
|
230
240
|
|
|
@@ -235,7 +245,7 @@ export function useMobxBridge(mobxObject, options = {}) {
|
|
|
235
245
|
if (result && typeof result === 'object' &&
|
|
236
246
|
!(result instanceof Date || result instanceof RegExp || result instanceof Map ||
|
|
237
247
|
result instanceof Set || result instanceof WeakMap || result instanceof WeakSet)) {
|
|
238
|
-
return createDeepProxy(result, prop);
|
|
248
|
+
return createDeepProxy(result, prop, getRoot);
|
|
239
249
|
}
|
|
240
250
|
return result;
|
|
241
251
|
},
|
|
@@ -243,9 +253,10 @@ export function useMobxBridge(mobxObject, options = {}) {
|
|
|
243
253
|
// Check if direct mutation is allowed
|
|
244
254
|
if (!allowDirectMutation) {
|
|
245
255
|
warnDirectMutation(`${prop}.${String(key)}`);
|
|
246
|
-
return
|
|
256
|
+
return true; // Must return true to avoid TypeError in strict mode
|
|
247
257
|
}
|
|
248
258
|
|
|
259
|
+
// Update the target in-place (this modifies the clone in propertyRefs[prop].value)
|
|
249
260
|
target[key] = val;
|
|
250
261
|
|
|
251
262
|
// Batch updates to avoid corrupting in-progress array operations
|
|
@@ -254,12 +265,21 @@ export function useMobxBridge(mobxObject, options = {}) {
|
|
|
254
265
|
updatePending = true;
|
|
255
266
|
queueMicrotask(() => {
|
|
256
267
|
updatePending = false;
|
|
268
|
+
// The root value has already been modified in-place above (target[key] = val)
|
|
269
|
+
// Now we need to trigger Vue reactivity and sync to MobX
|
|
270
|
+
|
|
271
|
+
// Clone the root to create a new reference for Vue reactivity
|
|
272
|
+
// This ensures Vue detects the change
|
|
273
|
+
const rootValue = getRoot();
|
|
274
|
+
const cloned = clone(rootValue);
|
|
275
|
+
|
|
257
276
|
// Update the Vue ref to trigger reactivity
|
|
258
|
-
propertyRefs[prop].value =
|
|
259
|
-
|
|
277
|
+
propertyRefs[prop].value = cloned;
|
|
278
|
+
|
|
279
|
+
// Update MobX immediately with the cloned value
|
|
260
280
|
updatingFromVue.add(prop);
|
|
261
281
|
try {
|
|
262
|
-
mobxObject[prop] =
|
|
282
|
+
mobxObject[prop] = cloned;
|
|
263
283
|
} finally {
|
|
264
284
|
updatingFromVue.delete(prop);
|
|
265
285
|
}
|