Permalink
Browse files

Refactor listener infrastructure (#35, #59, #63)

* Extract common listener behaviour from progress formatter
* Extract stats journal from progress formatter

Theses changes will ease the implementation of new listeners (e.g. the pretty, JSON and jUnit formatters).
  • Loading branch information...
jbpros committed Jun 17, 2012
1 parent 264d097 commit 837c021241f494c9534c4d62a65bbd0e03a3e1df
View
@@ -1,3 +1,38 @@
-var Listener = {};
+var Listener = function () {
+ var self = {
+ hear: function hear(event, callback) {
+ if (self.hasHandlerForEvent(event)) {
+ var handler = self.getHandlerForEvent(event);
+ handler(event, callback);
+ } else {
+ callback();
+ }
+ },
+
+ hasHandlerForEvent: function hasHandlerForEvent(event) {
+ var handlerName = self.buildHandlerNameForEvent(event);
+ return self[handlerName] != undefined;
+ },
+
+ buildHandlerNameForEvent: function buildHandlerNameForEvent(event) {
+ var handlerName =
+ Listener.EVENT_HANDLER_NAME_PREFIX +
+ event.getName() +
+ Listener.EVENT_HANDLER_NAME_SUFFIX;
+ return handlerName;
+ },
+
+ getHandlerForEvent: function getHandlerForEvent(event) {
+ var eventHandlerName = self.buildHandlerNameForEvent(event);
+ return self[eventHandlerName];
+ }
+ };
+ return self;
+};
+
+Listener.EVENT_HANDLER_NAME_PREFIX = 'handle';
+Listener.EVENT_HANDLER_NAME_SUFFIX = 'Event';
+
Listener.ProgressFormatter = require('./listener/progress_formatter');
+Listener.StatsJournal = require('./listener/stats_journal');
module.exports = Listener;
Oops, something went wrong.

0 comments on commit 837c021

Please sign in to comment.