@@ -17,17 +17,40 @@ class LibraryHelper extends Domain {
17
17
final Logger _logger = Logger ('LibraryHelper' );
18
18
19
19
/// Map of library ID to [Library] .
20
- final _librariesById = < String , Library > {} ;
20
+ late final Map <String , Library > _librariesById ;
21
21
22
22
/// Map of libraryRef ID to [LibraryRef] .
23
- final _libraryRefsById = < String , LibraryRef > {} ;
23
+ late final Map <String , LibraryRef > _libraryRefsById ;
24
24
25
25
LibraryRef ? _rootLib;
26
26
27
27
LibraryHelper (AppInspectorInterface appInspector) {
28
28
inspector = appInspector;
29
29
}
30
30
31
+ /// Initialize any caches.
32
+ ///
33
+ /// If [modifiedModuleReport] is not null, invalidates only modified libraries
34
+ /// from the cache and recomputes values for any eager caches.
35
+ void initialize ([ModifiedModuleReport ? modifiedModuleReport]) {
36
+ _rootLib = null ;
37
+ if (modifiedModuleReport != null ) {
38
+ for (final library in modifiedModuleReport.modifiedLibraries) {
39
+ // These will later be initialized by `libraryFor` if needed.
40
+ _librariesById.remove (library);
41
+ _libraryRefsById.remove (library);
42
+ }
43
+ for (final library in modifiedModuleReport.reloadedLibraries) {
44
+ // These need to be recomputed here as `libraryRefs` only checks if this
45
+ // map is empty before returning.
46
+ _libraryRefsById[library] = _createLibraryRef (library);
47
+ }
48
+ } else {
49
+ _librariesById = < String , Library > {};
50
+ _libraryRefsById = < String , LibraryRef > {};
51
+ }
52
+ }
53
+
31
54
Future <LibraryRef > get rootLib async {
32
55
if (_rootLib != null ) return _rootLib! ;
33
56
final libraries = await libraryRefs;
@@ -52,26 +75,13 @@ class LibraryHelper extends Domain {
52
75
return _rootLib! ;
53
76
}
54
77
55
- /// Removes any modified libraries from the cache and either eagerly or lazily
56
- /// computes values for the reloaded libraries in the [modifiedModuleReport] .
57
- void invalidate (ModifiedModuleReport modifiedModuleReport) {
58
- for (final library in modifiedModuleReport.modifiedLibraries) {
59
- // These will later be initialized by `libraryFor` if needed.
60
- _librariesById.remove (library);
61
- _libraryRefsById.remove (library);
62
- }
63
- for (final library in modifiedModuleReport.reloadedLibraries) {
64
- _libraryRefsById[library] = _createLibraryRef (library);
65
- }
66
- }
67
-
68
78
LibraryRef _createLibraryRef (String library) =>
69
79
LibraryRef (id: library, name: library, uri: library);
70
80
71
81
/// Returns all libraryRefs in the app.
72
82
///
73
83
/// Note this can return a cached result that can be selectively reinitialized
74
- /// using [invalidate ] .
84
+ /// using [initialize ] .
75
85
Future <List <LibraryRef >> get libraryRefs async {
76
86
if (_libraryRefsById.isNotEmpty) return _libraryRefsById.values.toList ();
77
87
final libraries =
0 commit comments