@@ -95,6 +95,7 @@ function RestWrite(config, auth, className, query, data, originalData, clientSDK
95
95
// Shared SchemaController to be reused to reduce the number of loadSchema() calls per request
96
96
// Once set the schemaData should be immutable
97
97
this . validSchemaController = null ;
98
+ this . pendingOps = { } ;
98
99
}
99
100
100
101
// A convenient method to perform all the steps of processing the
@@ -225,18 +226,11 @@ RestWrite.prototype.runBeforeSaveTrigger = function () {
225
226
return Promise . resolve ( ) ;
226
227
}
227
228
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 ( ) ;
233
230
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 } ;
240
234
241
235
return Promise . resolve ( )
242
236
. then ( ( ) => {
@@ -1531,20 +1525,7 @@ RestWrite.prototype.runAfterSaveTrigger = function () {
1531
1525
return Promise . resolve ( ) ;
1532
1526
}
1533
1527
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 ( ) ;
1548
1529
updatedObject . _handleSaveResponse ( this . response . response , this . response . status || 200 ) ;
1549
1530
1550
1531
this . config . database . loadSchema ( ) . then ( schemaController => {
@@ -1569,8 +1550,15 @@ RestWrite.prototype.runAfterSaveTrigger = function () {
1569
1550
this . context
1570
1551
)
1571
1552
. then ( result => {
1572
- if ( result && typeof result === 'object' ) {
1553
+ const jsonReturned = result && ! result . _toFullJSON ;
1554
+ if ( jsonReturned ) {
1555
+ this . pendingOps = { } ;
1573
1556
this . response . response = result ;
1557
+ } else {
1558
+ this . response . response = this . _updateResponseWithData (
1559
+ ( result || updatedObject ) . _toFullJSON ( ) ,
1560
+ this . data
1561
+ ) ;
1574
1562
}
1575
1563
} )
1576
1564
. catch ( function ( err ) {
@@ -1604,7 +1592,13 @@ RestWrite.prototype.sanitizedData = function () {
1604
1592
} ;
1605
1593
1606
1594
// 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
+
1608
1602
const className = Parse . Object . fromJSON ( extraData ) ;
1609
1603
const readOnlyAttributes = className . constructor . readOnlyAttributes
1610
1604
? className . constructor . readOnlyAttributes ( )
@@ -1642,7 +1636,7 @@ RestWrite.prototype.buildUpdatedObject = function (extraData) {
1642
1636
delete sanitized [ attribute ] ;
1643
1637
}
1644
1638
updatedObject . set ( sanitized ) ;
1645
- return updatedObject ;
1639
+ return { updatedObject, originalObject } ;
1646
1640
} ;
1647
1641
1648
1642
RestWrite . prototype . cleanUserAuthData = function ( ) {
@@ -1662,6 +1656,15 @@ RestWrite.prototype.cleanUserAuthData = function () {
1662
1656
} ;
1663
1657
1664
1658
RestWrite . 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
+ }
1665
1668
if ( _ . isEmpty ( this . storage . fieldsChangedByTrigger ) ) {
1666
1669
return response ;
1667
1670
}
0 commit comments