Comparing version 0.3.2 to 0.4.0
60
index.js
@@ -32,3 +32,4 @@ var q = require('q'); | ||
subscriptions = {}, | ||
filters = {}, | ||
transforms = {}, | ||
moderators = {}, | ||
forwards = {}; | ||
@@ -56,13 +57,24 @@ | ||
var selectedFilters = getByTopic(filters, topic); | ||
var selectedSubs = getByTopic(subscriptions, topic); | ||
return q(true).then(function () { | ||
var selectedTransforms = getByTopic(transforms, topic); | ||
var data = (input) ? clone(input) : {}; | ||
var promiseArgs = q(data); | ||
selectedFilters.forEach(function (filter) { | ||
promiseArgs = promiseArgs.then(filter); | ||
selectedTransforms.forEach(function (transform) { | ||
promiseArgs = promiseArgs.then(transform); | ||
}); | ||
return promiseArgs; | ||
}).then(function (data) { | ||
if (moderator[topic]) { | ||
return q().then(function () { | ||
return moderator[topic](topic, data); | ||
}).then(function (newTopic) { | ||
topic = newTopic; | ||
return data; | ||
}) | ||
} else { | ||
return data; | ||
} | ||
}).then(function (data) { | ||
var selectedSubs = getByTopic(subscriptions, topic); | ||
var todos = []; | ||
@@ -83,11 +95,19 @@ //call subscribers | ||
//register a filter function that gets called the same way as a subscribtion handler, but has to resolve to arguments that are supposed to be passed on | ||
//if filter function throws, the publish is instantly cancelled | ||
function filter(what, filter) { | ||
if (!filters[what]) { | ||
filters[what] = []; | ||
//register a transform function that gets called the same way as a subscribtion handler, but has to resolve to arguments that are supposed to be passed on | ||
//if transform function throws, the publish is instantly cancelled | ||
function transform(what, transform) { | ||
if (!transforms[what]) { | ||
transforms[what] = []; | ||
} | ||
filters[what].push(filter); | ||
transforms[what].push(transform); | ||
} | ||
function moderator(what, moderator) { | ||
if (!moderators[what]) { | ||
moderators[what] = moderator; | ||
} else { | ||
throw new Error("There can be only one moderator for topic. " + what); | ||
} | ||
} | ||
function forwardTo(chan) { | ||
@@ -108,3 +128,4 @@ if (chan.name !== name) { | ||
subscriptions = {}; | ||
filters = {}; | ||
transforms = {}; | ||
moderators = {}; | ||
forwards = {}; | ||
@@ -115,2 +136,3 @@ } | ||
return function (definition) { | ||
api.promises = q; | ||
return definition(api); | ||
@@ -123,13 +145,13 @@ } | ||
publisher: mkDef({ | ||
publish: publish, | ||
promises: q | ||
publish: publish | ||
}), | ||
subscriber: mkDef({ | ||
subscribe: subscribe, | ||
unsubscribeAll: unsubscribeAll, | ||
promises: q | ||
unsubscribeAll: unsubscribeAll | ||
}), | ||
filter: mkDef({ | ||
filter: filter, | ||
promises: q | ||
transform: mkDef({ | ||
transform: transform | ||
}), | ||
moderator: mkDef({ | ||
moderator: moderator | ||
}) | ||
@@ -136,0 +158,0 @@ }, |
{ | ||
"name": "axons", | ||
"version": "0.3.2", | ||
"version": "0.4.0", | ||
"description": "A communication channel you always wanted instead of pub-sub", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
24107
577