@@ -95,6 +95,7 @@ function RestWrite(config, auth, className, query, data, originalData, clientSDK
9595  // Shared SchemaController to be reused to reduce the number of loadSchema() calls per request 
9696  // Once set the schemaData should be immutable 
9797  this . validSchemaController  =  null ; 
98+   this . pendingOps  =  { } ; 
9899} 
99100
100101// A convenient method to perform all the steps of processing the 
@@ -225,18 +226,11 @@ RestWrite.prototype.runBeforeSaveTrigger = function () {
225226    return  Promise . resolve ( ) ; 
226227  } 
227228
228-   // Cloud code gets a bit of extra data for its objects 
229-   var  extraData  =  {  className : this . className  } ; 
230-   if  ( this . query  &&  this . query . objectId )  { 
231-     extraData . objectId  =  this . query . objectId ; 
232-   } 
229+   const  {  originalObject,  updatedObject }  =  this . buildParseObjects ( ) ; 
233230
234-   let  originalObject  =  null ; 
235-   const  updatedObject  =  this . buildUpdatedObject ( extraData ) ; 
236-   if  ( this . query  &&  this . query . objectId )  { 
237-     // This is an update for existing object. 
238-     originalObject  =  triggers . inflate ( extraData ,  this . originalData ) ; 
239-   } 
231+   const  stateController  =  Parse . CoreManager . getObjectStateController ( ) ; 
232+   const  [ pending ]  =  stateController . getPendingOps ( updatedObject . _getStateIdentifier ( ) ) ; 
233+   this . pendingOps  =  {  ...pending  } ; 
240234
241235  return  Promise . resolve ( ) 
242236    . then ( ( )  =>  { 
@@ -1531,20 +1525,7 @@ RestWrite.prototype.runAfterSaveTrigger = function () {
15311525    return  Promise . resolve ( ) ; 
15321526  } 
15331527
1534-   var  extraData  =  {  className : this . className  } ; 
1535-   if  ( this . query  &&  this . query . objectId )  { 
1536-     extraData . objectId  =  this . query . objectId ; 
1537-   } 
1538- 
1539-   // Build the original object, we only do this for a update write. 
1540-   let  originalObject ; 
1541-   if  ( this . query  &&  this . query . objectId )  { 
1542-     originalObject  =  triggers . inflate ( extraData ,  this . originalData ) ; 
1543-   } 
1544- 
1545-   // Build the inflated object, different from beforeSave, originalData is not empty 
1546-   // since developers can change data in the beforeSave. 
1547-   const  updatedObject  =  this . buildUpdatedObject ( extraData ) ; 
1528+   const  {  originalObject,  updatedObject }  =  this . buildParseObjects ( ) ; 
15481529  updatedObject . _handleSaveResponse ( this . response . response ,  this . response . status  ||  200 ) ; 
15491530
15501531  this . config . database . loadSchema ( ) . then ( schemaController  =>  { 
@@ -1569,8 +1550,15 @@ RestWrite.prototype.runAfterSaveTrigger = function () {
15691550      this . context 
15701551    ) 
15711552    . then ( result  =>  { 
1572-       if  ( result  &&  typeof  result  ===  'object' )  { 
1553+       const  jsonReturned  =  result  &&  ! result . _toFullJSON ; 
1554+       if  ( jsonReturned )  { 
1555+         this . pendingOps  =  { } ; 
15731556        this . response . response  =  result ; 
1557+       }  else  { 
1558+         this . response . response  =  this . _updateResponseWithData ( 
1559+           ( result  ||  updatedObject ) . _toFullJSON ( ) , 
1560+           this . data 
1561+         ) ; 
15741562      } 
15751563    } ) 
15761564    . catch ( function  ( err )  { 
@@ -1604,7 +1592,13 @@ RestWrite.prototype.sanitizedData = function () {
16041592} ; 
16051593
16061594// Returns an updated copy of the object 
1607- RestWrite . prototype . buildUpdatedObject  =  function  ( extraData )  { 
1595+ RestWrite . prototype . buildParseObjects  =  function  ( )  { 
1596+   const  extraData  =  {  className : this . className ,  objectId : this . query ?. objectId  } ; 
1597+   let  originalObject ; 
1598+   if  ( this . query  &&  this . query . objectId )  { 
1599+     originalObject  =  triggers . inflate ( extraData ,  this . originalData ) ; 
1600+   } 
1601+ 
16081602  const  className  =  Parse . Object . fromJSON ( extraData ) ; 
16091603  const  readOnlyAttributes  =  className . constructor . readOnlyAttributes 
16101604    ? className . constructor . readOnlyAttributes ( ) 
@@ -1642,7 +1636,7 @@ RestWrite.prototype.buildUpdatedObject = function (extraData) {
16421636    delete  sanitized [ attribute ] ; 
16431637  } 
16441638  updatedObject . set ( sanitized ) ; 
1645-   return  updatedObject ; 
1639+   return  {   updatedObject,  originalObject  } ; 
16461640} ; 
16471641
16481642RestWrite . prototype . cleanUserAuthData  =  function  ( )  { 
@@ -1662,6 +1656,15 @@ RestWrite.prototype.cleanUserAuthData = function () {
16621656} ; 
16631657
16641658RestWrite . prototype . _updateResponseWithData  =  function  ( response ,  data )  { 
1659+   const  {  updatedObject }  =  this . buildParseObjects ( ) ; 
1660+   const  stateController  =  Parse . CoreManager . getObjectStateController ( ) ; 
1661+   const  [ pending ]  =  stateController . getPendingOps ( updatedObject . _getStateIdentifier ( ) ) ; 
1662+   for  ( const  key  in  this . pendingOps )  { 
1663+     if  ( ! pending [ key ] )  { 
1664+       data [ key ]  =  this . originalData  ? this . originalData [ key ]  : {  __op : 'Delete'  } ; 
1665+       this . storage . fieldsChangedByTrigger . push ( key ) ; 
1666+     } 
1667+   } 
16651668  if  ( _ . isEmpty ( this . storage . fieldsChangedByTrigger ) )  { 
16661669    return  response ; 
16671670  } 
0 commit comments