@@ -27,7 +27,7 @@ class PingType implements CommonPingData {
2727 // execute and synchronize with the submission API.
2828 private resolveTestPromiseFunction ?: PromiseCallback ;
2929 private rejectTestPromiseFunction ?: PromiseCallback ;
30- private testValidator ?: ValidatorFunction ;
30+ private testCallback ?: ValidatorFunction ;
3131
3232 constructor ( meta : CommonPingData ) {
3333 this . name = meta . name ;
@@ -59,69 +59,55 @@ class PingType implements CommonPingData {
5959 // other dispatched tasks. Unfortunately, this causes a deadlock.
6060 // In order to work around that problem, we kick off validation
6161 // right before the actual submission takes place, through another
62- // async function (and not through the dispatcher). We then await
63- // in the dispatched submission task on the related promise, which
64- // will always resolve due to the use of `finally`.
65- if ( this . testValidator ) {
66- const cleanup = ( ) => {
67- this . resolveTestPromiseFunction = undefined ;
68- this . rejectTestPromiseFunction = undefined ;
69- this . testValidator = undefined ;
70- } ;
71-
72- this . testValidator ( reason )
62+ // async function (and not through the dispatcher). After validation
63+ // is complete, regardless of the outcome, the ping submission is
64+ // finally triggered.
65+ if ( this . testCallback ) {
66+ this . testCallback ( reason )
7367 . then ( ( ) => {
74- // Temporarily store the function and then clean up, so that fast consecutive
75- // calls to `testBeforeNextSubmit` don't fail because things are still set
76- // after the calling promise is resolved.
77- const resolver = this . resolveTestPromiseFunction ;
78- cleanup ( ) ;
79-
80- this . internalSubmit ( reason , resolver ) ;
68+ PingType . _private_internalSubmit ( this , reason , this . resolveTestPromiseFunction ) ;
8169 } )
8270 . catch ( e => {
8371 console . error ( `There was an error validating "${ this . name } " (${ reason ?? "no reason" } ):` , e ) ;
84-
85- // Temporarily store the function and then clean up, so that fast consecutive
86- // calls to `testBeforeNextSubmit` don't fail because things are still set
87- // after the calling promise is resolved.
88- const rejecter = this . rejectTestPromiseFunction ;
89- cleanup ( ) ;
90-
91- this . internalSubmit ( reason , rejecter ) ;
72+ PingType . _private_internalSubmit ( this , reason , this . rejectTestPromiseFunction ) ;
9273 } ) ;
9374 } else {
94- this . internalSubmit ( reason ) ;
75+ PingType . _private_internalSubmit ( this , reason ) ;
9576 }
9677 }
9778
98- private internalSubmit ( reason ?: string , testResolver ?: PromiseCallback ) : void {
79+ private static _private_internalSubmit ( instance : PingType , reason ?: string , testResolver ?: PromiseCallback ) : void {
9980 Context . dispatcher . launch ( async ( ) => {
10081 if ( ! Context . initialized ) {
10182 console . info ( "Glean must be initialized before submitting pings." ) ;
10283 return ;
10384 }
10485
105- if ( ! Context . uploadEnabled && ! this . isDeletionRequest ( ) ) {
86+ if ( ! Context . uploadEnabled && ! instance . isDeletionRequest ( ) ) {
10687 console . info ( "Glean disabled: not submitting pings. Glean may still submit the deletion-request ping." ) ;
10788 return ;
10889 }
10990
11091 let correctedReason = reason ;
111- if ( reason && ! this . reasonCodes . includes ( reason ) ) {
92+ if ( reason && ! instance . reasonCodes . includes ( reason ) ) {
11293 console . error ( `Invalid reason code ${ reason } from ${ this . name } . Ignoring.` ) ;
11394 correctedReason = undefined ;
11495 }
11596
11697 const identifier = generateUUIDv4 ( ) ;
117- await collectAndStorePing ( identifier , this , correctedReason ) ;
98+ await collectAndStorePing ( identifier , instance , correctedReason ) ;
11899
119100 // This guarantees that, when running tests, the promise returned by
120101 // `testBeforeNextSubmit` is resolved after the ping is collected: this is
121102 // needed to make sure calling the testing APIs on metrics behave consistently
122103 // if tests run fast.
123104 if ( testResolver ) {
124105 testResolver ( ) ;
106+
107+ // Finally clean up!
108+ instance . resolveTestPromiseFunction = undefined ;
109+ instance . rejectTestPromiseFunction = undefined ;
110+ instance . testCallback = undefined ;
125111 }
126112 } ) ;
127113 }
@@ -133,22 +119,22 @@ class PingType implements CommonPingData {
133119 *
134120 * TODO: Only allow this function to be called on test mode (depends on Bug 1682771).
135121 *
136- * @param validatorFn The asynchronous validation function to run in order to validate
137- * the ping content.
122+ * @param callbackFn The asynchronous validation function to run in order to validate
123+ * the ping content.
138124 *
139125 * @returns A `Promise` resolved when the ping is collected and the validation function
140126 * is executed.
141127 */
142- async testBeforeNextSubmit ( validatorFn : ValidatorFunction ) : Promise < void > {
143- if ( this . testValidator ) {
128+ async testBeforeNextSubmit ( callbackFn : ValidatorFunction ) : Promise < void > {
129+ if ( this . testCallback ) {
144130 console . error ( `There is an existing test call for ping "${ this . name } ". Ignoring.` ) ;
145131 return ;
146132 }
147133
148134 return new Promise ( ( resolve , reject ) => {
149135 this . resolveTestPromiseFunction = resolve ;
150136 this . rejectTestPromiseFunction = reject ;
151- this . testValidator = validatorFn ;
137+ this . testCallback = callbackFn ;
152138 } ) ;
153139 }
154140}
0 commit comments