33 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44
55import type { DebugOptions } from "./debug_options" ;
6- import type Dispatcher from "./dispatcher" ;
6+ import Dispatcher from "./dispatcher" ;
77import type MetricsDatabase from "./metrics/database" ;
88import type EventsDatabase from "./metrics/events_database" ;
99import type PingsDatabase from "./pings/database" ;
@@ -23,21 +23,20 @@ import type PingsDatabase from "./pings/database";
2323export class Context {
2424 private static _instance : Context ;
2525
26- private _dispatcher ! : Dispatcher ;
26+ private _dispatcher ! : Dispatcher | null ;
2727
2828 private _uploadEnabled ! : boolean ;
2929 private _metricsDatabase ! : MetricsDatabase ;
3030 private _eventsDatabase ! : EventsDatabase ;
3131 private _pingsDatabase ! : PingsDatabase ;
3232
3333 private _applicationId ! : string ;
34- private _initialized ! : boolean ;
34+ private _initialized : boolean ;
3535
3636 private _debugOptions ! : DebugOptions ;
3737
3838 private constructor ( ) {
39- // Intentionally empty, exclusively defined to mark the
40- // constructor as private.
39+ this . _initialized = false ;
4140 }
4241
4342 static get instance ( ) : Context {
@@ -48,7 +47,33 @@ export class Context {
4847 return Context . _instance ;
4948 }
5049
50+ /**
51+ * **Test-only API**
52+ *
53+ * Resets the Context to an uninitialized state.
54+ */
55+ static async testUninitialize ( ) : Promise < void > {
56+ // Clear the dispatcher queue and return the dispatcher back to an uninitialized state.
57+ if ( Context . instance . _dispatcher ) {
58+ await Context . instance . _dispatcher . testUninitialize ( ) ;
59+ }
60+
61+ // Due to the test requirement of keeping storage in place,
62+ // we can't simply wipe out the full `Context` instance.
63+ // The closest thing we can do is making the dispatcher `null`.
64+ Context . instance . _dispatcher = null ;
65+ Context . initialized = false ;
66+ }
67+
5168 static get dispatcher ( ) : Dispatcher {
69+ // Create a dispatcher if one isn't available already.
70+ // This is required since the dispatcher may be used
71+ // earlier than Glean initialization, so we can't rely
72+ // on `Glean.initialize` to set it.
73+ if ( ! Context . instance . _dispatcher ) {
74+ Context . instance . _dispatcher = new Dispatcher ( ) ;
75+ }
76+
5277 return Context . instance . _dispatcher ;
5378 }
5479
0 commit comments