Skip to content

Commit

Permalink
Event piping will not triggered other piped namespaces .-.
Browse files Browse the repository at this point in the history
  • Loading branch information
macsmac committed Jun 4, 2017
1 parent e05a696 commit 5c74679
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions src/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const Namespace = function(options = {}, emitter = {}) {
const type = decodeURIComponent(data[0]);
const args = data[1].split(",").map(decodeURIComponent);

namespace.emit.apply(namespace, [type, ...args]);
namespace._emit(type, args, false, true);

callback();
}
Expand All @@ -61,7 +61,7 @@ const Namespace = function(options = {}, emitter = {}) {
output._read = function() {
namespace.useOnce(function(type, args) {
output.push(encodeURIComponent(type) + ";" + args.map(decodeURIComponent).join(","));
});
}, true);
}

return output;
Expand Down Expand Up @@ -144,30 +144,31 @@ const Namespace = function(options = {}, emitter = {}) {
return namespace;
}

this.emit = function(type) {
this._emit = function(type, args, skipPiped, skipMiddlewares) {
if (namespace.freezed) return namespace;

if (options.stats) {
namespace.listenerStats[type] = ~~namespace.listenerStats[type] + 1;
}

const args = _.slice(arguments);
const data = _.slice(args, 1);

_.each(namespace.connected, e => e && e.emit.apply(e.namespace ? e.namespace() : e, args));
if (!skipPiped) _.each(namespace.connected, e => e && e._emit(type, args, true));

async.eachSeries([
namespace.executeMiddlewares,
namespace.executeMiddlewares.bind(namespace, skipMiddlewares),
namespace.executeListeners
], function(handler, callback) {
handler(type, data, callback);
handler(type, args, callback);
}, function(error) {
namespace.triggerError(type, data, error);
namespace.triggerError(type, args, error);
});

return namespace;
}

this.emit = function(type) {
return namespace._emit(type, _.slice(arguments, 1));
}

this.emitFast = function(type) {
const data = _.slice(arguments, 1);

Expand All @@ -182,16 +183,17 @@ const Namespace = function(options = {}, emitter = {}) {
return namespace.listenerStats;
}

this.use = function(handler, once) {
this.use = function(handler, once, skip) {
namespace.middlewares.push({
handler: handler,
once: once
once: once,
skip: skip
});

return namespace;
}
this.useOnce = function(handler) {
return namespace.use(handler, true);
this.useOnce = function(handler, skip) {
return namespace.use(handler, true, skip);
}
this.catch = function(handler) {
namespace.catches.push(handler);
Expand Down Expand Up @@ -262,13 +264,15 @@ const Namespace = function(options = {}, emitter = {}) {
callback(listeners[0].handler.apply(listeners[0], data));
}
}
this.executeChain = function(chain, getArgs, handlerExecuted, callback) {
this.executeChain = function(skipMarked, chain, getArgs, handlerExecuted, callback) {
if (!chain.length) return callback();

var i = 0;
var once;

async.eachSeries(chain, function(handler, callback) {
if (handler.skip && skipMarked) return callback();

const next = function(error) {
callback(error || null);
}
Expand All @@ -289,8 +293,9 @@ const Namespace = function(options = {}, emitter = {}) {
i++;
}, callback);
}
this.executeMiddlewares = function(type, data, callback) {
this.executeMiddlewares = function(skip, type, data, callback) {
namespace.executeChain(
skip,
namespace.middlewares,
(handler, next) => [type, data, handler.length > 2 ? next : undefined],
function(handler, result, next) {
Expand All @@ -306,6 +311,7 @@ const Namespace = function(options = {}, emitter = {}) {
if (!namespace.catches.length) throw error;

namespace.executeChain(
false,
namespace.catches,
(handler, next) => [type, args, error, next],
function(handler, result, next) {
Expand Down

0 comments on commit 5c74679

Please sign in to comment.