Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Bumped postal.js to version 0.8.3

  • Loading branch information...
commit d8f298f9a5f587b8424787493753d17fc5e6fc10 1 parent 434c2d7
@ifandelse ifandelse authored
View
404 ajax/libs/postal.js/0.8.3/postal.js
@@ -0,0 +1,404 @@
+/*
+ postal
+ Author: Jim Cowart (http://freshbrewedcode.com/jimcowart)
+ License: Dual licensed MIT (http://www.opensource.org/licenses/mit-license) & GPL (http://www.opensource.org/licenses/gpl-license)
+ Version 0.8.3
+ */
+(function ( root, factory ) {
+ if ( typeof module === "object" && module.exports ) {
+ // Node, or CommonJS-Like environments
+ module.exports = function ( _ ) {
+ _ = _ || require( "underscore" );
+ return factory( _ );
+ }
+ } 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 ) {
+
+ var DEFAULT_CHANNEL = "/",
+ DEFAULT_DISPOSEAFTER = 0,
+ SYSTEM_CHANNEL = "postal";
+ var ConsecutiveDistinctPredicate = function () {
+ var previous;
+ return function ( data ) {
+ var eq = false;
+ if ( _.isString( data ) ) {
+ eq = data === previous;
+ previous = data;
+ }
+ else {
+ eq = _.isEqual( data, previous );
+ previous = _.clone( data );
+ }
+ return !eq;
+ };
+ };
+ var DistinctPredicate = function () {
+ var previous = [];
+
+ return function ( data ) {
+ var isDistinct = !_.any( previous, function ( p ) {
+ if ( _.isObject( data ) || _.isArray( data ) ) {
+ return _.isEqual( data, p );
+ }
+ return data === p;
+ } );
+ if ( isDistinct ) {
+ previous.push( data );
+ }
+ return isDistinct;
+ };
+ };
+ var ChannelDefinition = function ( channelName ) {
+ this.channel = channelName || DEFAULT_CHANNEL;
+ };
+
+ ChannelDefinition.prototype.subscribe = function () {
+ return arguments.length === 1 ?
+ new SubscriptionDefinition( this.channel, arguments[0].topic, arguments[0].callback ) :
+ new SubscriptionDefinition( this.channel, arguments[0], arguments[1] );
+ };
+
+ 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;
+ return postal.configuration.bus.publish( envelope );
+ };
+ var SubscriptionDefinition = function ( channel, topic, callback ) {
+ this.channel = channel;
+ this.topic = topic;
+ this.callback = callback;
+ this.constraints = [];
+ this.context = null;
+ postal.configuration.bus.publish( {
+ channel : SYSTEM_CHANNEL,
+ topic : "subscription.created",
+ data : {
+ event : "subscription.created",
+ channel : channel,
+ topic : topic
+ }
+ } );
+ postal.configuration.bus.subscribe( this );
+ };
+
+ SubscriptionDefinition.prototype = {
+ unsubscribe : function () {
+ postal.configuration.bus.unsubscribe( this );
+ postal.configuration.bus.publish( {
+ channel : SYSTEM_CHANNEL,
+ topic : "subscription.removed",
+ data : {
+ event : "subscription.removed",
+ channel : this.channel,
+ topic : this.topic
+ }
+ } );
+ },
+
+ defer : function () {
+ var fn = this.callback;
+ this.callback = function ( data ) {
+ setTimeout( function () {
+ fn( data );
+ }, 0 );
+ };
+ return this;
+ },
+
+ disposeAfter : function ( maxCalls ) {
+ if ( _.isNaN( maxCalls ) || maxCalls <= 0 ) {
+ throw "The value provided to disposeAfter (maxCalls) must be a number greater than zero.";
+ }
+ var fn = this.callback;
+ var dispose = _.after( maxCalls, _.bind( function () {
+ this.unsubscribe();
+ }, this ) );
+
+ this.callback = function () {
+ fn.apply( this.context, arguments );
+ dispose();
+ };
+ return this;
+ },
+
+ distinctUntilChanged : function () {
+ this.withConstraint( new ConsecutiveDistinctPredicate() );
+ return this;
+ },
+
+ distinct : function () {
+ this.withConstraint( new DistinctPredicate() );
+ return this;
+ },
+
+ once : function () {
+ this.disposeAfter( 1 );
+ },
+
+ withConstraint : function ( predicate ) {
+ if ( !_.isFunction( predicate ) ) {
+ throw "Predicate constraint must be a function";
+ }
+ this.constraints.push( predicate );
+ return this;
+ },
+
+ withConstraints : function ( predicates ) {
+ var self = this;
+ if ( _.isArray( predicates ) ) {
+ _.each( predicates, function ( predicate ) {
+ self.withConstraint( predicate );
+ } );
+ }
+ return self;
+ },
+
+ withContext : function ( context ) {
+ this.context = context;
+ return this;
+ },
+
+ withDebounce : function ( milliseconds ) {
+ if ( _.isNaN( milliseconds ) ) {
+ throw "Milliseconds must be a number";
+ }
+ var fn = this.callback;
+ this.callback = _.debounce( fn, milliseconds );
+ return this;
+ },
+
+ withDelay : function ( milliseconds ) {
+ if ( _.isNaN( milliseconds ) ) {
+ throw "Milliseconds must be a number";
+ }
+ var fn = this.callback;
+ this.callback = function ( data ) {
+ setTimeout( function () {
+ fn( data );
+ }, milliseconds );
+ };
+ return this;
+ },
+
+ withThrottle : function ( milliseconds ) {
+ if ( _.isNaN( milliseconds ) ) {
+ throw "Milliseconds must be a number";
+ }
+ var fn = this.callback;
+ this.callback = _.throttle( fn, milliseconds );
+ return this;
+ },
+
+ subscribe : function ( callback ) {
+ this.callback = callback;
+ return this;
+ }
+ };
+ var bindingsResolver = {
+ cache : {},
+ regex : {},
+
+ compare : function ( binding, topic ) {
+ var pattern, rgx, prev, 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 = !!prev && prev !== "#" ? "\\.\\b" : "\\b";
+ if(segment === "#") {
+ res += "[A-Z,a-z,0-9,\\.]*"
+ } else if (segment === "*") {
+ res += "[A-Z,a-z,0-9]+"
+ } else {
+ res += segment;
+ }
+ prev = 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) {
+ if ( postal.configuration.resolver.compare( subDef.topic, envelope.topic ) ) {
+ if ( _.all( subDef.constraints, function ( constraint ) {
+ return constraint.call( subDef.context, envelope.data, envelope );
+ } ) ) {
+ if ( typeof subDef.callback === 'function' ) {
+ subDef.callback.call( subDef.context, envelope.data, envelope );
+ }
+ }
+ }
+ };
+
+ var localBus = {
+ addWireTap : function ( callback ) {
+ var self = this;
+ self.wireTaps.push( callback );
+ return function () {
+ var idx = self.wireTaps.indexOf( callback );
+ if ( idx !== -1 ) {
+ self.wireTaps.splice( idx, 1 );
+ }
+ };
+ },
+
+ publish : function ( envelope ) {
+ 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);
+ }
+ }
+ } );
+ }
+ return envelope;
+ },
+
+ reset : function () {
+ if ( this.subscriptions ) {
+ _.each( this.subscriptions, function ( channel ) {
+ _.each( channel, function ( topic ) {
+ while ( topic.length ) {
+ topic.pop().unsubscribe();
+ }
+ } );
+ } );
+ this.subscriptions = {};
+ }
+ },
+
+ subscribe : function ( subDef ) {
+ var idx, found, fn, channel = this.subscriptions[subDef.channel], subs;
+ 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;
+ },
+
+ subscriptions : {},
+
+ wireTaps : [],
+
+ unsubscribe : function ( config ) {
+ if ( this.subscriptions[config.channel][config.topic] ) {
+ var len = this.subscriptions[config.channel][config.topic].length,
+ idx = 0;
+ for ( ; idx < len; idx++ ) {
+ if ( this.subscriptions[config.channel][config.topic][idx] === config ) {
+ this.subscriptions[config.channel][config.topic].splice( idx, 1 );
+ break;
+ }
+ }
+ }
+ }
+ };
+ localBus.subscriptions[SYSTEM_CHANNEL] = {};
+ var postal = {
+ configuration : {
+ bus : localBus,
+ resolver : bindingsResolver,
+ DEFAULT_CHANNEL : DEFAULT_CHANNEL,
+ SYSTEM_CHANNEL : SYSTEM_CHANNEL
+ },
+
+ ChannelDefinition : ChannelDefinition,
+ SubscriptionDefinition : SubscriptionDefinition,
+
+ channel : function ( channelName ) {
+ return new ChannelDefinition( channelName );
+ },
+
+ subscribe : function ( options ) {
+ return new SubscriptionDefinition( options.channel || DEFAULT_CHANNEL, options.topic, options.callback );
+ },
+
+ publish : function ( envelope ) {
+ envelope.channel = envelope.channel || DEFAULT_CHANNEL;
+ return postal.configuration.bus.publish( envelope );
+ },
+
+ addWireTap : function ( callback ) {
+ return this.configuration.bus.addWireTap( callback );
+ },
+
+ linkChannels : function ( sources, destinations ) {
+ var result = [];
+ sources = !_.isArray( sources ) ? [sources] : sources;
+ destinations = !_.isArray( destinations ) ? [destinations] : destinations;
+ _.each( sources, function ( source ) {
+ var sourceTopic = source.topic || "#";
+ _.each( destinations, function ( destination ) {
+ var destChannel = destination.channel || DEFAULT_CHANNEL;
+ result.push(
+ postal.subscribe( {
+ channel : source.channel || DEFAULT_CHANNEL,
+ topic : source.topic || "#",
+ callback : function ( data, env ) {
+ var newEnv = _.clone( env );
+ newEnv.topic = _.isFunction( destination.topic ) ? destination.topic( env.topic ) : destination.topic || env.topic;
+ newEnv.channel = destChannel;
+ newEnv.data = data;
+ postal.publish( newEnv );
+ }
+ } )
+ );
+ } );
+ } );
+ return result;
+ },
+
+ utils : {
+ getSubscribersFor : function () {
+ var channel = arguments[ 0 ],
+ tpc = arguments[ 1 ];
+ if ( arguments.length === 1 ) {
+ channel = arguments[ 0 ].channel || postal.configuration.DEFAULT_CHANNEL;
+ tpc = arguments[ 0 ].topic;
+ }
+ if ( postal.configuration.bus.subscriptions[ channel ] &&
+ postal.configuration.bus.subscriptions[ channel ].hasOwnProperty( tpc ) ) {
+ return postal.configuration.bus.subscriptions[ channel ][ tpc ];
+ }
+ return [];
+ },
+
+ reset : function () {
+ postal.configuration.bus.reset();
+ postal.configuration.resolver.reset();
+ }
+ }
+ };
+
+ return postal;
+} ));
View
7 ajax/libs/postal.js/0.8.3/postal.min.js
@@ -0,0 +1,7 @@
+/*
+ postal
+ Author: Jim Cowart (http://freshbrewedcode.com/jimcowart)
+ License: Dual licensed MIT (http://www.opensource.org/licenses/mit-license) & GPL (http://www.opensource.org/licenses/gpl-license)
+ Version 0.8.3
+ */
+(function(n,t){"object"==typeof module&&module.exports?module.exports=function(n){return n=n||require("underscore"),t(n)}:"function"==typeof define&&define.amd?define(["underscore"],function(i){return t(i,n)}):n.postal=t(n._,n)})(this,function(n){var t="/",i="postal",s=function(){var t;return function(i){var s=!1;return n.isString(i)?(s=i===t,t=i):(s=n.isEqual(i,t),t=n.clone(i)),!s}},c=function(){var t=[];return function(i){var s=!n.any(t,function(t){return n.isObject(i)||n.isArray(i)?n.isEqual(i,t):i===t});return s&&t.push(i),s}},e=function(n){this.channel=n||t};e.prototype.subscribe=function(){return 1===arguments.length?new r(this.channel,arguments[0].topic,arguments[0].callback):new r(this.channel,arguments[0],arguments[1])},e.prototype.publish=function(){var n=1===arguments.length?"[object String]"===Object.prototype.toString.call(arguments[0])?{topic:arguments[0]}:arguments[0]:{topic:arguments[0],data:arguments[1]};return n.channel=this.channel,h.configuration.bus.publish(n)};var r=function(n,t,s){this.channel=n,this.topic=t,this.callback=s,this.constraints=[],this.context=null,h.configuration.bus.publish({channel:i,topic:"subscription.created",data:{event:"subscription.created",channel:n,topic:t}}),h.configuration.bus.subscribe(this)};r.prototype={unsubscribe:function(){h.configuration.bus.unsubscribe(this),h.configuration.bus.publish({channel:i,topic:"subscription.removed",data:{event:"subscription.removed",channel:this.channel,topic:this.topic}})},defer:function(){var n=this.callback;return this.callback=function(t){setTimeout(function(){n(t)},0)},this},disposeAfter:function(t){if(n.isNaN(t)||0>=t)throw"The value provided to disposeAfter (maxCalls) must be a number greater than zero.";var i=this.callback,s=n.after(t,n.bind(function(){this.unsubscribe()},this));return this.callback=function(){i.apply(this.context,arguments),s()},this},distinctUntilChanged:function(){return this.withConstraint(new s),this},distinct:function(){return this.withConstraint(new c),this},once:function(){this.disposeAfter(1)},withConstraint:function(t){if(!n.isFunction(t))throw"Predicate constraint must be a function";return this.constraints.push(t),this},withConstraints:function(t){var i=this;return n.isArray(t)&&n.each(t,function(n){i.withConstraint(n)}),i},withContext:function(n){return this.context=n,this},withDebounce:function(t){if(n.isNaN(t))throw"Milliseconds must be a number";var i=this.callback;return this.callback=n.debounce(i,t),this},withDelay:function(t){if(n.isNaN(t))throw"Milliseconds must be a number";var i=this.callback;return this.callback=function(n){setTimeout(function(){i(n)},t)},this},withThrottle:function(t){if(n.isNaN(t))throw"Milliseconds must be a number";var i=this.callback;return this.callback=n.throttle(i,t),this},subscribe:function(n){return this.callback=n,this}};var o={cache:{},regex:{},compare:function(t,i){var s,c,e,r=this.cache[i]&&this.cache[i][t];return r!==undefined?r:((c=this.regex[t])||(s="^"+n.map(t.split("."),function(n){var t=e&&"#"!==e?"\\.\\b":"\\b";return t+="#"===n?"[A-Z,a-z,0-9,\\.]*":"*"===n?"[A-Z,a-z,0-9]+":n,e=n,t}).join("")+"$",c=this.regex[t]=RegExp(s)),this.cache[i]=this.cache[i]||{},this.cache[i][t]=r=c.test(i),r)},reset:function(){this.cache={},this.regex={}}},u=function(t,i){h.configuration.resolver.compare(t.topic,i.topic)&&n.all(t.constraints,function(n){return n.call(t.context,i.data,i)})&&"function"==typeof t.callback&&t.callback.call(t.context,i.data,i)},a={addWireTap:function(n){var t=this;return t.wireTaps.push(n),function(){var i=t.wireTaps.indexOf(n);-1!==i&&t.wireTaps.splice(i,1)}},publish:function(t){return t.timeStamp=new Date,n.each(this.wireTaps,function(n){n(t.data,t)}),this.subscriptions[t.channel]&&n.each(this.subscriptions[t.channel],function(n){for(var i,s=0,c=n.length;c>s;)(i=n[s++])&&u(i,t)}),t},reset:function(){this.subscriptions&&(n.each(this.subscriptions,function(t){n.each(t,function(n){for(;n.length;)n.pop().unsubscribe()})}),this.subscriptions={})},subscribe:function(n){var t,i=this.subscriptions[n.channel];return i||(i=this.subscriptions[n.channel]={}),t=this.subscriptions[n.channel][n.topic],t||(t=this.subscriptions[n.channel][n.topic]=[]),t.push(n),n},subscriptions:{},wireTaps:[],unsubscribe:function(n){if(this.subscriptions[n.channel][n.topic])for(var t=this.subscriptions[n.channel][n.topic].length,i=0;t>i;i++)if(this.subscriptions[n.channel][n.topic][i]===n){this.subscriptions[n.channel][n.topic].splice(i,1);break}}};a.subscriptions[i]={};var h={configuration:{bus:a,resolver:o,DEFAULT_CHANNEL:t,SYSTEM_CHANNEL:i},ChannelDefinition:e,SubscriptionDefinition:r,channel:function(n){return new e(n)},subscribe:function(n){return new r(n.channel||t,n.topic,n.callback)},publish:function(n){return n.channel=n.channel||t,h.configuration.bus.publish(n)},addWireTap:function(n){return this.configuration.bus.addWireTap(n)},linkChannels:function(i,s){var c=[];return i=n.isArray(i)?i:[i],s=n.isArray(s)?s:[s],n.each(i,function(i){i.topic||"#",n.each(s,function(s){var e=s.channel||t;c.push(h.subscribe({channel:i.channel||t,topic:i.topic||"#",callback:function(t,i){var c=n.clone(i);c.topic=n.isFunction(s.topic)?s.topic(i.topic):s.topic||i.topic,c.channel=e,c.data=t,h.publish(c)}}))})}),c},utils:{getSubscribersFor:function(){var n=arguments[0],t=arguments[1];return 1===arguments.length&&(n=arguments[0].channel||h.configuration.DEFAULT_CHANNEL,t=arguments[0].topic),h.configuration.bus.subscriptions[n]&&h.configuration.bus.subscriptions[n].hasOwnProperty(t)?h.configuration.bus.subscriptions[n][t]:[]},reset:function(){h.configuration.bus.reset(),h.configuration.resolver.reset()}}};return h});
View
8 ajax/libs/postal.js/package.json
@@ -2,7 +2,7 @@
"name" : "postal.js",
"filename": "postal.min.js",
"description" : "Pub/Sub library providing wildcard subscriptions, complex message handling, etc. Works server and client-side.",
- "version" : "0.8.2",
+ "version" : "0.8.3",
"url" : "http://github.com/postaljs/postal.js",
"homepage" : "http://github.com/postaljs/postal.js",
"repository" : {
@@ -22,9 +22,9 @@
"url" : "http://freshbrewedcode.com/alexrobson"
},
{
- "name" : "Nicholas Cloud",
- "email" : "WhyNotJustComment@OnMyBlog.com",
- "url" : "http://nicholascloud.com"
+ "name" : "Nicholas Cloud",
+ "email" : "WhyNotJustComment@OnMyBlog.com",
+ "url" : "http://nicholascloud.com"
},
{
"name" : "Doug Neiner",
Please sign in to comment.
Something went wrong with that request. Please try again.