@@ -11,7 +11,9 @@ var vuexEasyAccess = require('vuex-easy-access');
1111var isWhat = require ( 'is-what' ) ;
1212var copy = _interopDefault ( require ( 'copy-anything' ) ) ;
1313var mergeAnything = require ( 'merge-anything' ) ;
14- var flatten = _interopDefault ( require ( 'flatten-anything' ) ) ;
14+ var flatten = require ( 'flatten-anything' ) ;
15+ var flatten__default = _interopDefault ( flatten ) ;
16+ var pathToProp = _interopDefault ( require ( 'path-to-prop' ) ) ;
1517var compareAnything = require ( 'compare-anything' ) ;
1618var findAndReplaceAnything = require ( 'find-and-replace-anything' ) ;
1719var filter = _interopDefault ( require ( 'filter-anything' ) ) ;
@@ -329,6 +331,37 @@ function isIncrementHelper(payload) {
329331 payload . isIncrementHelper === true ) ;
330332}
331333
334+ function convertHelpers ( originVal , newVal ) {
335+ if ( isWhat . isArray ( originVal ) && isArrayHelper ( newVal ) ) {
336+ newVal = newVal . executeOn ( originVal ) ;
337+ }
338+ if ( isWhat . isNumber ( originVal ) && isIncrementHelper ( newVal ) ) {
339+ newVal = newVal . executeOn ( originVal ) ;
340+ }
341+ return newVal ; // always return newVal as fallback!!
342+ }
343+ /**
344+ * Creates the params needed to $set a target based on a nested.path
345+ *
346+ * @param {object } target
347+ * @param {string } path
348+ * @param {* } value
349+ * @returns {[object, string, any] }
350+ */
351+ function getSetParams ( target , path , value ) {
352+ var _a ;
353+ var pathParts = path . split ( '.' ) ;
354+ var prop = pathParts . pop ( ) ;
355+ var pathParent = pathParts . join ( '.' ) ;
356+ var targetForNestedProp = pathToProp ( target , pathParent ) ;
357+ if ( targetForNestedProp === undefined ) {
358+ // the target doesn't have an object ready at this level to set the value to
359+ // so we need to step down a level and try again
360+ return getSetParams ( target , pathParent , ( _a = { } , _a [ prop ] = value , _a ) ) ;
361+ }
362+ var valueToSet = value ;
363+ return [ targetForNestedProp , prop , valueToSet ] ;
364+ }
332365/**
333366 * a function returning the mutations object
334367 *
@@ -402,29 +435,27 @@ function pluginMutations (userState) {
402435 }
403436 } ,
404437 PATCH_DOC : function ( state , patches ) {
405- var _this = this ;
438+ var _a ;
406439 // Get the state prop ref
407440 var ref = state . _conf . statePropName ? state [ state . _conf . statePropName ] : state ;
408441 if ( state . _conf . firestoreRefType . toLowerCase ( ) === 'collection' ) {
409442 ref = ref [ patches . id ] ;
410443 }
411444 if ( ! ref )
412445 return error ( 'patch-no-ref' ) ;
413- return Object . keys ( patches ) . forEach ( function ( key ) {
414- var newVal = patches [ key ] ;
415- // Merge if exists
416- function helpers ( originVal , newVal ) {
417- if ( isWhat . isArray ( originVal ) && isArrayHelper ( newVal ) ) {
418- newVal = newVal . executeOn ( originVal ) ;
419- }
420- if ( isWhat . isNumber ( originVal ) && isIncrementHelper ( newVal ) ) {
421- newVal = newVal . executeOn ( originVal ) ;
422- }
423- return newVal ; // always return newVal as fallback!!
424- }
425- newVal = mergeAnything . merge ( { extensions : [ helpers ] } , ref [ key ] , patches [ key ] ) ;
426- _this . _vm . $set ( ref , key , newVal ) ;
427- } ) ;
446+ var patchesFlat = flatten . flattenObject ( patches ) ;
447+ for ( var _i = 0 , _b = Object . entries ( patchesFlat ) ; _i < _b . length ; _i ++ ) {
448+ var _c = _b [ _i ] , path = _c [ 0 ] , value = _c [ 1 ] ;
449+ var targetVal = pathToProp ( ref , path ) ;
450+ var newVal = convertHelpers ( targetVal , value ) ;
451+ // do not update anything if the values are the same
452+ // this is technically not required, because vue takes care of this as well:
453+ if ( targetVal === newVal )
454+ continue ;
455+ // update just the nested value
456+ var setParams = getSetParams ( ref , path , newVal ) ;
457+ ( _a = this . _vm ) . $set . apply ( _a , setParams ) ;
458+ }
428459 } ,
429460 DELETE_DOC : function ( state , id ) {
430461 if ( state . _conf . firestoreRefType . toLowerCase ( ) !== 'collection' )
@@ -1286,9 +1317,9 @@ function pluginActions (Firebase) {
12861317 var getters = _a . getters , commit = _a . commit ;
12871318 var defaultValues = getters . defaultValues ;
12881319 var searchTarget = getters . collectionMode ? getters . storeRef [ doc . id ] : getters . storeRef ;
1289- var compareInfo = compareAnything . compareObjectProps ( flatten ( doc ) , // presentIn 0
1290- flatten ( defaultValues ) , // presentIn 1
1291- flatten ( searchTarget ) // presentIn 2
1320+ var compareInfo = compareAnything . compareObjectProps ( flatten__default ( doc ) , // presentIn 0
1321+ flatten__default ( defaultValues ) , // presentIn 1
1322+ flatten__default ( searchTarget ) // presentIn 2
12921323 ) ;
12931324 Object . keys ( compareInfo . presentIn ) . forEach ( function ( prop ) {
12941325 // don't worry about props not in fillables
@@ -1918,7 +1949,7 @@ function pluginGetters (Firebase) {
19181949 patchData . updated_by = state . _sync . userId ;
19191950 // clean up item
19201951 var cleanedPatchData = filter ( patchData , getters . fillables , getters . guard ) ;
1921- var itemToUpdate = flatten ( cleanedPatchData ) ;
1952+ var itemToUpdate = flatten__default ( cleanedPatchData ) ;
19221953 // add id (required to get ref later at apiHelpers.ts)
19231954 // @ts -ignore
19241955 itemToUpdate . id = id ;
0 commit comments