Skip to content

Commit

Permalink
Merge pull request #239 from Workiva/unload-modules-loaded-after-unlo…
Browse files Browse the repository at this point in the history
…ad-starts

Ensure all child modules get unloaded
  • Loading branch information
rmconsole3-wf authored Mar 1, 2024
2 parents 14fbfdf + c9a452c commit e83c44a
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions lib/src/lifecycle_module.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1124,12 +1124,20 @@ abstract class LifecycleModule extends SimpleModule with Disposable {
_activeSpan = _startTransitionSpan('unload');

_willUnloadController.add(this);
await Future.wait(_childModules.toList().map((child) {
child.parentContext = _activeSpan?.context;
return child.unload().whenComplete(() {
child.parentContext = null;
});
}));

// We're looping here because it's possible for additional child modules to be added to this list while we are
// unloading the current items. While loadChildModule is guarded from adding new modules after unload starts,
// it contains asynchronous elements that allow a module to be added to this list during the unload.
// Note that items get removed from this list by an event handler listening to their didDispose stream.
while (_childModules.isNotEmpty) {
await Future.wait(_childModules.toList().map((child) {
child.parentContext = _activeSpan?.context;
return child.unload().whenComplete(() {
child.parentContext = null;
});
}));
}

try {
await onUnload();
} catch (error, stackTrace) {
Expand Down

0 comments on commit e83c44a

Please sign in to comment.