Skip to content

Commit

Permalink
ref.invalidate now correctly clear all resources associated with th…
Browse files Browse the repository at this point in the history
…e provider if the provider is no-longer used. (#3382)

fixes #2729
  • Loading branch information
rrousselGit committed Mar 1, 2024
1 parent ce970d2 commit e6cc877
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/riverpod/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

- Improved `Provider(dependencies: [...])` documentation.
- Fix out of date `pub.dev` description
- `ref.invalidate` now correctly clear all resources associated
with the provider if the provider is no-longer used.

## 2.5.0 - 2024-02-03

Expand Down
1 change: 1 addition & 0 deletions packages/riverpod/lib/src/framework/element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ abstract class ProviderElementBase<State> implements Ref<State>, Node {

_mustRecomputeState = true;
runOnDispose();
mayNeedDispose();
_container.scheduler.scheduleProviderRefresh(this);

// We don't call this._markDependencyMayHaveChanged here because we voluntarily
Expand Down
17 changes: 17 additions & 0 deletions packages/riverpod/test/framework/provider_container_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@ import '../utils.dart';

void main() {
group('ProviderContainer', () {
group('invalidate', () {
test('can disposes of the element if not used anymore', () async {
final provider = Provider.autoDispose((r) {
r.keepAlive();
return 0;
});
final container = createContainer();

container.read(provider);
container.invalidate(provider);

await container.pump();

expect(container.getAllProviderElements(), isEmpty);
});
});

test('Supports unmounting containers in reverse order', () {
final container = createContainer();

Expand Down
45 changes: 45 additions & 0 deletions packages/riverpod/test/framework/ref_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,51 @@ import '../utils.dart';

void main() {
group('Ref', () {
group('invalidateSelf', () {
test('can disposes of the element if not used anymore', () async {
late Ref<Object?> ref;
final provider = Provider.autoDispose((r) {
ref = r;
r.keepAlive();
return 0;
});
final container = createContainer();

container.read(provider);
ref.invalidateSelf();

await container.pump();

expect(container.getAllProviderElements(), isEmpty);
});
});

group('invalidate', () {
test('can disposes of the element if not used anymore', () async {
late Ref<Object?> ref;
final dep = Provider((r) {
ref = r;
return 0;
});
final provider = Provider.autoDispose((r) {
r.keepAlive();
return 0;
});
final container = createContainer();

container.read(dep);
container.read(provider);
ref.invalidate(provider);

await container.pump();

expect(
container.getAllProviderElements().map((e) => e.origin),
[dep],
);
});
});

test(
'cannot call ref.watch/ref.read/ref.listen/ref.onDispose after a dependency changed',
() {
Expand Down

0 comments on commit e6cc877

Please sign in to comment.