@@ -46,24 +46,58 @@ function validateClassNameForTriggers(className, type) {
4646
4747const _triggerStore = { } ;
4848
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
5059 applicationId = applicationId || Parse . applicationId ;
5160 _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 ) ;
5492}
5593
5694export 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 ) ;
6096}
6197
6298export function addTrigger ( type , className , handler , applicationId ) {
6399 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 ) ;
67101}
68102
69103export function addLiveQueryEventHandler ( handler , applicationId ) {
@@ -73,13 +107,11 @@ export function addLiveQueryEventHandler(handler, applicationId) {
73107}
74108
75109export function removeFunction ( functionName , applicationId ) {
76- applicationId = applicationId || Parse . applicationId ;
77- delete _triggerStore [ applicationId ] . Functions [ functionName ]
110+ remove ( Category . Functions , functionName , applicationId ) ;
78111}
79112
80113export function removeTrigger ( type , className , applicationId ) {
81- applicationId = applicationId || Parse . applicationId ;
82- delete _triggerStore [ applicationId ] . Triggers [ type ] [ className ]
114+ remove ( Category . Triggers , `${ type } .${ className } ` , applicationId ) ;
83115}
84116
85117export function _unregisterAll ( ) {
@@ -90,34 +122,19 @@ export function getTrigger(className, triggerType, applicationId) {
90122 if ( ! applicationId ) {
91123 throw "Missing ApplicationID" ;
92124 }
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 ) ;
101126}
102127
103128export function triggerExists ( className : string , type : string , applicationId : string ) : boolean {
104129 return ( getTrigger ( className , type , applicationId ) != undefined ) ;
105130}
106131
107132export 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 ) ;
113134}
114135
115136export 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 ) ;
121138}
122139
123140export function getJobs ( applicationId ) {
@@ -130,15 +147,11 @@ export function getJobs(applicationId) {
130147
131148
132149export 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 ) ;
138151}
139152
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 = {
142155 triggerName : triggerType ,
143156 object : parseObject ,
144157 master : false ,
@@ -151,6 +164,11 @@ export function getRequestObject(triggerType, auth, parseObject, originalParseOb
151164 request . original = originalParseObject ;
152165 }
153166
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+
154172 if ( ! auth ) {
155173 return request ;
156174 }
@@ -390,17 +408,20 @@ export function maybeRunQueryTrigger(triggerType, className, restWhere, restOpti
390408// Resolves to an object, empty or containing an object key. A beforeSave
391409// trigger will set the object key to the rest format object to save.
392410// 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 ) {
394412 if ( ! parseObject ) {
395413 return Promise . resolve ( { } ) ;
396414 }
397415 return new Promise ( function ( resolve , reject ) {
398416 var trigger = getTrigger ( parseObject . className , triggerType , config . applicationId ) ;
399417 if ( ! trigger ) return resolve ( ) ;
400- var request = getRequestObject ( triggerType , auth , parseObject , originalParseObject , config ) ;
418+ var request = getRequestObject ( triggerType , auth , parseObject , originalParseObject , config , context ) ;
401419 var { success, error } = getResponseObject ( request , ( object ) => {
402420 logTriggerSuccessBeforeHook (
403421 triggerType , parseObject . className , parseObject . toJSON ( ) , object , auth ) ;
422+ if ( triggerType === Types . beforeSave || triggerType === Types . afterSave ) {
423+ Object . assign ( context , request . context ) ;
424+ }
404425 resolve ( object ) ;
405426 } , ( error ) => {
406427 logTriggerErrorBeforeHook (
0 commit comments