Updated __postalReady__ array logic to expect objects with onReady callbacks

This commit is contained in:
ifandelse 2013-09-06 11:11:29 -04:00
parent 5a74064466
commit 5d1e7f8f88
7 changed files with 139 additions and 105 deletions

View file

@ -4,13 +4,14 @@
License: Dual licensed MIT (http://www.opensource.org/licenses/mit-license) & GPL (http://www.opensource.org/licenses/gpl-license)
Version 0.8.7
*/
/*jshint -W098 */
(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 ( _ ) {
@ -22,9 +23,9 @@
}
}( this, function ( _, global, undefined ) {
var DEFAULT_CHANNEL = "/",
DEFAULT_DISPOSEAFTER = 0,
SYSTEM_CHANNEL = "postal";
var postal;
/*jshint -W098 */
var ConsecutiveDistinctPredicate = function () {
var previous;
return function ( data ) {
@ -40,6 +41,7 @@
return !eq;
};
};
/*jshint -W098 */
var DistinctPredicate = function () {
var previous = [];
@ -56,8 +58,9 @@
return isDistinct;
};
};
/* global postal, SubscriptionDefinition */
var ChannelDefinition = function ( channelName ) {
this.channel = channelName || DEFAULT_CHANNEL;
this.channel = channelName || postal.configuration.DEFAULT_CHANNEL;
};
ChannelDefinition.prototype.subscribe = function () {
@ -68,13 +71,15 @@
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] };
( 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 );
};
/* global postal */
/*jshint -W117 */
var SubscriptionDefinition = function ( channel, topic, callback ) {
this.channel = channel;
this.topic = topic;
@ -82,12 +87,12 @@
this.constraints = [];
this.context = null;
postal.configuration.bus.publish( {
channel : SYSTEM_CHANNEL,
topic : "subscription.created",
data : {
event : "subscription.created",
channel : postal.configuration.SYSTEM_CHANNEL,
topic : "subscription.created",
data : {
event : "subscription.created",
channel : channel,
topic : topic
topic : topic
}
} );
postal.configuration.bus.subscribe( this );
@ -95,16 +100,16 @@
SubscriptionDefinition.prototype = {
unsubscribe : function () {
if(!this.inactive) {
if ( !this.inactive ) {
this.inactive = true;
postal.configuration.bus.unsubscribe( this );
postal.configuration.bus.publish( {
channel : SYSTEM_CHANNEL,
topic : "subscription.removed",
data : {
event : "subscription.removed",
channel : postal.configuration.SYSTEM_CHANNEL,
topic : "subscription.removed",
data : {
event : "subscription.removed",
channel : this.channel,
topic : this.topic
topic : this.topic
}
} );
}
@ -210,35 +215,36 @@
return this;
}
};
/*jshint -W098 */
var bindingsResolver = {
cache : {},
regex : {},
compare : function ( binding, topic ) {
var pattern, rgx, prevSegment, result = (this.cache[topic] && this.cache[topic][binding]);
if(typeof result !== "undefined") {
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) {
if ( !( rgx = this.regex[ binding ] )) {
pattern = "^" + _.map( binding.split( "." ),function ( segment ) {
var res = "";
if (!!prevSegment) {
if ( !!prevSegment ) {
res = prevSegment !== "#" ? "\\.\\b" : "\\b";
}
if(segment === "#") {
res += "[\\s\\S]*"
} else if (segment === "*") {
res += "[^.]+"
if ( segment === "#" ) {
res += "[\\s\\S]*";
} else if ( segment === "*" ) {
res += "[^.]+";
} else {
res += segment;
}
prevSegment = segment;
return res;
} ).join('') + "$";
rgx = this.regex[binding] = new RegExp( pattern );
} ).join( "" ) + "$";
rgx = this.regex[ binding ] = new RegExp( pattern );
}
this.cache[topic] = this.cache[topic] || {};
this.cache[topic][binding] = result = rgx.test( topic );
this.cache[ topic ] = this.cache[ topic ] || {};
this.cache[ topic ][ binding ] = result = rgx.test( topic );
return result;
},
@ -247,12 +253,13 @@
this.regex = {};
}
};
/* global postal */
var fireSub = function ( subDef, envelope ) {
if ( !subDef.inactive && 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' ) {
if ( typeof subDef.callback === "function" ) {
subDef.callback.call( subDef.context, envelope.data, envelope );
}
}
@ -315,7 +322,7 @@
},
subscribe : function ( subDef ) {
var idx, found, fn, channel = this.subscriptions[subDef.channel], subs;
var channel = this.subscriptions[subDef.channel], subs;
if ( !channel ) {
channel = this.subscriptions[subDef.channel] = {};
}
@ -349,16 +356,17 @@
}
}
};
localBus.subscriptions[SYSTEM_CHANNEL] = {};
var postal = {
/* global localBus, bindingsResolver, ChannelDefinition, SubscriptionDefinition, postal */
/*jshint -W020 */
postal = {
configuration : {
bus : localBus,
resolver : bindingsResolver,
DEFAULT_CHANNEL : DEFAULT_CHANNEL,
SYSTEM_CHANNEL : SYSTEM_CHANNEL
bus : localBus,
resolver : bindingsResolver,
DEFAULT_CHANNEL : "/",
SYSTEM_CHANNEL : "postal"
},
ChannelDefinition : ChannelDefinition,
ChannelDefinition : ChannelDefinition,
SubscriptionDefinition : SubscriptionDefinition,
channel : function ( channelName ) {
@ -366,11 +374,11 @@
},
subscribe : function ( options ) {
return new SubscriptionDefinition( options.channel || DEFAULT_CHANNEL, options.topic, options.callback );
return new SubscriptionDefinition( options.channel || postal.configuration.DEFAULT_CHANNEL, options.topic, options.callback );
},
publish : function ( envelope ) {
envelope.channel = envelope.channel || DEFAULT_CHANNEL;
envelope.channel = envelope.channel || postal.configuration.DEFAULT_CHANNEL;
return postal.configuration.bus.publish( envelope );
},
@ -379,17 +387,17 @@
},
linkChannels : function ( sources, destinations ) {
var result = [];
sources = !_.isArray( sources ) ? [sources] : sources;
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;
var destChannel = destination.channel || postal.configuration.DEFAULT_CHANNEL;
result.push(
postal.subscribe( {
channel : source.channel || DEFAULT_CHANNEL,
topic : source.topic || "#",
channel : source.channel || postal.configuration.DEFAULT_CHANNEL,
topic : sourceTopic,
callback : function ( data, env ) {
var newEnv = _.clone( env );
newEnv.topic = _.isFunction( destination.topic ) ? destination.topic( env.topic ) : destination.topic || env.topic;
@ -413,7 +421,7 @@
tpc = arguments[ 0 ].topic;
}
if ( postal.configuration.bus.subscriptions[ channel ] &&
Object.prototype.hasOwnProperty.call( postal.configuration.bus.subscriptions[ channel ], tpc )) {
Object.prototype.hasOwnProperty.call( postal.configuration.bus.subscriptions[ channel ], tpc ) ) {
return postal.configuration.bus.subscriptions[ channel ][ tpc ];
}
return [];
@ -425,6 +433,15 @@
}
}
};
localBus.subscriptions[postal.configuration.SYSTEM_CHANNEL] = {};
/*jshint -W106 */
if ( global.hasOwnProperty( "__postalReady__" ) && _.isArray( global.__postalReady__ ) ) {
while(global.__postalReady__.length) {
global.__postalReady__.shift().onReady(postal);
}
}
/*jshint +W106 */
return postal;
} ));

File diff suppressed because one or more lines are too long

View file

@ -4,13 +4,14 @@
License: Dual licensed MIT (http://www.opensource.org/licenses/mit-license) & GPL (http://www.opensource.org/licenses/gpl-license)
Version 0.8.7
*/
/*jshint -W098 */
(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 ( _ ) {
@ -22,9 +23,9 @@
}
}( this, function ( _, global, undefined ) {
var DEFAULT_CHANNEL = "/",
DEFAULT_DISPOSEAFTER = 0,
SYSTEM_CHANNEL = "postal";
var postal;
/*jshint -W098 */
var ConsecutiveDistinctPredicate = function () {
var previous;
return function ( data ) {
@ -40,6 +41,7 @@
return !eq;
};
};
/*jshint -W098 */
var DistinctPredicate = function () {
var previous = [];
@ -56,8 +58,9 @@
return isDistinct;
};
};
/* global postal, SubscriptionDefinition */
var ChannelDefinition = function ( channelName ) {
this.channel = channelName || DEFAULT_CHANNEL;
this.channel = channelName || postal.configuration.DEFAULT_CHANNEL;
};
ChannelDefinition.prototype.subscribe = function () {
@ -68,13 +71,15 @@
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] };
( 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 );
};
/* global postal */
/*jshint -W117 */
var SubscriptionDefinition = function ( channel, topic, callback ) {
this.channel = channel;
this.topic = topic;
@ -82,12 +87,12 @@
this.constraints = [];
this.context = null;
postal.configuration.bus.publish( {
channel : SYSTEM_CHANNEL,
topic : "subscription.created",
data : {
event : "subscription.created",
channel : postal.configuration.SYSTEM_CHANNEL,
topic : "subscription.created",
data : {
event : "subscription.created",
channel : channel,
topic : topic
topic : topic
}
} );
postal.configuration.bus.subscribe( this );
@ -95,16 +100,16 @@
SubscriptionDefinition.prototype = {
unsubscribe : function () {
if(!this.inactive) {
if ( !this.inactive ) {
this.inactive = true;
postal.configuration.bus.unsubscribe( this );
postal.configuration.bus.publish( {
channel : SYSTEM_CHANNEL,
topic : "subscription.removed",
data : {
event : "subscription.removed",
channel : postal.configuration.SYSTEM_CHANNEL,
topic : "subscription.removed",
data : {
event : "subscription.removed",
channel : this.channel,
topic : this.topic
topic : this.topic
}
} );
}
@ -210,35 +215,36 @@
return this;
}
};
/*jshint -W098 */
var bindingsResolver = {
cache : {},
regex : {},
compare : function ( binding, topic ) {
var pattern, rgx, prevSegment, result = (this.cache[topic] && this.cache[topic][binding]);
if(typeof result !== "undefined") {
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) {
if ( !( rgx = this.regex[ binding ] )) {
pattern = "^" + _.map( binding.split( "." ),function ( segment ) {
var res = "";
if (!!prevSegment) {
if ( !!prevSegment ) {
res = prevSegment !== "#" ? "\\.\\b" : "\\b";
}
if(segment === "#") {
res += "[\\s\\S]*"
} else if (segment === "*") {
res += "[^.]+"
if ( segment === "#" ) {
res += "[\\s\\S]*";
} else if ( segment === "*" ) {
res += "[^.]+";
} else {
res += segment;
}
prevSegment = segment;
return res;
} ).join('') + "$";
rgx = this.regex[binding] = new RegExp( pattern );
} ).join( "" ) + "$";
rgx = this.regex[ binding ] = new RegExp( pattern );
}
this.cache[topic] = this.cache[topic] || {};
this.cache[topic][binding] = result = rgx.test( topic );
this.cache[ topic ] = this.cache[ topic ] || {};
this.cache[ topic ][ binding ] = result = rgx.test( topic );
return result;
},
@ -247,12 +253,13 @@
this.regex = {};
}
};
/* global postal */
var fireSub = function ( subDef, envelope ) {
if ( !subDef.inactive && 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' ) {
if ( typeof subDef.callback === "function" ) {
subDef.callback.call( subDef.context, envelope.data, envelope );
}
}
@ -315,7 +322,7 @@
},
subscribe : function ( subDef ) {
var idx, found, fn, channel = this.subscriptions[subDef.channel], subs;
var channel = this.subscriptions[subDef.channel], subs;
if ( !channel ) {
channel = this.subscriptions[subDef.channel] = {};
}
@ -349,16 +356,17 @@
}
}
};
localBus.subscriptions[SYSTEM_CHANNEL] = {};
var postal = {
/* global localBus, bindingsResolver, ChannelDefinition, SubscriptionDefinition, postal */
/*jshint -W020 */
postal = {
configuration : {
bus : localBus,
resolver : bindingsResolver,
DEFAULT_CHANNEL : DEFAULT_CHANNEL,
SYSTEM_CHANNEL : SYSTEM_CHANNEL
bus : localBus,
resolver : bindingsResolver,
DEFAULT_CHANNEL : "/",
SYSTEM_CHANNEL : "postal"
},
ChannelDefinition : ChannelDefinition,
ChannelDefinition : ChannelDefinition,
SubscriptionDefinition : SubscriptionDefinition,
channel : function ( channelName ) {
@ -366,11 +374,11 @@
},
subscribe : function ( options ) {
return new SubscriptionDefinition( options.channel || DEFAULT_CHANNEL, options.topic, options.callback );
return new SubscriptionDefinition( options.channel || postal.configuration.DEFAULT_CHANNEL, options.topic, options.callback );
},
publish : function ( envelope ) {
envelope.channel = envelope.channel || DEFAULT_CHANNEL;
envelope.channel = envelope.channel || postal.configuration.DEFAULT_CHANNEL;
return postal.configuration.bus.publish( envelope );
},
@ -379,17 +387,17 @@
},
linkChannels : function ( sources, destinations ) {
var result = [];
sources = !_.isArray( sources ) ? [sources] : sources;
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;
var destChannel = destination.channel || postal.configuration.DEFAULT_CHANNEL;
result.push(
postal.subscribe( {
channel : source.channel || DEFAULT_CHANNEL,
topic : source.topic || "#",
channel : source.channel || postal.configuration.DEFAULT_CHANNEL,
topic : sourceTopic,
callback : function ( data, env ) {
var newEnv = _.clone( env );
newEnv.topic = _.isFunction( destination.topic ) ? destination.topic( env.topic ) : destination.topic || env.topic;
@ -413,7 +421,7 @@
tpc = arguments[ 0 ].topic;
}
if ( postal.configuration.bus.subscriptions[ channel ] &&
Object.prototype.hasOwnProperty.call( postal.configuration.bus.subscriptions[ channel ], tpc )) {
Object.prototype.hasOwnProperty.call( postal.configuration.bus.subscriptions[ channel ], tpc ) ) {
return postal.configuration.bus.subscriptions[ channel ][ tpc ];
}
return [];
@ -425,6 +433,15 @@
}
}
};
localBus.subscriptions[postal.configuration.SYSTEM_CHANNEL] = {};
/*jshint -W106 */
if ( global.hasOwnProperty( "__postalReady__" ) && _.isArray( global.__postalReady__ ) ) {
while(global.__postalReady__.length) {
global.__postalReady__.shift().onReady(postal);
}
}
/*jshint +W106 */
return postal;
} ));

File diff suppressed because one or more lines are too long

View file

@ -438,7 +438,7 @@
/*jshint -W106 */
if ( global.hasOwnProperty( "__postalReady__" ) && _.isArray( global.__postalReady__ ) ) {
while(global.__postalReady__.length) {
global.__postalReady__.shift(postal);
global.__postalReady__.shift().onReady(postal);
}
}
/*jshint +W106 */

2
lib/postal.min.js vendored

File diff suppressed because one or more lines are too long

View file

@ -30,7 +30,7 @@
/*jshint -W106 */
if ( global.hasOwnProperty( "__postalReady__" ) && _.isArray( global.__postalReady__ ) ) {
while(global.__postalReady__.length) {
global.__postalReady__.shift(postal);
global.__postalReady__.shift().onReady(postal);
}
}
/*jshint +W106 */