@@ -46,24 +46,58 @@ function validateClassNameForTriggers(className, type) {
46
46
47
47
const _triggerStore = { } ;
48
48
49
- export function addFunction ( functionName , handler , validationHandler , applicationId ) {
49
+ const Category = {
50
+ Functions : 'Functions' ,
51
+ Validators : 'Validators' ,
52
+ Jobs : 'Jobs' ,
53
+ Triggers : 'Triggers'
54
+ }
55
+
56
+ function getStore ( category , name , applicationId ) {
57
+ const path = name . split ( '.' ) ;
58
+ path . splice ( - 1 ) ; // remove last component
50
59
applicationId = applicationId || Parse . applicationId ;
51
60
_triggerStore [ applicationId ] = _triggerStore [ applicationId ] || baseStore ( ) ;
52
- _triggerStore [ applicationId ] . Functions [ functionName ] = handler ;
53
- _triggerStore [ applicationId ] . Validators [ functionName ] = validationHandler ;
61
+ let store = _triggerStore [ applicationId ] [ category ] ;
62
+ for ( const component of path ) {
63
+ store = store [ component ] ;
64
+ if ( ! store ) {
65
+ return undefined ;
66
+ }
67
+ }
68
+ return store ;
69
+ }
70
+
71
+ function add ( category , name , handler , applicationId ) {
72
+ const lastComponent = name . split ( '.' ) . splice ( - 1 ) ;
73
+ const store = getStore ( category , name , applicationId ) ;
74
+ store [ lastComponent ] = handler ;
75
+ }
76
+
77
+ function remove ( category , name , applicationId ) {
78
+ const lastComponent = name . split ( '.' ) . splice ( - 1 ) ;
79
+ const store = getStore ( category , name , applicationId ) ;
80
+ delete store [ lastComponent ] ;
81
+ }
82
+
83
+ function get ( category , name , applicationId ) {
84
+ const lastComponent = name . split ( '.' ) . splice ( - 1 ) ;
85
+ const store = getStore ( category , name , applicationId ) ;
86
+ return store [ lastComponent ] ;
87
+ }
88
+
89
+ export function addFunction ( functionName , handler , validationHandler , applicationId ) {
90
+ add ( Category . Functions , functionName , handler , applicationId ) ;
91
+ add ( Category . Validators , functionName , validationHandler , applicationId ) ;
54
92
}
55
93
56
94
export function addJob ( jobName , handler , applicationId ) {
57
- applicationId = applicationId || Parse . applicationId ;
58
- _triggerStore [ applicationId ] = _triggerStore [ applicationId ] || baseStore ( ) ;
59
- _triggerStore [ applicationId ] . Jobs [ jobName ] = handler ;
95
+ add ( Category . Jobs , jobName , handler , applicationId ) ;
60
96
}
61
97
62
98
export function addTrigger ( type , className , handler , applicationId ) {
63
99
validateClassNameForTriggers ( className , type ) ;
64
- applicationId = applicationId || Parse . applicationId ;
65
- _triggerStore [ applicationId ] = _triggerStore [ applicationId ] || baseStore ( ) ;
66
- _triggerStore [ applicationId ] . Triggers [ type ] [ className ] = handler ;
100
+ add ( Category . Triggers , `${ type } .${ className } ` , handler , applicationId ) ;
67
101
}
68
102
69
103
export function addLiveQueryEventHandler ( handler , applicationId ) {
@@ -73,13 +107,11 @@ export function addLiveQueryEventHandler(handler, applicationId) {
73
107
}
74
108
75
109
export function removeFunction ( functionName , applicationId ) {
76
- applicationId = applicationId || Parse . applicationId ;
77
- delete _triggerStore [ applicationId ] . Functions [ functionName ]
110
+ remove ( Category . Functions , functionName , applicationId ) ;
78
111
}
79
112
80
113
export function removeTrigger ( type , className , applicationId ) {
81
- applicationId = applicationId || Parse . applicationId ;
82
- delete _triggerStore [ applicationId ] . Triggers [ type ] [ className ]
114
+ remove ( Category . Triggers , `${ type } .${ className } ` , applicationId ) ;
83
115
}
84
116
85
117
export function _unregisterAll ( ) {
@@ -90,34 +122,19 @@ export function getTrigger(className, triggerType, applicationId) {
90
122
if ( ! applicationId ) {
91
123
throw "Missing ApplicationID" ;
92
124
}
93
- var manager = _triggerStore [ applicationId ]
94
- if ( manager
95
- && manager . Triggers
96
- && manager . Triggers [ triggerType ]
97
- && manager . Triggers [ triggerType ] [ className ] ) {
98
- return manager . Triggers [ triggerType ] [ className ] ;
99
- }
100
- return undefined ;
125
+ return get ( Category . Triggers , `${ triggerType } .${ className } ` , applicationId ) ;
101
126
}
102
127
103
128
export function triggerExists ( className : string , type : string , applicationId : string ) : boolean {
104
129
return ( getTrigger ( className , type , applicationId ) != undefined ) ;
105
130
}
106
131
107
132
export function getFunction ( functionName , applicationId ) {
108
- var manager = _triggerStore [ applicationId ] ;
109
- if ( manager && manager . Functions ) {
110
- return manager . Functions [ functionName ] ;
111
- }
112
- return undefined ;
133
+ return get ( Category . Functions , functionName , applicationId ) ;
113
134
}
114
135
115
136
export function getJob ( jobName , applicationId ) {
116
- var manager = _triggerStore [ applicationId ] ;
117
- if ( manager && manager . Jobs ) {
118
- return manager . Jobs [ jobName ] ;
119
- }
120
- return undefined ;
137
+ return get ( Category . Jobs , jobName , applicationId ) ;
121
138
}
122
139
123
140
export function getJobs ( applicationId ) {
@@ -130,15 +147,11 @@ export function getJobs(applicationId) {
130
147
131
148
132
149
export function getValidator ( functionName , applicationId ) {
133
- var manager = _triggerStore [ applicationId ] ;
134
- if ( manager && manager . Validators ) {
135
- return manager . Validators [ functionName ] ;
136
- }
137
- return undefined ;
150
+ return get ( Category . Validators , functionName , applicationId ) ;
138
151
}
139
152
140
- export function getRequestObject ( triggerType , auth , parseObject , originalParseObject , config ) {
141
- var request = {
153
+ export function getRequestObject ( triggerType , auth , parseObject , originalParseObject , config , context ) {
154
+ const request = {
142
155
triggerName : triggerType ,
143
156
object : parseObject ,
144
157
master : false ,
@@ -151,6 +164,11 @@ export function getRequestObject(triggerType, auth, parseObject, originalParseOb
151
164
request . original = originalParseObject ;
152
165
}
153
166
167
+ if ( triggerType === Types . beforeSave || triggerType === Types . afterSave ) {
168
+ // Set a copy of the context on the request object.
169
+ request . context = Object . assign ( { } , context ) ;
170
+ }
171
+
154
172
if ( ! auth ) {
155
173
return request ;
156
174
}
@@ -390,17 +408,20 @@ export function maybeRunQueryTrigger(triggerType, className, restWhere, restOpti
390
408
// Resolves to an object, empty or containing an object key. A beforeSave
391
409
// trigger will set the object key to the rest format object to save.
392
410
// originalParseObject is optional, we only need that for before/afterSave functions
393
- export function maybeRunTrigger ( triggerType , auth , parseObject , originalParseObject , config ) {
411
+ export function maybeRunTrigger ( triggerType , auth , parseObject , originalParseObject , config , context ) {
394
412
if ( ! parseObject ) {
395
413
return Promise . resolve ( { } ) ;
396
414
}
397
415
return new Promise ( function ( resolve , reject ) {
398
416
var trigger = getTrigger ( parseObject . className , triggerType , config . applicationId ) ;
399
417
if ( ! trigger ) return resolve ( ) ;
400
- var request = getRequestObject ( triggerType , auth , parseObject , originalParseObject , config ) ;
418
+ var request = getRequestObject ( triggerType , auth , parseObject , originalParseObject , config , context ) ;
401
419
var { success, error } = getResponseObject ( request , ( object ) => {
402
420
logTriggerSuccessBeforeHook (
403
421
triggerType , parseObject . className , parseObject . toJSON ( ) , object , auth ) ;
422
+ if ( triggerType === Types . beforeSave || triggerType === Types . afterSave ) {
423
+ Object . assign ( context , request . context ) ;
424
+ }
404
425
resolve ( object ) ;
405
426
} , ( error ) => {
406
427
logTriggerErrorBeforeHook (
0 commit comments