Skip to content

Commit

Permalink
Merge pull request #90 from Workiva/rap-2055-logger
Browse files Browse the repository at this point in the history
RAP-2055 Log LifecycleEvents
  • Loading branch information
jayudey-wf authored Jul 11, 2017
2 parents ad73e0f + fa99f60 commit 0a5e183
Show file tree
Hide file tree
Showing 4 changed files with 272 additions and 47 deletions.
45 changes: 36 additions & 9 deletions lib/src/lifecycle_module.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ abstract class LifecycleModule extends SimpleModule
_didUnloadChildModuleSubscriptions = {};
StreamController<LifecycleModule> _didUnloadController;
final Disposable _disposableProxy = new Disposable();
final Disposable _postUnloadDisposable = new Disposable();
Logger _logger;
String _name = 'Module';
String _name;
LifecycleState _previousState;
LifecycleState _state = LifecycleState.instantiated;
Completer<Null> _transition;
Expand All @@ -79,10 +80,7 @@ abstract class LifecycleModule extends SimpleModule

// constructor necessary to init load / unload state stream
LifecycleModule() {
// The didUnload event must be emitted after disposal which requires that
// the stream controller must be disposed of manually at the end of the
// unload transition.
_didUnloadController = new StreamController<LifecycleModule>.broadcast();
_logger = new Logger('$name');

[
_willLoadController = new StreamController<LifecycleModule>.broadcast(),
Expand All @@ -103,12 +101,33 @@ abstract class LifecycleModule extends SimpleModule
_didResumeController = new StreamController<LifecycleModule>.broadcast()
].forEach(manageStreamController);

_logger = new Logger('w_module');
// The didUnload event must be emitted after disposal which requires that
// the stream controller must be disposed of manually at the end of the
// unload transition.
_didUnloadController = new StreamController<LifecycleModule>.broadcast();
_postUnloadDisposable.manageStreamController(_didUnloadController);

<
String,
Stream>{
'didLoad': didLoad,
'didLoadChildModule': didLoadChildModule,
'didResume': didResume,
'didSuspend': didSuspend,
'didUnload': didUnload,
'didUnloadChildModule': didUnloadChildModule,
'willLoad': willLoad,
'willLoadChildModule': willLoadChildModule,
'willResume': willResume,
'willSuspend': willSuspend,
'willUnload': willUnload,
'willUnloadChildModule': willUnloadChildModule,
}.forEach(_logLifecycleEvents);
}

/// Name of the module for identification in exceptions and debug messages.
// ignore: unnecessary_getters_setters
String get name => _name;
String get name => _name ?? 'LifecycleModule($runtimeType)';

/// Deprecated: the module name should be defined by overriding the getter in
/// a subclass and it should not be mutable.
Expand Down Expand Up @@ -661,6 +680,14 @@ abstract class LifecycleModule extends SimpleModule
}
}

/// A utility to logging LifecycleModule lifecycle events
void _logLifecycleEvents(
String logLabel, Stream<dynamic> lifecycleEventStream) {
_postUnloadDisposable.manageStreamSubscription(lifecycleEventStream.listen(
(_) => _logger.fine(logLabel),
onError: (error) => _logger.warning('$logLabel error: $error')));
}

/// Handles a child [LifecycleModule]'s [didUnload] event.
Future<Null> _onChildModuleDidUnload(LifecycleModule module) async {
try {
Expand Down Expand Up @@ -769,12 +796,12 @@ abstract class LifecycleModule extends SimpleModule
_transition = null;
}
_didUnloadController.add(this);
await _didUnloadController.close();
await _postUnloadDisposable.dispose();
} on ModuleUnloadCanceledException catch (error, _) {
rethrow;
} catch (error, stackTrace) {
_didUnloadController.addError(error, stackTrace);
await _didUnloadController.close();
await _postUnloadDisposable.dispose();
rethrow;
}
}
Expand Down
Loading

0 comments on commit 0a5e183

Please sign in to comment.