rrousselGit / riverpod

A reactive caching and data-binding framework. Riverpod makes working with asynchronous code a breeze.
https://riverpod.dev
MIT License
5.82k stars 888 forks source link

Feature Request: Delay Autodispose Provider Disposal with Reset and Upper Limit #3466

Closed timothy closed 1 month ago

timothy commented 1 month ago

Is your feature request related to a problem? Please describe.

Tab Switching: In a tabbed application, autodispose can prematurely dispose of data associated with a hidden tab when the user switches. This leads to unnecessary reloading when the user returns to a prior tab.

Infinite Scrolling: With long lists containing complex elements like carousel widgets/card widgets that have family providers with IDs passed to them, autodispose can cause these elements to reload every time the user scrolls them out of view and then back into view.

Describe the solution you'd like

This feature request proposes an extension to autodispose providers that allow for delayed disposal with the following functionalities:

Delay Disposal with Duration: Introduce an optional way to pass a Duration object to an autoDispose provider. This duration specifies the delay before the provider's value is disposed of after the last widget stops listening.

Reset Delay on New Listeners: When a new widget starts listening to the provider, the disposal timer should be reset, effectively extending the lifetime of the value for the duration specified.

Force Disposal Upper Limit: Provide an optional parameter to specify a maximum lifetime for the provider's value. The provider will be disposed of the next time nothing is listening to it if its life exceeds this upper limit of the Duration.

Describe alternatives you've considered

Manually managing delayed disposal of providers. This approach is less convenient and error-prone. This is less ideal and increases the amount of code/tech overhead.

Benefits:

rrousselGit commented 1 month ago

Generally, re-fetching is preferred, to get the most up-to-date data.

But anyway, Riverpod already gives you all the tools to do this. Cf https://github.com/rrousselGit/riverpod/issues/1664#issuecomment-1264608305

Overall, this is in large parts a duplicate of https://github.com/rrousselGit/riverpod/issues/1664