forked from firka/flutter
Fixed race condition in PollingDeviceDiscovery. (#145506)
There are two issues in the previous implementation: 1. `_populateDevices` will return the devices from `deviceNotifier` if it had been initialized, assuming that once it's initialized, it has been properly populated. That assumption is not true because calling getters like `onAdded` would initialize `deviceNotifier` without populating it. 2. `deviceNotifier` instance might be replaced in some cases, causing `onAdded` subscribers to lose any future updates. To fix (1), this commit added the `isPopulated` field in `deviceNotifier` as a more accurate flag to determine if we need to populate it. To fix (2), this commit made `deviceNotifier` a final member in `PolingDeviceDiscovery`.
This commit is contained in:
@@ -97,9 +97,9 @@ String getSizeAsMB(int bytesLength) {
|
||||
/// removed, and calculate a diff of changes when a new list of items is
|
||||
/// available.
|
||||
class ItemListNotifier<T> {
|
||||
ItemListNotifier(): _items = <T>{};
|
||||
ItemListNotifier(): _items = <T>{}, _isPopulated = false;
|
||||
|
||||
ItemListNotifier.from(List<T> items) : _items = Set<T>.of(items);
|
||||
ItemListNotifier.from(List<T> items) : _items = Set<T>.of(items), _isPopulated = true;
|
||||
|
||||
Set<T> _items;
|
||||
|
||||
@@ -111,6 +111,11 @@ class ItemListNotifier<T> {
|
||||
|
||||
List<T> get items => _items.toList();
|
||||
|
||||
bool _isPopulated;
|
||||
|
||||
/// Returns whether the list has been populated.
|
||||
bool get isPopulated => _isPopulated;
|
||||
|
||||
void updateWithNewList(List<T> updatedList) {
|
||||
final Set<T> updatedSet = Set<T>.of(updatedList);
|
||||
|
||||
@@ -118,9 +123,10 @@ class ItemListNotifier<T> {
|
||||
final Set<T> removedItems = _items.difference(updatedSet);
|
||||
|
||||
_items = updatedSet;
|
||||
_isPopulated = true;
|
||||
|
||||
addedItems.forEach(_addedController.add);
|
||||
removedItems.forEach(_removedController.add);
|
||||
addedItems.forEach(_addedController.add);
|
||||
}
|
||||
|
||||
void removeItem(T item) {
|
||||
|
||||
Reference in New Issue
Block a user