Skip to content

Commit 6618b6a

Browse files
committed
fix: fixed a rare case where re-activated Element could lose its Ref providers
1 parent 82b2bb2 commit 6618b6a

File tree

1 file changed

+1
-15
lines changed

1 file changed

+1
-15
lines changed

packages/context_ref/lib/src/context_ref_root.dart

+1-15
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,6 @@ class InheritedContextRefElement extends InheritedElement {
4444
refs.add(ref);
4545
}
4646

47-
/// This is used to keep track of the dependent [Element]s that have been
48-
/// deactivated in the current frame. We need to keep track of them to
49-
/// not dispose of the [ValueProvider]s too early, while the [Element] can
50-
/// still be reactivated in the same frame.
51-
final _deactivatedElements = HashSet<Element>.identity();
52-
5347
ValueProvider<T> bind<T>({
5448
required BuildContext context,
5549
required Ref<T> ref,
@@ -59,11 +53,6 @@ class InheritedContextRefElement extends InheritedElement {
5953
}) {
6054
assert(context is Element);
6155

62-
// If bind is called, it means the element is active. If it was
63-
// previously deactivated - remove it from the list of deactivated
64-
// elements. This is important to handle the element re-parenting.
65-
_deactivatedElements.remove(context);
66-
6756
// Make [context] dependent on this element so that we can get notified
6857
// when the [context] is removed from the tree.
6958
context.dependOnInheritedElement(this);
@@ -139,11 +128,9 @@ class InheritedContextRefElement extends InheritedElement {
139128
// yet unmounted. The element can be reactivated during the same fame.
140129
// So, let's not dispose the context data immediately, but rather wait
141130
// until the end of the frame to see if the element is reactivated.
142-
_deactivatedElements.add(dependent);
143131
SchedulerBinding.instance.addPostFrameCallback((_) {
144-
if (_deactivatedElements.contains(dependent)) {
132+
if (!dependent.mounted) {
145133
_disposeContextData(dependent);
146-
_deactivatedElements.remove(dependent);
147134
}
148135
});
149136
super.removeDependent(dependent);
@@ -163,7 +150,6 @@ class InheritedContextRefElement extends InheritedElement {
163150

164151
@override
165152
void unmount() {
166-
_deactivatedElements.clear();
167153
for (final element in _refs.keys.toList(growable: false)) {
168154
_disposeContextData(element);
169155
}

0 commit comments

Comments
 (0)