Unverified Commit 8735ab1e authored by Dan Field's avatar Dan Field Committed by GitHub

Make onImage re-entrant safe (#50316)

parent 7b074468
...@@ -88,11 +88,15 @@ Future<void> precacheImage( ...@@ -88,11 +88,15 @@ Future<void> precacheImage(
ImageStreamListener listener; ImageStreamListener listener;
listener = ImageStreamListener( listener = ImageStreamListener(
(ImageInfo image, bool sync) { (ImageInfo image, bool sync) {
completer.complete(); if (!completer.isCompleted) {
completer.complete();
}
stream.removeListener(listener); stream.removeListener(listener);
}, },
onError: (dynamic exception, StackTrace stackTrace) { onError: (dynamic exception, StackTrace stackTrace) {
completer.complete(); if (!completer.isCompleted) {
completer.complete();
}
stream.removeListener(listener); stream.removeListener(listener);
if (onError != null) { if (onError != null) {
onError(exception, stackTrace); onError(exception, stackTrace);
......
...@@ -639,13 +639,17 @@ void main() { ...@@ -639,13 +639,17 @@ void main() {
) )
); );
expect(imageStreamCompleter.listeners.length, 2); // Two listeners - one is the listener added by precacheImage, the other by the ImageCache.
imageStreamCompleter.listeners.toList()[1].onImage(null, null); final List<ImageStreamListener> listeners = imageStreamCompleter.listeners.toList();
expect(listeners.length, 2);
expect(imageStreamCompleter.listeners.length, 1); // Make sure the first listener can be called re-entrantly
imageStreamCompleter.listeners.toList()[0].onImage(null, null); listeners[1].onImage(null, null);
listeners[1].onImage(null, null);
expect(imageStreamCompleter.listeners.length, 0); // Make sure the second listener can be called re-entrantly.
listeners[0].onImage(null, null);
listeners[0].onImage(null, null);
}); });
testWidgets('Precache completes with onError on error', (WidgetTester tester) async { testWidgets('Precache completes with onError on error', (WidgetTester tester) async {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment