Skip to content

Commit e9390ab

Browse files
committed
more fixes for PATCH_DOC
1 parent 07b313b commit e9390ab

File tree

4 files changed

+119
-44
lines changed

4 files changed

+119
-44
lines changed

dist/index.cjs.js

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,28 @@ function isIncrementHelper(payload) {
331331
payload.isIncrementHelper === true);
332332
}
333333

334+
/**
335+
* Creates the params needed to $set a target based on a nested.path
336+
*
337+
* @param {object} target
338+
* @param {string} path
339+
* @param {*} value
340+
* @returns {[object, string, any]}
341+
*/
342+
function getSetParams(target, path, value) {
343+
var _a;
344+
var pathParts = path.split('.');
345+
var prop = pathParts.pop();
346+
var pathParent = pathParts.join('.');
347+
var targetForNestedProp = pathToProp(target, pathParent);
348+
if (targetForNestedProp === undefined) {
349+
// the target doesn't have an object ready at this level to set the value to
350+
// so we need to step down a level and try again
351+
return getSetParams(target, pathParent, (_a = {}, _a[prop] = value, _a));
352+
}
353+
var valueToSet = value;
354+
return [targetForNestedProp, prop, valueToSet];
355+
}
334356
/**
335357
* a function returning the mutations object
336358
*
@@ -404,6 +426,7 @@ function pluginMutations (userState) {
404426
}
405427
},
406428
PATCH_DOC: function (state, patches) {
429+
var _a;
407430
// Get the state prop ref
408431
var ref = state._conf.statePropName ? state[state._conf.statePropName] : state;
409432
if (state._conf.firestoreRefType.toLowerCase() === 'collection') {
@@ -423,20 +446,17 @@ function pluginMutations (userState) {
423446
// const refPropsPicked = filter(ref, Object.keys(patches))
424447
// const patchesSanitised = merge({ extensions: [convertHelpers] }, refPropsPicked, patches)
425448
var patchesFlat = flatten.flattenObject(patches);
426-
for (var _i = 0, _a = Object.entries(patchesFlat); _i < _a.length; _i++) {
427-
var _b = _a[_i], path = _b[0], value = _b[1];
449+
for (var _i = 0, _b = Object.entries(patchesFlat); _i < _b.length; _i++) {
450+
var _c = _b[_i], path = _c[0], value = _c[1];
428451
var targetVal = pathToProp(ref, path);
429452
var newVal = convertHelpers(targetVal, value);
430453
// do not update anything if the values are the same
431454
// this is technically not required, because vue takes care of this as well:
432455
if (targetVal === newVal)
433-
return;
456+
continue;
434457
// update just the nested value
435-
var pathParts = path.split('.');
436-
var prop = pathParts.pop();
437-
var pathParent = pathParts.join('');
438-
var targetForNestedProp = pathToProp(ref, pathParent);
439-
this._vm.$set(targetForNestedProp, prop, newVal);
458+
var setParams = getSetParams(ref, path, newVal);
459+
(_a = this._vm).$set.apply(_a, setParams);
440460
}
441461
},
442462
DELETE_DOC: function (state, id) {

dist/index.esm.js

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,28 @@ function isIncrementHelper(payload) {
324324
payload.isIncrementHelper === true);
325325
}
326326

327+
/**
328+
* Creates the params needed to $set a target based on a nested.path
329+
*
330+
* @param {object} target
331+
* @param {string} path
332+
* @param {*} value
333+
* @returns {[object, string, any]}
334+
*/
335+
function getSetParams(target, path, value) {
336+
var _a;
337+
var pathParts = path.split('.');
338+
var prop = pathParts.pop();
339+
var pathParent = pathParts.join('.');
340+
var targetForNestedProp = pathToProp(target, pathParent);
341+
if (targetForNestedProp === undefined) {
342+
// the target doesn't have an object ready at this level to set the value to
343+
// so we need to step down a level and try again
344+
return getSetParams(target, pathParent, (_a = {}, _a[prop] = value, _a));
345+
}
346+
var valueToSet = value;
347+
return [targetForNestedProp, prop, valueToSet];
348+
}
327349
/**
328350
* a function returning the mutations object
329351
*
@@ -397,6 +419,7 @@ function pluginMutations (userState) {
397419
}
398420
},
399421
PATCH_DOC: function (state, patches) {
422+
var _a;
400423
// Get the state prop ref
401424
var ref = state._conf.statePropName ? state[state._conf.statePropName] : state;
402425
if (state._conf.firestoreRefType.toLowerCase() === 'collection') {
@@ -416,20 +439,17 @@ function pluginMutations (userState) {
416439
// const refPropsPicked = filter(ref, Object.keys(patches))
417440
// const patchesSanitised = merge({ extensions: [convertHelpers] }, refPropsPicked, patches)
418441
var patchesFlat = flattenObject(patches);
419-
for (var _i = 0, _a = Object.entries(patchesFlat); _i < _a.length; _i++) {
420-
var _b = _a[_i], path = _b[0], value = _b[1];
442+
for (var _i = 0, _b = Object.entries(patchesFlat); _i < _b.length; _i++) {
443+
var _c = _b[_i], path = _c[0], value = _c[1];
421444
var targetVal = pathToProp(ref, path);
422445
var newVal = convertHelpers(targetVal, value);
423446
// do not update anything if the values are the same
424447
// this is technically not required, because vue takes care of this as well:
425448
if (targetVal === newVal)
426-
return;
449+
continue;
427450
// update just the nested value
428-
var pathParts = path.split('.');
429-
var prop = pathParts.pop();
430-
var pathParent = pathParts.join('');
431-
var targetForNestedProp = pathToProp(ref, pathParent);
432-
this._vm.$set(targetForNestedProp, prop, newVal);
451+
var setParams = getSetParams(ref, path, newVal);
452+
(_a = this._vm).$set.apply(_a, setParams);
433453
}
434454
},
435455
DELETE_DOC: function (state, id) {

src/module/mutations.ts

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,38 @@ import { isArrayHelper } from '../utils/arrayHelpers'
1010
import { isIncrementHelper } from '../utils/incrementHelper'
1111
import getStateWithSync from './state'
1212

13+
function convertHelpers (originVal, newVal) {
14+
if (isArray(originVal) && isArrayHelper(newVal)) {
15+
newVal = newVal.executeOn(originVal)
16+
}
17+
if (isNumber(originVal) && isIncrementHelper(newVal)) {
18+
newVal = newVal.executeOn(originVal)
19+
}
20+
return newVal // always return newVal as fallback!!
21+
}
22+
23+
/**
24+
* Creates the params needed to $set a target based on a nested.path
25+
*
26+
* @param {object} target
27+
* @param {string} path
28+
* @param {*} value
29+
* @returns {[object, string, any]}
30+
*/
31+
function getSetParams (target: object, path: string, value: any): [object, string, any] {
32+
const pathParts = path.split('.')
33+
const prop = pathParts.pop()
34+
const pathParent = pathParts.join('.')
35+
const targetForNestedProp = pathToProp(target, pathParent)
36+
if (targetForNestedProp === undefined) {
37+
// the target doesn't have an object ready at this level to set the value to
38+
// so we need to step down a level and try again
39+
return getSetParams(target, pathParent, { [prop]: value })
40+
}
41+
const valueToSet = value
42+
return [targetForNestedProp, prop, valueToSet]
43+
}
44+
1345
/**
1446
* a function returning the mutations object
1547
*
@@ -82,33 +114,16 @@ export default function (userState: object): AnyObject {
82114
}
83115
if (!ref) return logError('patch-no-ref')
84116

85-
function convertHelpers (originVal, newVal) {
86-
if (isArray(originVal) && isArrayHelper(newVal)) {
87-
newVal = newVal.executeOn(originVal)
88-
}
89-
if (isNumber(originVal) && isIncrementHelper(newVal)) {
90-
newVal = newVal.executeOn(originVal)
91-
}
92-
return newVal // always return newVal as fallback!!
93-
}
94-
95-
// const refPropsPicked = filter(ref, Object.keys(patches))
96-
// const patchesSanitised = merge({ extensions: [convertHelpers] }, refPropsPicked, patches)
97-
98117
const patchesFlat = flattenObject(patches)
99-
100118
for (const [path, value] of Object.entries(patchesFlat)) {
101119
const targetVal = pathToProp(ref, path)
102120
const newVal = convertHelpers(targetVal, value)
103121
// do not update anything if the values are the same
104122
// this is technically not required, because vue takes care of this as well:
105-
if (targetVal === newVal) return
123+
if (targetVal === newVal) continue
106124
// update just the nested value
107-
const pathParts = path.split('.')
108-
const prop = pathParts.pop()
109-
const pathParent = pathParts.join('')
110-
const targetForNestedProp = pathToProp(ref, pathParent)
111-
this._vm.$set(targetForNestedProp, prop, newVal)
125+
const setParams = getSetParams(ref, path, newVal)
126+
this._vm.$set(...setParams)
112127
}
113128
},
114129
DELETE_DOC (state, id) {

test/helpers/index.cjs.js

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,28 @@ function isIncrementHelper(payload) {
850850
payload.isIncrementHelper === true);
851851
}
852852

853+
/**
854+
* Creates the params needed to $set a target based on a nested.path
855+
*
856+
* @param {object} target
857+
* @param {string} path
858+
* @param {*} value
859+
* @returns {[object, string, any]}
860+
*/
861+
function getSetParams(target, path, value) {
862+
var _a;
863+
var pathParts = path.split('.');
864+
var prop = pathParts.pop();
865+
var pathParent = pathParts.join('.');
866+
var targetForNestedProp = pathToProp(target, pathParent);
867+
if (targetForNestedProp === undefined) {
868+
// the target doesn't have an object ready at this level to set the value to
869+
// so we need to step down a level and try again
870+
return getSetParams(target, pathParent, (_a = {}, _a[prop] = value, _a));
871+
}
872+
var valueToSet = value;
873+
return [targetForNestedProp, prop, valueToSet];
874+
}
853875
/**
854876
* a function returning the mutations object
855877
*
@@ -923,6 +945,7 @@ function pluginMutations (userState) {
923945
}
924946
},
925947
PATCH_DOC: function (state, patches) {
948+
var _a;
926949
// Get the state prop ref
927950
var ref = state._conf.statePropName ? state[state._conf.statePropName] : state;
928951
if (state._conf.firestoreRefType.toLowerCase() === 'collection') {
@@ -942,20 +965,17 @@ function pluginMutations (userState) {
942965
// const refPropsPicked = filter(ref, Object.keys(patches))
943966
// const patchesSanitised = merge({ extensions: [convertHelpers] }, refPropsPicked, patches)
944967
var patchesFlat = flatten.flattenObject(patches);
945-
for (var _i = 0, _a = Object.entries(patchesFlat); _i < _a.length; _i++) {
946-
var _b = _a[_i], path = _b[0], value = _b[1];
968+
for (var _i = 0, _b = Object.entries(patchesFlat); _i < _b.length; _i++) {
969+
var _c = _b[_i], path = _c[0], value = _c[1];
947970
var targetVal = pathToProp(ref, path);
948971
var newVal = convertHelpers(targetVal, value);
949972
// do not update anything if the values are the same
950973
// this is technically not required, because vue takes care of this as well:
951974
if (targetVal === newVal)
952-
return;
975+
continue;
953976
// update just the nested value
954-
var pathParts = path.split('.');
955-
var prop = pathParts.pop();
956-
var pathParent = pathParts.join('');
957-
var targetForNestedProp = pathToProp(ref, pathParent);
958-
this._vm.$set(targetForNestedProp, prop, newVal);
977+
var setParams = getSetParams(ref, path, newVal);
978+
(_a = this._vm).$set.apply(_a, setParams);
959979
}
960980
},
961981
DELETE_DOC: function (state, id) {

0 commit comments

Comments
 (0)