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

Dispose modules during onLoad if parent is unloaded/unloading #243

Merged
merged 3 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## [3.0.6](https://github.com/Workiva/w_module/compare/3.0.5...3.0.6)

_March 4, 2024_

- **Bug Fix:** A child module which loads during the unload of the parent
may cause BadState exceptions.

## [3.0.0](https://github.com/Workiva/w_module/compare/2.0.5...3.0.0)

_August 18, 2023_
Expand Down
8 changes: 8 additions & 0 deletions lib/src/lifecycle_module.dart
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,14 @@ abstract class LifecycleModule extends SimpleModule with Disposable {

final completer = Completer<Null>();
onWillLoadChildModule(childModule).then((_) async {
// It is possible to reach this point due to the asynchrony of onWillLoadChildModule.
// In that case, simply do not load the child module and instead dispose it.
if (isUnloaded || isUnloading) {
await childModule.dispose();
completer.complete();
return;
}

_willLoadChildModuleController.add(childModule);

final childModuleWillUnloadSub = listenToStream(
Expand Down
23 changes: 23 additions & 0 deletions test/lifecycle_module_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1756,6 +1756,29 @@ void runTests(bool runSpanTests) {
childModule.eventList, equals(['willLoad', 'onLoad', 'didLoad']));
});

test('followed by parent unload causes child to dispose and never load',
() async {
parentModule.eventList?.clear();
final load = parentModule.loadChildModule(childModule);

await parentModule.unload();
await load;

expect(
parentModule.eventList,
equals([
'onWillLoadChildModule',
'onShouldUnload',
'willUnload',
'onUnload',
'didUnload',
'onDispose'
]),
);

expect(childModule.eventList, equals(['onDispose']));
});

test('should emit lifecycle log events', () async {
expect(
Logger.root.onRecord,
Expand Down
Loading