Comparing version 0.2.0 to 0.3.0
58
index.js
@@ -17,12 +17,9 @@ var q = require('q'); | ||
function topicHandler(topic) { | ||
var t = topic.split('#'); | ||
return { | ||
to: t[0], | ||
hash: t[1] | ||
} | ||
function getByTopic(collection, topic) { | ||
var parentTopic = topic.replace(/\.[^.]+$/, ''); | ||
return [].concat(collection[topic], (topic !== parentTopic ? getByTopic(collection, parentTopic) : [])); | ||
} | ||
function init() { | ||
@@ -37,8 +34,6 @@ | ||
function subscribe(topic, func) { | ||
var t = topicHandler(topic); | ||
if (!subscriptions[t.to]) { | ||
subscriptions[t.to] = []; | ||
if (!subscriptions[topic]) { | ||
subscriptions[topic] = []; | ||
} | ||
subscriptions[t.to].push({ | ||
hash: t.hash, | ||
subscriptions[topic].push({ | ||
func: func | ||
@@ -49,12 +44,5 @@ }); | ||
//clears the topic | ||
function unsubscribe(topic) { | ||
var t = topicHandler(topic); | ||
if (!t.hash) { | ||
if (subscriptions[t.to] && (subscriptions[t.to].length > 0)) { | ||
subscriptions[t.to] = []; | ||
} | ||
} else { | ||
subscriptions[t.to] = subscriptions[t.to].filter(function (s) { | ||
return s.hash !== t.hash; | ||
}); | ||
function unsubscribeAll(topic) { | ||
if (subscriptions[topic]) { | ||
subscriptions[topic] = []; | ||
} | ||
@@ -65,23 +53,19 @@ } | ||
function publish(topic, input) { | ||
var t = topicHandler(topic); | ||
var selectedFilters = getByTopic(filters, topic); | ||
var selectedSubs = getByTopic(subscriptions, topic); | ||
return q(true).then(function () { | ||
var data = (input) ? clone(input) : {}; | ||
var promiseArgs = q(data); | ||
if (filters[t.to] && filters[t.to].length > 0) { | ||
filters[t.to].forEach(function (args, filter) { | ||
promiseArgs = promiseArgs.then(filter); | ||
}); | ||
} | ||
selectedFilters.forEach(function (args, filter) { | ||
promiseArgs = promiseArgs.then(filter); | ||
}); | ||
return promiseArgs; | ||
}).then(function (data) { | ||
var todos = []; | ||
if (subscriptions[t.to] && subscriptions[t.to].length > 0) { | ||
//call subscribers | ||
todos = todos.concat(subscriptions[t.to].filter(function (subber) { | ||
return (!subber.hash || subber.hash === t.hash); | ||
}).map(function (subber) { | ||
return subber.func(data); | ||
})); | ||
} | ||
//call subscribers | ||
todos = todos.concat(selectedSubs.map(function (subber) { | ||
return subber.func(data); | ||
})); | ||
for (var ch in forwards) { | ||
@@ -139,3 +123,3 @@ //TODO, this is kinda inconsequent, resolutions from there are going to be returned as arrays | ||
subscribe: subscribe, | ||
unsubscribe: unsubscribe, | ||
unsubscribeAll: unsubscribeAll, | ||
promises: q | ||
@@ -142,0 +126,0 @@ }), |
{ | ||
"name": "axons", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "A communication channel you always wanted instead of pub-sub", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
23902
567