diff --git a/lib/src/lifecycle_module.dart b/lib/src/lifecycle_module.dart index 53a95bbf..e9f34596 100644 --- a/lib/src/lifecycle_module.dart +++ b/lib/src/lifecycle_module.dart @@ -51,14 +51,21 @@ enum LifecycleState { unloaded } +/// Contains "original" module API for backwards compatibility. +abstract class LifecycleModule extends _LifecycleModule + with SimpleModule {} + +/// Only contains lifecycle-related methods and properties. +abstract class LifecycleModuleV2 extends _LifecycleModule {} + /// Intended to be extended by most base module classes in order to provide a /// unified lifecycle API. -abstract class LifecycleModule extends SimpleModule with Disposable { +abstract class _LifecycleModule> with Disposable { static int _nextId = 0; // Used by tracing to tell apart multiple instances of the same module int _instanceId = _nextId++; - List _childModules = []; + List _childModules = []; Logger _logger; String _defaultName; LifecycleState _previousState; @@ -71,38 +78,30 @@ abstract class LifecycleModule extends SimpleModule with Disposable { DateTime _startLoadTime; // Lifecycle event StreamControllers - StreamController _willLoadChildModuleController = - StreamController.broadcast(); - StreamController _didLoadChildModuleController = - StreamController.broadcast(); - - StreamController _willLoadController = - StreamController.broadcast(); - StreamController _didLoadController = - StreamController.broadcast(); - - StreamController _willSuspendController = - StreamController.broadcast(); - StreamController _didSuspendController = - StreamController.broadcast(); - - StreamController _willResumeController = - StreamController.broadcast(); - StreamController _didResumeController = - StreamController.broadcast(); - - StreamController _willUnloadChildModuleController = - StreamController.broadcast(); - StreamController _didUnloadChildModuleController = - StreamController.broadcast(); - - StreamController _willUnloadController = - StreamController.broadcast(); - StreamController _didUnloadController = - StreamController.broadcast(); + StreamController _willLoadChildModuleController = + StreamController.broadcast(); + StreamController _didLoadChildModuleController = + StreamController.broadcast(); + + StreamController _willLoadController = StreamController.broadcast(); + StreamController _didLoadController = StreamController.broadcast(); + + StreamController _willSuspendController = StreamController.broadcast(); + StreamController _didSuspendController = StreamController.broadcast(); + + StreamController _willResumeController = StreamController.broadcast(); + StreamController _didResumeController = StreamController.broadcast(); + + StreamController _willUnloadChildModuleController = + StreamController.broadcast(); + StreamController _didUnloadChildModuleController = + StreamController.broadcast(); + + StreamController _willUnloadController = StreamController.broadcast(); + StreamController _didUnloadController = StreamController.broadcast(); // constructor necessary to init load / unload state stream - LifecycleModule() { + _LifecycleModule() { _logger = Logger('w_module.LifecycleModule:$name'); [ @@ -264,13 +263,13 @@ abstract class LifecycleModule extends SimpleModule with Disposable { } /// List of child components so that lifecycle can iterate over them as needed - Iterable get childModules => _childModules.toList(); + Iterable get childModules => _childModules.toList(); /// The [LifecycleModule] was loaded. /// /// Any error or exception thrown during the [LifecycleModule]'s /// [onLoad] call will be emitted. - Stream get didLoad => _didLoadController.stream; + Stream get didLoad => _didLoadController.stream; /// A child [LifecycleModule] was loaded. /// @@ -279,8 +278,7 @@ abstract class LifecycleModule extends SimpleModule with Disposable { /// /// Any error or exception thrown during the parent [LifecycleModule]'s /// [onDidLoadChildModule] call will be emitted. - Stream get didLoadChildModule => - _didLoadChildModuleController.stream; + Stream get didLoadChildModule => _didLoadChildModuleController.stream; /// The [LifecycleModule] was resumed. /// @@ -289,7 +287,7 @@ abstract class LifecycleModule extends SimpleModule with Disposable { /// /// Any error or exception thrown during the [LifecycleModule]'s /// [onResume] call will be emitted. - Stream get didResume => _didResumeController.stream; + Stream get didResume => _didResumeController.stream; /// The [LifecycleModule] was suspended. /// @@ -298,7 +296,7 @@ abstract class LifecycleModule extends SimpleModule with Disposable { /// /// Any error or exception thrown during the [LifecycleModule]'s /// [onSuspend] call will be emitted. - Stream get didSuspend => _didSuspendController.stream; + Stream get didSuspend => _didSuspendController.stream; /// The [LifecycleModule] was unloaded. /// @@ -307,7 +305,7 @@ abstract class LifecycleModule extends SimpleModule with Disposable { /// /// Any error or exception thrown during the [LifecycleModule]'s /// [onUnload] call will be emitted. - Stream get didUnload => _didUnloadController.stream; + Stream get didUnload => _didUnloadController.stream; /// A child [LifecycleModule] was unloaded. /// @@ -316,34 +314,32 @@ abstract class LifecycleModule extends SimpleModule with Disposable { /// /// Any error or exception thrown during the parent [LifecycleModule]'s /// [onDidUnloadChildModule] call will be emitted. - Stream get didUnloadChildModule => - _didUnloadChildModuleController.stream; + Stream get didUnloadChildModule => _didUnloadChildModuleController.stream; /// A child [LifecycleModule] is about to be loaded. /// /// Any error or exception thrown during the parent [LifecycleModule]'s /// [onDidLoadChildModule] call will be emitted. - Stream get willLoadChildModule => - _willLoadChildModuleController.stream; + Stream get willLoadChildModule => _willLoadChildModuleController.stream; /// A child [LifecycleModule] is about to be unloaded. /// /// Any error or exception thrown during the parent [LifecycleModule]'s /// [onDidUnloadChildModule] call will be emitted. - Stream get willUnloadChildModule => + Stream get willUnloadChildModule => _willUnloadChildModuleController.stream; /// The [LifecycleModule] is about to be resumed. - Stream get willResume => _willResumeController.stream; + Stream get willResume => _willResumeController.stream; /// The [LifecycleModule] is about to be unloaded. - Stream get willUnload => _willUnloadController.stream; + Stream get willUnload => _willUnloadController.stream; /// The [LifecycleModule] is about to be loaded. - Stream get willLoad => _willLoadController.stream; + Stream get willLoad => _willLoadController.stream; /// The [LifecycleModule] is about to be suspended. - Stream get willSuspend => _willSuspendController.stream; + Stream get willSuspend => _willSuspendController.stream; /// Whether the module is currently instantiated. bool get isInstantiated => _state == LifecycleState.instantiated; @@ -487,7 +483,7 @@ abstract class LifecycleModule extends SimpleModule with Disposable { /// Attempting to load a child module after a module has been unloaded will /// throw a [StateError]. @protected - Future loadChildModule(LifecycleModule childModule) { + Future loadChildModule(T childModule) { if (isOrWillBeDisposed) { return _buildDisposedOrDisposingResponse(methodName: 'loadChildModule'); } @@ -504,7 +500,7 @@ abstract class LifecycleModule extends SimpleModule with Disposable { } final completer = Completer(); - onWillLoadChildModule(childModule).then((LifecycleModule _) async { + onWillLoadChildModule(childModule).then((T _) async { _willLoadChildModuleController.add(childModule); final childModuleWillUnloadSub = listenToStream( @@ -819,19 +815,19 @@ abstract class LifecycleModule extends SimpleModule with Disposable { /// Custom logic to be executed when a child module is to be loaded. @protected - Future onWillLoadChildModule(LifecycleModule module) async {} + Future onWillLoadChildModule(T module) async {} /// Custom logic to be executed when a child module has been loaded. @protected - Future onDidLoadChildModule(LifecycleModule module) async {} + Future onDidLoadChildModule(T module) async {} /// Custom logic to be executed when a child module is to be unloaded. @protected - Future onWillUnloadChildModule(LifecycleModule module) async {} + Future onWillUnloadChildModule(T module) async {} /// Custom logic to be executed when a child module has been unloaded. @protected - Future onDidUnloadChildModule(LifecycleModule module) async {} + Future onDidUnloadChildModule(T module) async {} /// Custom logic to be executed during suspend. /// @@ -981,7 +977,7 @@ abstract class LifecycleModule extends SimpleModule with Disposable { } /// Handles a child [LifecycleModule]'s [didUnload] event. - Future _onChildModuleDidUnload(LifecycleModule module) async { + Future _onChildModuleDidUnload(T module) async { try { try { await onDidUnloadChildModule(module); @@ -1000,7 +996,7 @@ abstract class LifecycleModule extends SimpleModule with Disposable { } /// Handles a child [LifecycleModule]'s [willUnload] event. - Future _onChildModuleWillUnload(LifecycleModule module) async { + Future _onChildModuleWillUnload(T module) async { try { try { await onWillUnloadChildModule(module);