Skip to content

Commit c3ba349

Browse files
committed
Unpack buildCategories and add categoryToContainer as getter
1 parent 849b029 commit c3ba349

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

lib/src/model.dart

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4557,17 +4557,15 @@ class Category extends LibraryContainer implements Comparable<Category> {
45574557
class Package extends LibraryContainer implements Comparable<Package>, Privacy {
45584558
String _name;
45594559
PackageGraph _packageGraph;
4560+
4561+
final Map<String, Category> _categoryToContainer = {};
45604562
Package(this._name, this._packageGraph);
45614563

45624564
/// Return true if the code has defined non-default categories for libraries
45634565
/// in this package.
45644566
bool get hasCategories => categories.isNotEmpty;
45654567

4566-
LibraryContainer _defaultCategory;
4567-
LibraryContainer get defaultCategory {
4568-
if (_defaultCategory == null) _buildCategories();
4569-
return _defaultCategory;
4570-
}
4568+
LibraryContainer get defaultCategory => categoryToContainer[null];
45714569

45724570
bool _isPublic;
45734571
@override
@@ -4587,26 +4585,28 @@ class Package extends LibraryContainer implements Comparable<Package>, Privacy {
45874585
@override
45884586
List<Library> get publicLibraries => super.publicLibraries;
45894587

4590-
void _buildCategories() {
4591-
Map<String, Category> categoryToContainer = {
4592-
null: new Category(null, this, dartdocOptions)
4593-
};
4594-
for (Library lib in libraries) {
4595-
String category = lib.category;
4596-
categoryToContainer.putIfAbsent(
4597-
category, () => new Category(category, this, dartdocOptions));
4598-
categoryToContainer[category]._libraries.add(lib);
4588+
// A map of category name to the category itself.
4589+
Map<String, Category> get categoryToContainer {
4590+
if (_categoryToContainer.isEmpty) {
4591+
_categoryToContainer[null] = new Category(null, this, dartdocOptions);
4592+
for (Library lib in libraries) {
4593+
String category = lib.category;
4594+
_categoryToContainer.putIfAbsent(
4595+
category, () => new Category(category, this, dartdocOptions));
4596+
_categoryToContainer[category]._libraries.add(lib);
4597+
}
45994598
}
4600-
_categories = categoryToContainer.values
4601-
.where((c) => c.name != null)
4602-
.toList()
4603-
..sort();
4604-
_defaultCategory = categoryToContainer[null];
4599+
return _categoryToContainer;
46054600
}
46064601

46074602
List<LibraryContainer> _categories;
46084603
List<LibraryContainer> get categories {
4609-
if (_categories == null) _buildCategories();
4604+
if (_categories == null) {
4605+
_categories = categoryToContainer.values
4606+
.where((c) => c.name != null)
4607+
.toList()
4608+
..sort();
4609+
}
46104610
return _categories;
46114611
}
46124612

0 commit comments

Comments
 (0)