Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: not disposing autoDispose family provider #2481

Merged
merged 6 commits into from
Apr 24, 2023
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
1 change: 1 addition & 0 deletions packages/riverpod/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- Improved error message for missing `dependencies` (thanks to @ValentinVignal)
- Fixed various typos in the documentation (thanks to @ValentinVignal)
- Fix not disposing autoDispose family providers

## 2.3.5 - 2023-04-18

Expand Down
3 changes: 1 addition & 2 deletions packages/riverpod/lib/src/framework/container.dart
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,7 @@ class ProviderContainer implements Node {
// on provider dispose, to avoid memory leak

void removeStateReaderFrom(ProviderContainer container) {
final reader = container._stateReaders[element._origin];
if (reader?.override == provider) {
if (container._stateReaders[element._origin] == reader) {
container._stateReaders.remove(element._origin);
}
container._children.forEach(removeStateReaderFrom);
Expand Down
27 changes: 27 additions & 0 deletions packages/riverpod/test/framework/auto_dispose_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,33 @@ final alwaysAlive = Provider((ref) {
expect(container.getAllProviderElements(), isEmpty);
});

test(
'when cancel autoDispose family override it should be disposed immediately',
rrousselGit marked this conversation as resolved.
Show resolved Hide resolved
() async {
final provider = Provider.autoDispose.family<int, int>((ref, _) => -1);
rrousselGit marked this conversation as resolved.
Show resolved Hide resolved

var constructionCount = 0;
final override = Provider.autoDispose.family<int, int>((ref, _) {
jeiea marked this conversation as resolved.
Show resolved Hide resolved
return ++constructionCount;
});

final overriden = provider.overrideWithProvider(override);
jeiea marked this conversation as resolved.
Show resolved Hide resolved
final root = createContainer();
final container = createContainer(
parent: root,
overrides: [overriden],
);

final instance1 = provider(0);
jeiea marked this conversation as resolved.
Show resolved Hide resolved
var count = container.read(instance1);
expect(count, 1);
await container.pump();
rrousselGit marked this conversation as resolved.
Show resolved Hide resolved

final instance2 = provider(0);
jeiea marked this conversation as resolved.
Show resolved Hide resolved
count = container.read(instance2);
expect(count, 2);
});

test(
'can select auto-dispose providers if the selecting provider is auto-dispose too',
() {
Expand Down