@@ -18,6 +18,8 @@ export class Context {
1818 private _dbPath ! : string ;
1919 private _fixturesPath ! : string ;
2020 _docsService ! : DocumentManagementService ;
21+ private _initPromise : Promise < void > | null = null ;
22+ private _initialized = false ;
2123
2224 constructor ( ) { }
2325
@@ -111,15 +113,28 @@ export class Context {
111113 }
112114
113115 async initialize ( ) {
114- this . _latestRelease = await this . _getLatestRelease ( ) ;
115- this . _dbPath = await this . _downloadAndCacheDb ( this . _latestRelease ) ;
116- this . _fixturesPath = await this . _downloadFixtures ( this . _latestRelease ) ;
117- this . _docsService = new DocumentManagementService ( this . _dbPath , this . _fixturesPath ) ;
118- await this . _docsService . initialize ( ) ;
116+ if ( this . _initialized ) return ;
117+ if ( this . _initPromise ) return this . _initPromise ;
118+
119+ this . _initPromise = ( async ( ) => {
120+ this . _latestRelease = await this . _getLatestRelease ( ) ;
121+ this . _dbPath = await this . _downloadAndCacheDb ( this . _latestRelease ) ;
122+ this . _fixturesPath = await this . _downloadFixtures ( this . _latestRelease ) ;
123+ this . _docsService = new DocumentManagementService ( this . _dbPath , this . _fixturesPath ) ;
124+ await this . _docsService . initialize ( ) ;
125+ this . _initialized = true ;
126+ } ) ( ) . catch ( ( error ) => {
127+ this . _initPromise = null ;
128+ throw error ;
129+ } ) ;
130+
131+ return this . _initPromise ;
119132 }
120133
121134 async shutdown ( ) {
122- await this . _docsService . shutdown ( ) ;
135+ if ( this . _docsService ) {
136+ await this . _docsService . shutdown ( ) ;
137+ }
123138 if ( this . _dbPath ) {
124139 try {
125140 await fs . unlink ( this . _dbPath ) ;
@@ -136,5 +151,7 @@ export class Context {
136151 logger . error ( `Error deleting temporary fixtures ${ this . _fixturesPath } :` , error ) ;
137152 }
138153 }
154+ this . _initialized = false ;
155+ this . _initPromise = null ;
139156 }
140157}
0 commit comments