2014-01-27 06:52:22 +00:00
|
|
|
/**
|
|
|
|
|
* postal - Pub/Sub library providing wildcard subscriptions, complex message handling, etc. Works server and client-side.
|
|
|
|
|
* Author: Jim Cowart (http://freshbrewedcode.com/jimcowart)
|
2014-02-11 19:41:38 +00:00
|
|
|
* Version: v0.9.0-rc1
|
2014-01-27 06:52:22 +00:00
|
|
|
* Url: http://github.com/postaljs/postal.js
|
|
|
|
|
* License(s): MIT, GPL
|
|
|
|
|
*/
|
|
|
|
|
(function (root, factory) {
|
|
|
|
|
if (typeof module === "object" && module.exports) {
|
|
|
|
|
// Node, or CommonJS-Like environments
|
|
|
|
|
module.exports = factory(require("underscore"), this);
|
|
|
|
|
} else if (typeof define === "function" && define.amd) {
|
|
|
|
|
// AMD. Register as an anonymous module.
|
|
|
|
|
define(["underscore"], function (_) {
|
|
|
|
|
return factory(_, root);
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
// Browser globals
|
|
|
|
|
root.postal = factory(root._, root);
|
|
|
|
|
}
|
|
|
|
|
}(this, function (_, global, undefined) {
|
2014-01-27 21:59:45 +00:00
|
|
|
var _postal;
|
2014-01-27 06:52:22 +00:00
|
|
|
var prevPostal = global.postal;
|
|
|
|
|
var ChannelDefinition = function (channelName) {
|
2014-01-27 21:59:45 +00:00
|
|
|
this.channel = channelName || _postal.configuration.DEFAULT_CHANNEL;
|
2014-01-29 07:25:51 +00:00
|
|
|
this.initialize();
|
2014-01-27 06:52:22 +00:00
|
|
|
};
|
2014-01-29 07:25:51 +00:00
|
|
|
ChannelDefinition.prototype.initialize = function () {};
|
2014-01-27 06:52:22 +00:00
|
|
|
ChannelDefinition.prototype.subscribe = function () {
|
2014-01-29 07:25:51 +00:00
|
|
|
return _postal.subscribe({
|
|
|
|
|
channel: this.channel,
|
|
|
|
|
topic: (arguments.length === 1 ? arguments[0].topic : arguments[0]),
|
|
|
|
|
callback: (arguments.length === 1 ? arguments[0].callback : arguments[1])
|
|
|
|
|
});
|
2014-01-27 06:52:22 +00:00
|
|
|
};
|
|
|
|
|
ChannelDefinition.prototype.publish = function () {
|
|
|
|
|
var envelope = arguments.length === 1 ? (Object.prototype.toString.call(arguments[0]) === "[object String]" ? {
|
|
|
|
|
topic: arguments[0]
|
|
|
|
|
} : arguments[0]) : {
|
|
|
|
|
topic: arguments[0],
|
|
|
|
|
data: arguments[1]
|
|
|
|
|
};
|
|
|
|
|
envelope.channel = this.channel;
|
2014-01-29 07:25:51 +00:00
|
|
|
_postal.publish(envelope);
|
2014-01-27 06:52:22 +00:00
|
|
|
};
|
|
|
|
|
var SubscriptionDefinition = function (channel, topic, callback) {
|
|
|
|
|
if (arguments.length !== 3) {
|
|
|
|
|
throw new Error("You must provide a channel, topic and callback when creating a SubscriptionDefinition instance.");
|
|
|
|
|
}
|
|
|
|
|
if (topic.length === 0) {
|
|
|
|
|
throw new Error("Topics cannot be empty");
|
|
|
|
|
}
|
|
|
|
|
this.channel = channel;
|
|
|
|
|
this.topic = topic;
|
|
|
|
|
this.subscribe(callback);
|
|
|
|
|
};
|
|
|
|
|
SubscriptionDefinition.prototype = {
|
|
|
|
|
unsubscribe: function () {
|
|
|
|
|
if (!this.inactive) {
|
|
|
|
|
this.inactive = true;
|
2014-01-27 21:59:45 +00:00
|
|
|
_postal.unsubscribe(this);
|
2014-01-27 06:52:22 +00:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
subscribe: function (callback) {
|
|
|
|
|
this.callback = callback;
|
|
|
|
|
return this;
|
|
|
|
|
},
|
|
|
|
|
withContext: function (context) {
|
2014-01-29 07:25:51 +00:00
|
|
|
this.context = context;
|
2014-01-27 06:52:22 +00:00
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
var bindingsResolver = {
|
|
|
|
|
cache: {},
|
|
|
|
|
regex: {},
|
|
|
|
|
compare: function (binding, topic) {
|
|
|
|
|
var pattern, rgx, prevSegment, result = (this.cache[topic] && this.cache[topic][binding]);
|
|
|
|
|
if (typeof result !== "undefined") {
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
if (!(rgx = this.regex[binding])) {
|
|
|
|
|
pattern = "^" + _.map(binding.split("."), function (segment) {
|
|
|
|
|
var res = "";
|
|
|
|
|
if ( !! prevSegment) {
|
|
|
|
|
res = prevSegment !== "#" ? "\\.\\b" : "\\b";
|
|
|
|
|
}
|
|
|
|
|
if (segment === "#") {
|
|
|
|
|
res += "[\\s\\S]*";
|
|
|
|
|
} else if (segment === "*") {
|
|
|
|
|
res += "[^.]+";
|
|
|
|
|
} else {
|
|
|
|
|
res += segment;
|
|
|
|
|
}
|
|
|
|
|
prevSegment = segment;
|
|
|
|
|
return res;
|
|
|
|
|
}).join("") + "$";
|
|
|
|
|
rgx = this.regex[binding] = new RegExp(pattern);
|
|
|
|
|
}
|
|
|
|
|
this.cache[topic] = this.cache[topic] || {};
|
|
|
|
|
this.cache[topic][binding] = result = rgx.test(topic);
|
|
|
|
|
return result;
|
|
|
|
|
},
|
|
|
|
|
reset: function () {
|
|
|
|
|
this.cache = {};
|
|
|
|
|
this.regex = {};
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
var fireSub = function (subDef, envelope) {
|
2014-01-27 21:59:45 +00:00
|
|
|
if (!subDef.inactive && _postal.configuration.resolver.compare(subDef.topic, envelope.topic)) {
|
2014-01-29 07:25:51 +00:00
|
|
|
subDef.callback.call(subDef.context || this, envelope.data, envelope);
|
2014-01-27 06:52:22 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
var pubInProgress = 0;
|
|
|
|
|
var unSubQueue = [];
|
|
|
|
|
var clearUnSubQueue = function () {
|
|
|
|
|
while (unSubQueue.length) {
|
2014-01-27 21:59:45 +00:00
|
|
|
_postal.unsubscribe(unSubQueue.shift());
|
2014-01-27 06:52:22 +00:00
|
|
|
}
|
|
|
|
|
};
|
2014-01-27 21:59:45 +00:00
|
|
|
_postal = {
|
|
|
|
|
configuration: {
|
|
|
|
|
resolver: bindingsResolver,
|
|
|
|
|
DEFAULT_CHANNEL: "/",
|
|
|
|
|
SYSTEM_CHANNEL: "postal"
|
|
|
|
|
},
|
|
|
|
|
subscriptions: {},
|
|
|
|
|
wireTaps: [],
|
|
|
|
|
ChannelDefinition: ChannelDefinition,
|
|
|
|
|
SubscriptionDefinition: SubscriptionDefinition,
|
|
|
|
|
channel: function (channelName) {
|
|
|
|
|
return new ChannelDefinition(channelName);
|
|
|
|
|
},
|
|
|
|
|
subscribe: function (options) {
|
|
|
|
|
var subDef = new SubscriptionDefinition(options.channel || this.configuration.DEFAULT_CHANNEL, options.topic, options.callback);
|
|
|
|
|
var channel = this.subscriptions[subDef.channel];
|
|
|
|
|
var subs;
|
|
|
|
|
this.publish({
|
|
|
|
|
channel: this.configuration.SYSTEM_CHANNEL,
|
|
|
|
|
topic: "subscription.created",
|
|
|
|
|
data: {
|
|
|
|
|
event: "subscription.created",
|
|
|
|
|
channel: subDef.channel,
|
|
|
|
|
topic: subDef.topic
|
2014-01-27 06:52:22 +00:00
|
|
|
}
|
2014-01-27 21:59:45 +00:00
|
|
|
});
|
|
|
|
|
if (!channel) {
|
|
|
|
|
channel = this.subscriptions[subDef.channel] = {};
|
|
|
|
|
}
|
|
|
|
|
subs = this.subscriptions[subDef.channel][subDef.topic];
|
|
|
|
|
if (!subs) {
|
|
|
|
|
subs = this.subscriptions[subDef.channel][subDef.topic] = [];
|
|
|
|
|
}
|
|
|
|
|
subs.push(subDef);
|
|
|
|
|
return subDef;
|
2014-01-27 06:52:22 +00:00
|
|
|
},
|
|
|
|
|
publish: function (envelope) {
|
|
|
|
|
++pubInProgress;
|
2014-01-27 21:59:45 +00:00
|
|
|
envelope.channel = envelope.channel || this.configuration.DEFAULT_CHANNEL;
|
2014-01-27 06:52:22 +00:00
|
|
|
envelope.timeStamp = new Date();
|
|
|
|
|
_.each(this.wireTaps, function (tap) {
|
|
|
|
|
tap(envelope.data, envelope);
|
|
|
|
|
});
|
|
|
|
|
if (this.subscriptions[envelope.channel]) {
|
|
|
|
|
_.each(this.subscriptions[envelope.channel], function (subscribers) {
|
|
|
|
|
var idx = 0,
|
|
|
|
|
len = subscribers.length,
|
|
|
|
|
subDef;
|
|
|
|
|
while (idx < len) {
|
|
|
|
|
if (subDef = subscribers[idx++]) {
|
|
|
|
|
fireSub(subDef, envelope);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
if (--pubInProgress === 0) {
|
|
|
|
|
clearUnSubQueue();
|
|
|
|
|
}
|
|
|
|
|
},
|
2014-01-27 21:59:45 +00:00
|
|
|
unsubscribe: function (subDef) {
|
2014-01-27 06:52:22 +00:00
|
|
|
if (pubInProgress) {
|
2014-01-27 21:59:45 +00:00
|
|
|
unSubQueue.push(subDef);
|
2014-01-27 06:52:22 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2014-01-27 21:59:45 +00:00
|
|
|
if (this.subscriptions[subDef.channel] && this.subscriptions[subDef.channel][subDef.topic]) {
|
|
|
|
|
var len = this.subscriptions[subDef.channel][subDef.topic].length,
|
2014-01-27 06:52:22 +00:00
|
|
|
idx = 0;
|
|
|
|
|
while (idx < len) {
|
2014-01-27 21:59:45 +00:00
|
|
|
if (this.subscriptions[subDef.channel][subDef.topic][idx] === subDef) {
|
|
|
|
|
this.subscriptions[subDef.channel][subDef.topic].splice(idx, 1);
|
2014-01-27 06:52:22 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
idx += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-01-27 21:59:45 +00:00
|
|
|
this.publish({
|
|
|
|
|
channel: this.configuration.SYSTEM_CHANNEL,
|
2014-01-27 06:52:22 +00:00
|
|
|
topic: "subscription.removed",
|
|
|
|
|
data: {
|
|
|
|
|
event: "subscription.removed",
|
2014-01-27 21:59:45 +00:00
|
|
|
channel: subDef.channel,
|
|
|
|
|
topic: subDef.topic
|
2014-01-27 06:52:22 +00:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
addWireTap: function (callback) {
|
2014-01-27 21:59:45 +00:00
|
|
|
var self = this;
|
|
|
|
|
self.wireTaps.push(callback);
|
|
|
|
|
return function () {
|
|
|
|
|
var idx = self.wireTaps.indexOf(callback);
|
|
|
|
|
if (idx !== -1) {
|
|
|
|
|
self.wireTaps.splice(idx, 1);
|
|
|
|
|
}
|
|
|
|
|
};
|
2014-01-27 06:52:22 +00:00
|
|
|
},
|
|
|
|
|
noConflict: function () {
|
2014-02-07 18:49:04 +00:00
|
|
|
if (typeof window === "undefined" || (typeof window !== "undefined" && typeof define === "function" && define.amd)) {
|
2014-01-27 06:52:22 +00:00
|
|
|
throw new Error("noConflict can only be used in browser clients which aren't using AMD modules");
|
|
|
|
|
}
|
|
|
|
|
global.postal = prevPostal;
|
|
|
|
|
return this;
|
|
|
|
|
},
|
2014-01-27 21:59:45 +00:00
|
|
|
getSubscribersFor: function () {
|
|
|
|
|
var channel = arguments[0],
|
|
|
|
|
tpc = arguments[1];
|
|
|
|
|
if (arguments.length === 1) {
|
|
|
|
|
channel = arguments[0].channel || this.configuration.DEFAULT_CHANNEL;
|
|
|
|
|
tpc = arguments[0].topic;
|
|
|
|
|
}
|
|
|
|
|
if (this.subscriptions[channel] && Object.prototype.hasOwnProperty.call(this.subscriptions[channel], tpc)) {
|
|
|
|
|
return this.subscriptions[channel][tpc];
|
|
|
|
|
}
|
|
|
|
|
return [];
|
|
|
|
|
},
|
|
|
|
|
reset: function () {
|
|
|
|
|
if (this.subscriptions) {
|
|
|
|
|
_.each(this.subscriptions, function (channel) {
|
|
|
|
|
_.each(channel, function (topic) {
|
|
|
|
|
while (topic.length) {
|
|
|
|
|
topic.pop().unsubscribe();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
this.subscriptions = {};
|
2014-01-27 06:52:22 +00:00
|
|
|
}
|
2014-01-27 21:59:45 +00:00
|
|
|
this.configuration.resolver.reset();
|
2014-01-27 06:52:22 +00:00
|
|
|
}
|
|
|
|
|
};
|
2014-01-27 21:59:45 +00:00
|
|
|
_postal.subscriptions[_postal.configuration.SYSTEM_CHANNEL] = {};
|
2014-01-27 06:52:22 +00:00
|
|
|
if (global && Object.prototype.hasOwnProperty.call(global, "__postalReady__") && _.isArray(global.__postalReady__)) {
|
|
|
|
|
while (global.__postalReady__.length) {
|
2014-01-27 21:59:45 +00:00
|
|
|
global.__postalReady__.shift().onReady(_postal);
|
2014-01-27 06:52:22 +00:00
|
|
|
}
|
|
|
|
|
}
|
2014-01-27 21:59:45 +00:00
|
|
|
return _postal;
|
2014-01-27 06:52:22 +00:00
|
|
|
}));
|