Skip to content

Commit 812d07e

Browse files
committed
Add intiializer to LibraryHelper
1 parent 062fbe4 commit 812d07e

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

dwds/lib/src/debugging/inspector.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ class AppInspector implements AppInspectorInterface {
118118

119119
if (modifiedModuleReport != null) {
120120
// Invalidate `_libraryHelper` as we use it populate any script caches.
121-
_libraryHelper.invalidate(modifiedModuleReport);
121+
_libraryHelper.initialize(modifiedModuleReport);
122122
} else {
123-
_libraryHelper = LibraryHelper(this);
123+
_libraryHelper = LibraryHelper(this)..initialize();
124124
_scriptRefsById.clear();
125125
_serverPathToScriptRef.clear();
126126
_scriptIdToLibraryId.clear();

dwds/lib/src/debugging/libraries.dart

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,40 @@ class LibraryHelper extends Domain {
1717
final Logger _logger = Logger('LibraryHelper');
1818

1919
/// Map of library ID to [Library].
20-
final _librariesById = <String, Library>{};
20+
late final Map<String, Library> _librariesById;
2121

2222
/// Map of libraryRef ID to [LibraryRef].
23-
final _libraryRefsById = <String, LibraryRef>{};
23+
late final Map<String, LibraryRef> _libraryRefsById;
2424

2525
LibraryRef? _rootLib;
2626

2727
LibraryHelper(AppInspectorInterface appInspector) {
2828
inspector = appInspector;
2929
}
3030

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+
3154
Future<LibraryRef> get rootLib async {
3255
if (_rootLib != null) return _rootLib!;
3356
final libraries = await libraryRefs;
@@ -52,26 +75,13 @@ class LibraryHelper extends Domain {
5275
return _rootLib!;
5376
}
5477

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-
6878
LibraryRef _createLibraryRef(String library) =>
6979
LibraryRef(id: library, name: library, uri: library);
7080

7181
/// Returns all libraryRefs in the app.
7282
///
7383
/// Note this can return a cached result that can be selectively reinitialized
74-
/// using [invalidate].
84+
/// using [initialize].
7585
Future<List<LibraryRef>> get libraryRefs async {
7686
if (_libraryRefsById.isNotEmpty) return _libraryRefsById.values.toList();
7787
final libraries =

0 commit comments

Comments
 (0)