| 171 | * running at the same time. |
| 172 | */ |
| 173 | const GARBAGE_COLLECTOR = new (class GarbageCollector { |
| 174 | |
| 175 | _lastCollected: ?number; |
| 176 | _cacheWasReset: boolean; |
| 177 | |
| 178 | constructor() { |
| 179 | this._cacheWasReset = false; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * We want to avoid preventing tool use if the cleanup fails for some reason, |
| 184 | * but still provide some chance for people to report/fix things. |
| 185 | */ |
| 186 | _collectSyncNoThrow() { |
| 187 | try { |
| 188 | collectCacheIfOldSync(); |
| 189 | } catch (error) { |
| 190 | terminal.log(error.stack); |
| 191 | terminal.log( |
| 192 | 'Error: Cleaning up the cache folder failed. Continuing anyway.', |
| 193 | ); |
| 194 | terminal.log('The cache folder is: %s', getCacheDirPath()); |
| 195 | } |
| 196 | this._lastCollected = Date.now(); |
| 197 | } |
| 198 | |
| 199 | _resetCache(reporter: Reporter) { |
| 200 | rimraf.sync(getCacheDirPath()); |
| 201 | reporter.update({type: 'transform_cache_reset'}); |
| 202 | this._cacheWasReset = true; |
| 203 | this._lastCollected = Date.now(); |
| 204 | } |
| 205 | |
| 206 | collectIfNecessarySync(options: CacheOptions) { |
| 207 | if (options.resetCache && !this._cacheWasReset) { |
| 208 | this._resetCache(options.reporter); |
| 209 | return; |
| 210 | } |
| 211 | const lastCollected = this._lastCollected; |
| 212 | if ( |
| 213 | lastCollected == null || |
| 214 | Date.now() - lastCollected > GARBAGE_COLLECTION_PERIOD |
| 215 | ) { |
| 216 | this._collectSyncNoThrow(); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | })(); |
| 221 | |
| 222 | /** |
| 223 | * When restarting packager we want to avoid running the collection over again, so we store |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…