@@ -109,8 +109,8 @@ class DartToolDefinition extends ToolDefinition {
109
109
assert (args[0 ] == command.first);
110
110
// Set up flags to create a new snapshot, if needed, and use the first run
111
111
// as the training run.
112
- SnapshotCache .createInstance (_resourceProvider);
113
- var snapshot = SnapshotCache .instance .getSnapshot (command.first);
112
+ var snapshotCache = SnapshotCache .instanceFor (_resourceProvider);
113
+ var snapshot = snapshotCache .getSnapshot (command.first);
114
114
var snapshotFile = snapshot._snapshotFile;
115
115
var snapshotPath =
116
116
_resourceProvider.pathContext.absolute (snapshotFile.path);
@@ -212,11 +212,11 @@ class _Snapshot {
212
212
void _snapshotCompleted () => _snapshotCompleter.complete ();
213
213
}
214
214
215
- /// A singleton that keeps track of cached snapshot files. The [dispose]
215
+ /// A class that keeps track of cached snapshot files. The [dispose]
216
216
/// function must be called before process exit to clean up snapshots in the
217
217
/// cache.
218
218
class SnapshotCache {
219
- static SnapshotCache _instance ;
219
+ static final _instances = < ResourceProvider , SnapshotCache > {} ;
220
220
221
221
final Folder snapshotCache;
222
222
final ResourceProvider _resourceProvider;
@@ -227,10 +227,12 @@ class SnapshotCache {
227
227
: snapshotCache =
228
228
_resourceProvider.createSystemTemp ('dartdoc_snapshot_cache_' );
229
229
230
- static SnapshotCache get instance => _instance;
231
-
232
- static SnapshotCache createInstance (ResourceProvider resourceProvider) =>
233
- _instance ?? = SnapshotCache ._(resourceProvider);
230
+ /// Returns a [SnapshotCache] for a given [ResourceProvider] , creating one
231
+ /// only if one doesn't exist yet for the given [ResourceProvider] .
232
+ factory SnapshotCache .instanceFor (ResourceProvider resourceProvider) {
233
+ return _instances.putIfAbsent (
234
+ resourceProvider, () => SnapshotCache ._(resourceProvider));
235
+ }
234
236
235
237
_Snapshot getSnapshot (String toolPath) {
236
238
if (snapshots.containsKey (toolPath)) {
@@ -243,7 +245,7 @@ class SnapshotCache {
243
245
}
244
246
245
247
void dispose () {
246
- _instance = null ;
248
+ _instances. remove (_resourceProvider) ;
247
249
if (snapshotCache != null && snapshotCache.exists) {
248
250
return snapshotCache.delete ();
249
251
}
0 commit comments