Skip to content

Commit 2edf6d9

Browse files
authored
Simplify _idsToLoad handling. (#3967)
1 parent 3494d50 commit 2edf6d9

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

build/lib/src/library_cycle_graph/library_cycle_graph_loader.dart

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,15 @@ class LibraryCycleGraphLoader {
109109
}
110110

111111
/// Whether there are assets to load before or at [upToPhase].
112-
bool _hasIdToLoad({required int upToPhase}) =>
113-
_idsToLoad.keys.where((key) => key <= upToPhase).isNotEmpty;
112+
bool _hasIdToLoad({required int upToPhase}) {
113+
final first = _idsToLoad.keys.firstOrNull;
114+
if (first == null) return false;
115+
if (first > upToPhase) {
116+
// Entries are sorted, so all further entries are also too high phase.
117+
return false;
118+
}
119+
return true;
120+
}
114121

115122
/// The phase and ID of the next asset to load before or at [upToPhase].
116123
///
@@ -120,11 +127,16 @@ class LibraryCycleGraphLoader {
120127
///
121128
/// When done loading call [_removeIdToLoad] with the phase and ID.
122129
(int, AssetId) _nextIdToLoad({required int upToPhase}) {
123-
final entry =
124-
_idsToLoad.entries.where((entry) => entry.key <= upToPhase).first;
125-
final result = entry.value.last;
126-
_loadingIds.add((entry.key, result));
127-
return (entry.key, result);
130+
final first = _idsToLoad.entries.first;
131+
if (first.key > upToPhase) {
132+
// Entries are sorted, so all further entries are also too high phase.
133+
throw StateError('No such element.');
134+
}
135+
// Return the last ID from the list of IDs at this phase because it's
136+
// cheapest to remove in `_removeIdToLoad`.
137+
final result = first.value.last;
138+
_loadingIds.add((first.key, result));
139+
return (first.key, result);
128140
}
129141

130142
/// Removes from [_idsToLoad].

0 commit comments

Comments
 (0)