Skip to content

Commit

Permalink
Merge pull request #243 from Workiva/unload-modules-before-dispose
Browse files Browse the repository at this point in the history
Dispose modules during onLoad if parent is unloaded/unloading
  • Loading branch information
rmconsole7-wk authored Mar 4, 2024
2 parents 00d6f60 + c012217 commit f9d8858
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
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

0 comments on commit f9d8858

Please sign in to comment.