Skip to content

Commit

Permalink
Merge pull request rrousselGit#190 from rrousselGit/fix-dispose
Browse files Browse the repository at this point in the history
  • Loading branch information
rrousselGit authored Oct 14, 2020
2 parents f438d8e + db82cf2 commit d81a46c
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 9 deletions.
5 changes: 5 additions & 0 deletions packages/flutter_riverpod/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 0.11.2

- Fixed a bug where providers (usually ScopedProviders) did not dispose correctly
(see also https://github.com/rrousselGit/river_pod/issues/154).

# 0.11.1

- Fixed a bug where hot-reload did not work for `ConsumerWidget`/`Consumer`
Expand Down
5 changes: 5 additions & 0 deletions packages/hooks_riverpod/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 0.11.2

- Fixed a bug where providers (usually ScopedProviders) did not dispose correctly
(see also https://github.com/rrousselGit/river_pod/issues/154).

# 0.11.1

- Fixed a bug where hot-reload did not work for `ConsumerWidget`/`Consumer`
Expand Down
5 changes: 5 additions & 0 deletions packages/riverpod/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 0.11.2

- Fixed a bug where providers (usually ScopedProviders) did not dispose correctly
(see also https://github.com/rrousselGit/river_pod/issues/154).

# 0.11.0

- `package:riverpod/riverpod.dart` now exports `StateNotifier`
Expand Down
22 changes: 13 additions & 9 deletions packages/riverpod/lib/src/framework/container.dart
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,10 @@ class ProviderContainer {
final queue = DoubleLinkedQueue<ProviderElement>();

for (final element in _stateReaders.values) {
if (element.dependents == null || element._subscriptions.isEmpty) {
if (element._subscriptions == null ||
element._subscriptions.keys
.where((e) => e._container == this)
.isEmpty) {
queue.add(element);
}
}
Expand All @@ -376,14 +379,15 @@ class ProviderContainer {

yield element;

if (element._dependents == null) {
continue;
}
for (final dependent in element._dependents) {
if (dependent._container == this &&
// All the parents of a node must have been visited before a node is visited
dependent._subscriptions.keys.every(visitedNodes.contains)) {
queue.add(dependent);
if (element._dependents != null) {
for (final dependent in element._dependents) {
if (dependent._container == this &&
// All the parents of a node must have been visited before a node is visited
dependent._subscriptions.keys.every((e) {
return e._container != this || visitedNodes.contains(e);
})) {
queue.add(dependent);
}
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions packages/riverpod/test/framework2/scoped_provider_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ void main() {
await Future<void>.value();

expect(element.mounted, false);

container.dispose();

expect(element.mounted, false);
});

test('overridesAs are auto disposed', () async {
Expand Down
45 changes: 45 additions & 0 deletions packages/riverpod/test/visit_states_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,51 @@ void main() {
}
});

// A#1
// |
// B#2
test('linear across two containers', () {
final a = Provider<A>((ref) => A());

final b = ScopedProvider<B>((watch) {
watch(a);
return B();
});

final parent = ProviderContainer();
final container = ProviderContainer(parent: parent);
container.read(b);

expect(compute(container), [b]);
});

// A#1 B#2
// \ /
// C#2
test('branching across two containers', () {
final a = Provider<A>((ref) => A());

final b = ScopedProvider<B>((watch) {
return B();
});

final c = ScopedProvider<C>((watch) {
watch(a);
watch(b);
return C();
});

final parent = ProviderContainer();

final perm = Permutations(2, [b, c]);
for (final permutation in perm()) {
final container = ProviderContainer(parent: parent);
permutation.forEach(container.read);

expect(compute(container), [b, c]);
}
});

/// A
/// / \
/// B __ |
Expand Down

0 comments on commit d81a46c

Please sign in to comment.