Skip to content

Commit 16c7e6d

Browse files
committed
[flutter_svg] Clarify why pending loads track request identity
1 parent ac2722b commit 16c7e6d

2 files changed

Lines changed: 10 additions & 31 deletions

File tree

‎third_party/packages/flutter_svg/lib/src/cache.dart‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import 'package:flutter/foundation.dart';
22
import 'package:vector_graphics_compiler/vector_graphics_compiler.dart';
33

4-
/// An in-flight load and its identity, independent of the returned Future.
5-
///
6-
/// Distinct requests can return the same Future after an eviction. The identity
7-
/// prevents an earlier request from populating the cache for a later one.
4+
// Eviction can be followed by another request that reuses the same Future.
5+
// Track request identity separately so obsolete callbacks are ignored.
86
class _PendingLoad {
97
_PendingLoad(this.future);
108

‎third_party/packages/flutter_svg/test/cache_test.dart‎

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -170,29 +170,6 @@ void main() {
170170
expect(uncaughtErrors, isEmpty);
171171
});
172172

173-
test('failed loads are removed from the pending cache and can be retried', () async {
174-
final cache = Cache();
175-
final firstLoad = Completer<ByteData>();
176-
final Future<ByteData> pending = cache.putIfAbsent('icon', () => firstLoad.future);
177-
178-
expect(
179-
identical(pending, cache.putIfAbsent('icon', () => throw StateError('duplicate load'))),
180-
isTrue,
181-
);
182-
183-
final Future<void> expectedFailure = expectLater(pending, throwsStateError);
184-
firstLoad.completeError(StateError('temporarily unavailable'));
185-
await expectedFailure;
186-
187-
final recovered = ByteData(2);
188-
final ByteData result = await cache.putIfAbsent(
189-
'icon',
190-
() => Future<ByteData>.value(recovered),
191-
);
192-
expect(identical(result, recovered), isTrue);
193-
expect(cache.count, 1);
194-
});
195-
196173
test('clear prevents pending loads from repopulating stale entries', () async {
197174
final cache = Cache();
198175
final oldLoad = Completer<ByteData>();
@@ -238,16 +215,20 @@ void main() {
238215
expect(cache.count, 0);
239216
});
240217

241-
test('eviction remains safe if a new load returns the same Future', () async {
218+
test('an evicted request cannot cache a Future reused by a later request', () async {
242219
final cache = Cache();
243220
final shared = Completer<ByteData>();
244221
final Future<ByteData> first = cache.putIfAbsent('icon', () => shared.future);
245222
expect(cache.evict('icon'), isTrue);
223+
224+
final Future<int> countBetweenCallbacks = shared.future.then((_) => cache.count);
246225
final Future<ByteData> second = cache.putIfAbsent('icon', () => shared.future);
247226

248-
shared.complete(ByteData(1));
249-
await first;
250-
await second;
227+
final data = ByteData(1);
228+
shared.complete(data);
229+
expect(await first, same(data));
230+
expect(await countBetweenCallbacks, 0);
231+
expect(await second, same(data));
251232
expect(cache.count, 1);
252233
});
253234

0 commit comments

Comments
 (0)