Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
-
  • Loading branch information
polina-c committed Jun 2, 2023
commit 281e1f5c83af869cf5a2a957638c52ceca5f9875
72 changes: 63 additions & 9 deletions packages/flutter/lib/src/widgets/widget_inspector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -766,9 +766,8 @@ mixin WidgetInspectorService {
/// class needs to manually manage groups of objects that should be kept
/// alive.
final Map<String, Set<InspectorReferenceData>> _groups = <String, Set<InspectorReferenceData>>{};
final Map<String, InspectorReferenceData> _idToReferenceData = <String, InspectorReferenceData>{};
final Map<Object, String> _objectToId = Map<Object, String>.identity();
int _nextId = 0;

final WeakMap<Object, InspectorReferenceData> _objectToReferenceData = WeakMap<Object, InspectorReferenceData>();

/// The pubRootDirectories that are currently configured for the widget inspector.
List<String>? _pubRootDirectories;
Expand Down Expand Up @@ -1269,9 +1268,7 @@ mixin WidgetInspectorService {
@protected
void disposeAllGroups() {
_groups.clear();
_idToReferenceData.clear();
_objectToId.clear();
_nextId = 0;
_objectToReferenceData.clear();
}

/// Reset all InspectorService state.
Expand Down Expand Up @@ -1306,9 +1303,7 @@ mixin WidgetInspectorService {
if (reference.count == 0) {
final Object? value = reference.value;
if (value != null) {
final String? id = _objectToId.remove(value);
assert(id != null);
_idToReferenceData.remove(id);
_objectToReferenceData.remove(value);
}
}
}
Expand Down Expand Up @@ -3739,3 +3734,62 @@ class _WidgetFactory {
// recognize the annotation.
// ignore: library_private_types_in_public_api
const _WidgetFactory widgetFactory = _WidgetFactory();

/// Does not hold keys from garbage collection.
@visibleForTesting
class WeakMap<K, V> {

Expando<Object> _objects = Expando<Object>();

/// Strings, numbers, booleans.
final Map<K, V> _primitives = <K, V>{};

bool _isPrimitive(Object? key) {
return key == null || key is String || key is num || key is bool;
}

/// Returns true if the map contains the given [key].
bool containsKey(K key) {
if (_isPrimitive(key)) {
return _primitives.containsKey(key);
} else {
return _objects[key!] != null;
}
}

/// Returns the value for the given [key] or null if [key] is not in the map
/// or garbage collected.
///
/// Does not support records to act as keys.
V? operator [](K key){
if (_isPrimitive(key)) {
return _primitives[key];
} else {
return _objects[key!] as V?;
}
}

/// Associates the [key] with the given [value].
void operator []=(K key, V value){
if (_isPrimitive(key)) {
_primitives[key] = value;
} else {
_objects[key!] = value;
}
}

/// Removes the value for the given [key] from the map.
void remove(K key) {
if (_isPrimitive(key)) {
_primitives.remove(key);
} else {
_objects[key!] = null;
}
}

/// Removes all pairs from the map.
void clear() {
_objects = Expando<Object>();
_primitives.clear();
}
}
7 changes: 7 additions & 0 deletions packages/flutter/test/widgets/widget_inspector_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,13 @@ void main() {
expect(()=> InspectorReferenceData((1, 2)), throwsA(isA<StateError>()));
});

group('$WeakMap', (){
test('assigned value', () async {
final WeakMap<Object, Object> weakMap = WeakMap<Object, Object>();
weakMap[1] = 2;
});
});

_TestWidgetInspectorService.runTests();
}

Expand Down