Skip to content

Commit

Permalink
DRY
Browse files Browse the repository at this point in the history
  • Loading branch information
Maximilian Antoni committed Jun 18, 2013
1 parent fb8cecc commit 547e718
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions lib/hub.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,34 +331,36 @@ function create() {
return cachedEntries(matchersCache, type, event, re, eachMatcher);
}

function doneOnListeners(scope, re, args, e, values, callback) {
var result = values ? (scope.allResults ?
values : values[values.length - 1]) : null;
if (scope.stopped) {
done(scope, callback, e, result);
} else {
args = [e, result];
var fns = findListeners('after', scope.event, re);
emit(fns, scope, args, [], function (err, values) {
function createEmitAfter(finder, then) {
return function (scope, re, args, callback) {
var fns = finder('after', scope.event, re);
emit(fns, scope, args, [], function (err) {
if (scope.stopped) {
done(scope, callback, err);
} else {
if (err) {
args[0] = combinedErrors(args[0], err);
}
var fns = findMatchers('after', scope.event, re);
emit(fns, scope, args, [], function (err) {
if (scope.stopped) {
done(scope, callback, err);
} else {
if (err) {
args[0] = combinedErrors(args[0], err);
}
done(scope, callback, args[0], args[1]);
}
});
then(scope, re, args, callback);
}
});
};
}

function doneAfterMatchers(scope, re, args, callback) {
done(scope, callback, args[0], args[1]);
}

var doneAfterListeners = createEmitAfter(findMatchers, doneAfterMatchers);
var emitAfterListeners = createEmitAfter(findListeners, doneAfterListeners);

function doneOnListeners(scope, re, args, e, values, callback) {
var result = values ? (scope.allResults ?
values : values[values.length - 1]) : null;
if (scope.stopped) {
done(scope, callback, e, result);
} else {
emitAfterListeners(scope, re, [e, result], callback);
}
}

Expand Down

0 comments on commit 547e718

Please sign in to comment.