fluttercommunity / fluttercommunity/redux.dart
[feature ask] getter to ask if the store has been torn down
- Dominant language
- Dart
- Stars
- 519
- Forks
- 57
- PR merge metrics
- No merged PRs in 30d
Description
HI 👋 Redux Team,
I've got a redux store that exists briefly and sometimes it can be disposed of while a long running middleware server call is in flight. If I try to dispatch another action to set store state following that async call, there's an exception thrown because the store's change stream has been closed. To prevent those exceptions caused by calling dispatch on a torn-down store, it would be nice to be able to ask the store if it's still open to change.
Right now I have the following extension for this, but it requires getting tricky with the onChange stream.
```dart
extension StoreSafeAsyncDispatch on Store {
/// if a store is closed it can not dispatch actions or apply changes
Future get isClosed async {
var closed = false;
await onChange.listen((_) {}, onDone: () => closed = true).cancel();
return closed;
}
/// only dispatch if the store is not closed
Future asyncDispatch(dynamic action) async {
if (await isClosed) return;
return dispatch(action);
}
}
```
Thanks for your time and consideration!
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.