mirror of
https://github.com/Hopiu/postal.js.git
synced 2026-03-16 22:20:23 +00:00
Updated examples
This commit is contained in:
parent
2cd58733d8
commit
b0461a54b4
9 changed files with 134 additions and 274 deletions
|
|
@ -11,7 +11,7 @@
|
|||
"output": {
|
||||
"full": "lib",
|
||||
"partial": {
|
||||
"lib/postal.*" : [ "./example/standard/js", "./example/amd/js/libs/postal" ]
|
||||
"**/lib/postal.*" : [ "example/standard/js", "example/amd/js/libs/postal" ]
|
||||
}
|
||||
},
|
||||
"dependencies": [ "anvil.http", "anvil.uglify", "anvil.jshint" ]
|
||||
|
|
|
|||
|
|
@ -2,27 +2,29 @@
|
|||
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.7.3
|
||||
Version 0.8.0
|
||||
*/
|
||||
(function ( root, factory ) {
|
||||
if ( typeof define === "function" && define.amd ) {
|
||||
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
|
||||
factory( root._, root );
|
||||
root.postal = factory( root._, root );
|
||||
}
|
||||
}( this, function ( _, global, undefined ) {
|
||||
|
||||
var DEFAULT_CHANNEL = "/",
|
||||
DEFAULT_PRIORITY = 50,
|
||||
DEFAULT_DISPOSEAFTER = 0,
|
||||
SYSTEM_CHANNEL = "postal",
|
||||
NO_OP = function () {
|
||||
};
|
||||
|
||||
SYSTEM_CHANNEL = "postal";
|
||||
var ConsecutiveDistinctPredicate = function () {
|
||||
var previous;
|
||||
return function ( data ) {
|
||||
|
|
@ -39,62 +41,54 @@
|
|||
};
|
||||
};
|
||||
var DistinctPredicate = function () {
|
||||
var previous = [];
|
||||
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;
|
||||
};
|
||||
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] );
|
||||
},
|
||||
|
||||
publish : function () {
|
||||
var envelope = arguments.length === 1 ? arguments[0] : { topic: arguments[0], data: arguments[1] };
|
||||
envelope.channel = this.channel;
|
||||
postal.configuration.bus.publish( envelope );
|
||||
return envelope;
|
||||
}
|
||||
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]' ?
|
||||
arguments[0] : { topic: 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.priority = DEFAULT_PRIORITY;
|
||||
this.constraints = new Array( 0 );
|
||||
this.maxCalls = DEFAULT_DISPOSEAFTER;
|
||||
this.onHandled = NO_OP;
|
||||
this.constraints = [];
|
||||
this.context = null;
|
||||
postal.configuration.bus.publish( {
|
||||
channel : SYSTEM_CHANNEL,
|
||||
topic : "subscription.created",
|
||||
timeStamp : new Date(),
|
||||
data : {
|
||||
event : "subscription.created",
|
||||
channel : channel,
|
||||
topic : topic
|
||||
}
|
||||
} );
|
||||
|
||||
postal.configuration.bus.subscribe( this );
|
||||
|
||||
};
|
||||
|
||||
SubscriptionDefinition.prototype = {
|
||||
|
|
@ -103,7 +97,6 @@
|
|||
postal.configuration.bus.publish( {
|
||||
channel : SYSTEM_CHANNEL,
|
||||
topic : "subscription.removed",
|
||||
timeStamp : new Date(),
|
||||
data : {
|
||||
event : "subscription.removed",
|
||||
channel : this.channel,
|
||||
|
|
@ -124,20 +117,19 @@
|
|||
if ( _.isNaN( maxCalls ) || maxCalls <= 0 ) {
|
||||
throw "The value provided to disposeAfter (maxCalls) must be a number greater than zero.";
|
||||
}
|
||||
|
||||
var fn = this.onHandled;
|
||||
var fn = this.callback;
|
||||
var dispose = _.after( maxCalls, _.bind( function () {
|
||||
this.unsubscribe( this );
|
||||
this.unsubscribe();
|
||||
}, this ) );
|
||||
|
||||
this.onHandled = function () {
|
||||
this.callback = function () {
|
||||
fn.apply( this.context, arguments );
|
||||
dispose();
|
||||
};
|
||||
return this;
|
||||
},
|
||||
|
||||
distinctUntilChanged : function () {
|
||||
distinctUntilChanged : function () {
|
||||
this.withConstraint( new ConsecutiveDistinctPredicate() );
|
||||
return this;
|
||||
},
|
||||
|
|
@ -147,9 +139,9 @@
|
|||
return this;
|
||||
},
|
||||
|
||||
once: function() {
|
||||
this.disposeAfter(1);
|
||||
},
|
||||
once : function () {
|
||||
this.disposeAfter( 1 );
|
||||
},
|
||||
|
||||
withConstraint : function ( predicate ) {
|
||||
if ( !_.isFunction( predicate ) ) {
|
||||
|
|
@ -196,15 +188,6 @@
|
|||
return this;
|
||||
},
|
||||
|
||||
withPriority : function ( priority ) {
|
||||
if ( _.isNaN( priority ) ) {
|
||||
throw "Priority must be a number";
|
||||
}
|
||||
this.priority = priority;
|
||||
postal.configuration.bus.changePriority( this );
|
||||
return this;
|
||||
},
|
||||
|
||||
withThrottle : function ( milliseconds ) {
|
||||
if ( _.isNaN( milliseconds ) ) {
|
||||
throw "Milliseconds must be a number";
|
||||
|
|
@ -219,7 +202,6 @@
|
|||
return this;
|
||||
}
|
||||
};
|
||||
|
||||
var bindingsResolver = {
|
||||
cache : { },
|
||||
|
||||
|
|
@ -228,10 +210,10 @@
|
|||
return true;
|
||||
}
|
||||
var pattern = ("^" + binding.replace( /\./g, "\\." ) // escape actual periods
|
||||
.replace( /\*/g, "[A-Z,a-z,0-9]*" ) // asterisks match any alpha-numeric 'word'
|
||||
.replace( /#/g, ".*" ) + "$") // hash matches 'n' # of words (+ optional on start/end of topic)
|
||||
.replace( "\\..*$", "(\\..*)*$" ) // fix end of topic matching on hash wildcards
|
||||
.replace( "^.*\\.", "^(.*\\.)*" ); // fix beginning of topic matching on hash wildcards
|
||||
.replace( /\*/g, "[A-Z,a-z,0-9]*" ) // asterisks match any alpha-numeric 'word'
|
||||
.replace( /#/g, ".*" ) + "$") // hash matches 'n' # of words (+ optional on start/end of topic)
|
||||
.replace( "\\..*$", "(\\..*)*$" ) // fix end of topic matching on hash wildcards
|
||||
.replace( "^.*\\.", "^(.*\\.)*" ); // fix beginning of topic matching on hash wildcards
|
||||
var rgx = new RegExp( pattern );
|
||||
var result = rgx.test( topic );
|
||||
if ( result ) {
|
||||
|
|
@ -247,9 +229,7 @@
|
|||
this.cache = {};
|
||||
}
|
||||
};
|
||||
|
||||
var localBus = {
|
||||
|
||||
addWireTap : function ( callback ) {
|
||||
var self = this;
|
||||
self.wireTaps.push( callback );
|
||||
|
|
@ -261,48 +241,28 @@
|
|||
};
|
||||
},
|
||||
|
||||
changePriority : function ( subDef ) {
|
||||
var idx, found;
|
||||
if ( this.subscriptions[subDef.channel] && this.subscriptions[subDef.channel][subDef.topic] ) {
|
||||
this.subscriptions[subDef.channel][subDef.topic] = _.without( this.subscriptions[subDef.channel][subDef.topic], subDef );
|
||||
idx = this.subscriptions[subDef.channel][subDef.topic].length - 1;
|
||||
for ( ; idx >= 0; idx-- ) {
|
||||
if ( this.subscriptions[subDef.channel][subDef.topic][idx].priority <= subDef.priority ) {
|
||||
this.subscriptions[subDef.channel][subDef.topic].splice( idx + 1, 0, subDef );
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ( !found ) {
|
||||
this.subscriptions[subDef.channel][subDef.topic].unshift( subDef );
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
publish : function ( envelope ) {
|
||||
envelope.timeStamp = new Date();
|
||||
envelope.timeStamp = new Date();
|
||||
_.each( this.wireTaps, function ( tap ) {
|
||||
tap( envelope.data, envelope );
|
||||
} );
|
||||
|
||||
if ( this.subscriptions[envelope.channel] ) {
|
||||
_.each( this.subscriptions[envelope.channel], function ( topic ) {
|
||||
// TODO: research faster ways to handle this than _.clone
|
||||
_.each( _.clone( topic ), function ( subDef ) {
|
||||
if ( postal.configuration.resolver.compare( subDef.topic, envelope.topic ) ) {
|
||||
if ( _.all( subDef.constraints, function ( constraint ) {
|
||||
return constraint.call(subDef.context, envelope.data, envelope );
|
||||
return constraint.call( subDef.context, envelope.data, envelope );
|
||||
} ) ) {
|
||||
if ( typeof subDef.callback === 'function' ) {
|
||||
subDef.callback.call( subDef.context, envelope.data, envelope );
|
||||
subDef.onHandled();
|
||||
}
|
||||
}
|
||||
}
|
||||
} );
|
||||
} );
|
||||
}
|
||||
|
||||
return envelope;
|
||||
},
|
||||
|
||||
reset : function () {
|
||||
|
|
@ -320,13 +280,12 @@
|
|||
|
||||
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] = new Array( 0 );
|
||||
subs = this.subscriptions[subDef.channel][subDef.topic] = [];
|
||||
}
|
||||
subs.push( subDef );
|
||||
return subDef;
|
||||
|
|
@ -334,7 +293,7 @@
|
|||
|
||||
subscriptions : {},
|
||||
|
||||
wireTaps : new Array( 0 ),
|
||||
wireTaps : [],
|
||||
|
||||
unsubscribe : function ( config ) {
|
||||
if ( this.subscriptions[config.channel][config.topic] ) {
|
||||
|
|
@ -349,28 +308,20 @@
|
|||
}
|
||||
}
|
||||
};
|
||||
|
||||
var sessionInfo = {};
|
||||
|
||||
// save some setup time, albeit tiny
|
||||
localBus.subscriptions[SYSTEM_CHANNEL] = {};
|
||||
|
||||
var postal = {
|
||||
configuration : {
|
||||
bus : localBus,
|
||||
resolver : bindingsResolver,
|
||||
DEFAULT_CHANNEL : DEFAULT_CHANNEL,
|
||||
DEFAULT_PRIORITY : DEFAULT_PRIORITY,
|
||||
DEFAULT_DISPOSEAFTER : DEFAULT_DISPOSEAFTER,
|
||||
SYSTEM_CHANNEL : SYSTEM_CHANNEL
|
||||
},
|
||||
|
||||
ChannelDefinition : ChannelDefinition,
|
||||
|
||||
SubscriptionDefinition: SubscriptionDefinition,
|
||||
ChannelDefinition : ChannelDefinition,
|
||||
SubscriptionDefinition : SubscriptionDefinition,
|
||||
|
||||
channel : function ( channelName ) {
|
||||
return new ChannelDefinition( channelName );
|
||||
return new ChannelDefinition( channelName );
|
||||
},
|
||||
|
||||
subscribe : function ( options ) {
|
||||
|
|
@ -378,9 +329,8 @@
|
|||
},
|
||||
|
||||
publish : function ( envelope ) {
|
||||
envelope.channel = envelope.channel || DEFAULT_CHANNEL;
|
||||
postal.configuration.bus.publish( envelope );
|
||||
return envelope;
|
||||
envelope.channel = envelope.channel || DEFAULT_CHANNEL;
|
||||
return postal.configuration.bus.publish( envelope );
|
||||
},
|
||||
|
||||
addWireTap : function ( callback ) {
|
||||
|
|
@ -388,13 +338,9 @@
|
|||
},
|
||||
|
||||
linkChannels : function ( sources, destinations ) {
|
||||
var result = [];
|
||||
if ( !_.isArray( sources ) ) {
|
||||
sources = [sources];
|
||||
}
|
||||
if ( !_.isArray( destinations ) ) {
|
||||
destinations = [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 ) {
|
||||
|
|
@ -420,23 +366,16 @@
|
|||
utils : {
|
||||
getSubscribersFor : function () {
|
||||
var channel = arguments[ 0 ],
|
||||
tpc = arguments[ 1 ],
|
||||
result = [];
|
||||
tpc = arguments[ 1 ];
|
||||
if ( arguments.length === 1 ) {
|
||||
if ( Object.prototype.toString.call( channel ) === "[object String]" ) {
|
||||
channel = postal.configuration.DEFAULT_CHANNEL;
|
||||
tpc = arguments[ 0 ];
|
||||
}
|
||||
else {
|
||||
channel = arguments[ 0 ].channel || postal.configuration.DEFAULT_CHANNEL;
|
||||
tpc = arguments[ 0 ].topic;
|
||||
}
|
||||
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 ) ) {
|
||||
result = postal.configuration.bus.subscriptions[ channel ][ tpc ];
|
||||
postal.configuration.bus.subscriptions[ channel ].hasOwnProperty( tpc ) ) {
|
||||
return postal.configuration.bus.subscriptions[ channel ][ tpc ];
|
||||
}
|
||||
return result;
|
||||
return [];
|
||||
},
|
||||
|
||||
reset : function () {
|
||||
|
|
@ -446,6 +385,5 @@
|
|||
}
|
||||
};
|
||||
|
||||
global.postal = postal;
|
||||
return postal;
|
||||
} ));
|
||||
7
example/amd/js/libs/postal/postal.min.js
vendored
7
example/amd/js/libs/postal/postal.min.js
vendored
File diff suppressed because one or more lines are too long
|
|
@ -6,6 +6,5 @@ div {
|
|||
.results {
|
||||
margin-bottom: 20px;
|
||||
padding: 10px;
|
||||
border-top: 1pt solid lightsteelblue;
|
||||
border-bottom: 1pt solid lightsteelblue;
|
||||
}
|
||||
|
|
@ -2,27 +2,29 @@
|
|||
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.7.3
|
||||
Version 0.8.0
|
||||
*/
|
||||
(function ( root, factory ) {
|
||||
if ( typeof define === "function" && define.amd ) {
|
||||
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 ( _ ) {
|
||||
define( ["."], function ( _ ) {
|
||||
return factory( _, root );
|
||||
} );
|
||||
} else {
|
||||
// Browser globals
|
||||
factory( root._, root );
|
||||
root.postal = factory( root._, root );
|
||||
}
|
||||
}( this, function ( _, global, undefined ) {
|
||||
|
||||
var DEFAULT_CHANNEL = "/",
|
||||
DEFAULT_PRIORITY = 50,
|
||||
DEFAULT_DISPOSEAFTER = 0,
|
||||
SYSTEM_CHANNEL = "postal",
|
||||
NO_OP = function () {
|
||||
};
|
||||
|
||||
SYSTEM_CHANNEL = "postal";
|
||||
var ConsecutiveDistinctPredicate = function () {
|
||||
var previous;
|
||||
return function ( data ) {
|
||||
|
|
@ -39,62 +41,54 @@
|
|||
};
|
||||
};
|
||||
var DistinctPredicate = function () {
|
||||
var previous = [];
|
||||
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;
|
||||
};
|
||||
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] );
|
||||
},
|
||||
|
||||
publish : function () {
|
||||
var envelope = arguments.length === 1 ? arguments[0] : { topic: arguments[0], data: arguments[1] };
|
||||
envelope.channel = this.channel;
|
||||
postal.configuration.bus.publish( envelope );
|
||||
return envelope;
|
||||
}
|
||||
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]' ?
|
||||
arguments[0] : { topic: 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.priority = DEFAULT_PRIORITY;
|
||||
this.constraints = new Array( 0 );
|
||||
this.maxCalls = DEFAULT_DISPOSEAFTER;
|
||||
this.onHandled = NO_OP;
|
||||
this.constraints = [];
|
||||
this.context = null;
|
||||
postal.configuration.bus.publish( {
|
||||
channel : SYSTEM_CHANNEL,
|
||||
topic : "subscription.created",
|
||||
timeStamp : new Date(),
|
||||
data : {
|
||||
event : "subscription.created",
|
||||
channel : channel,
|
||||
topic : topic
|
||||
}
|
||||
} );
|
||||
|
||||
postal.configuration.bus.subscribe( this );
|
||||
|
||||
};
|
||||
|
||||
SubscriptionDefinition.prototype = {
|
||||
|
|
@ -103,7 +97,6 @@
|
|||
postal.configuration.bus.publish( {
|
||||
channel : SYSTEM_CHANNEL,
|
||||
topic : "subscription.removed",
|
||||
timeStamp : new Date(),
|
||||
data : {
|
||||
event : "subscription.removed",
|
||||
channel : this.channel,
|
||||
|
|
@ -124,20 +117,19 @@
|
|||
if ( _.isNaN( maxCalls ) || maxCalls <= 0 ) {
|
||||
throw "The value provided to disposeAfter (maxCalls) must be a number greater than zero.";
|
||||
}
|
||||
|
||||
var fn = this.onHandled;
|
||||
var fn = this.callback;
|
||||
var dispose = _.after( maxCalls, _.bind( function () {
|
||||
this.unsubscribe( this );
|
||||
this.unsubscribe();
|
||||
}, this ) );
|
||||
|
||||
this.onHandled = function () {
|
||||
this.callback = function () {
|
||||
fn.apply( this.context, arguments );
|
||||
dispose();
|
||||
};
|
||||
return this;
|
||||
},
|
||||
|
||||
distinctUntilChanged : function () {
|
||||
distinctUntilChanged : function () {
|
||||
this.withConstraint( new ConsecutiveDistinctPredicate() );
|
||||
return this;
|
||||
},
|
||||
|
|
@ -147,9 +139,9 @@
|
|||
return this;
|
||||
},
|
||||
|
||||
once: function() {
|
||||
this.disposeAfter(1);
|
||||
},
|
||||
once : function () {
|
||||
this.disposeAfter( 1 );
|
||||
},
|
||||
|
||||
withConstraint : function ( predicate ) {
|
||||
if ( !_.isFunction( predicate ) ) {
|
||||
|
|
@ -196,15 +188,6 @@
|
|||
return this;
|
||||
},
|
||||
|
||||
withPriority : function ( priority ) {
|
||||
if ( _.isNaN( priority ) ) {
|
||||
throw "Priority must be a number";
|
||||
}
|
||||
this.priority = priority;
|
||||
postal.configuration.bus.changePriority( this );
|
||||
return this;
|
||||
},
|
||||
|
||||
withThrottle : function ( milliseconds ) {
|
||||
if ( _.isNaN( milliseconds ) ) {
|
||||
throw "Milliseconds must be a number";
|
||||
|
|
@ -219,7 +202,6 @@
|
|||
return this;
|
||||
}
|
||||
};
|
||||
|
||||
var bindingsResolver = {
|
||||
cache : { },
|
||||
|
||||
|
|
@ -228,10 +210,10 @@
|
|||
return true;
|
||||
}
|
||||
var pattern = ("^" + binding.replace( /\./g, "\\." ) // escape actual periods
|
||||
.replace( /\*/g, "[A-Z,a-z,0-9]*" ) // asterisks match any alpha-numeric 'word'
|
||||
.replace( /#/g, ".*" ) + "$") // hash matches 'n' # of words (+ optional on start/end of topic)
|
||||
.replace( "\\..*$", "(\\..*)*$" ) // fix end of topic matching on hash wildcards
|
||||
.replace( "^.*\\.", "^(.*\\.)*" ); // fix beginning of topic matching on hash wildcards
|
||||
.replace( /\*/g, "[A-Z,a-z,0-9]*" ) // asterisks match any alpha-numeric 'word'
|
||||
.replace( /#/g, ".*" ) + "$") // hash matches 'n' # of words (+ optional on start/end of topic)
|
||||
.replace( "\\..*$", "(\\..*)*$" ) // fix end of topic matching on hash wildcards
|
||||
.replace( "^.*\\.", "^(.*\\.)*" ); // fix beginning of topic matching on hash wildcards
|
||||
var rgx = new RegExp( pattern );
|
||||
var result = rgx.test( topic );
|
||||
if ( result ) {
|
||||
|
|
@ -247,9 +229,7 @@
|
|||
this.cache = {};
|
||||
}
|
||||
};
|
||||
|
||||
var localBus = {
|
||||
|
||||
addWireTap : function ( callback ) {
|
||||
var self = this;
|
||||
self.wireTaps.push( callback );
|
||||
|
|
@ -261,48 +241,28 @@
|
|||
};
|
||||
},
|
||||
|
||||
changePriority : function ( subDef ) {
|
||||
var idx, found;
|
||||
if ( this.subscriptions[subDef.channel] && this.subscriptions[subDef.channel][subDef.topic] ) {
|
||||
this.subscriptions[subDef.channel][subDef.topic] = _.without( this.subscriptions[subDef.channel][subDef.topic], subDef );
|
||||
idx = this.subscriptions[subDef.channel][subDef.topic].length - 1;
|
||||
for ( ; idx >= 0; idx-- ) {
|
||||
if ( this.subscriptions[subDef.channel][subDef.topic][idx].priority <= subDef.priority ) {
|
||||
this.subscriptions[subDef.channel][subDef.topic].splice( idx + 1, 0, subDef );
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ( !found ) {
|
||||
this.subscriptions[subDef.channel][subDef.topic].unshift( subDef );
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
publish : function ( envelope ) {
|
||||
envelope.timeStamp = new Date();
|
||||
envelope.timeStamp = new Date();
|
||||
_.each( this.wireTaps, function ( tap ) {
|
||||
tap( envelope.data, envelope );
|
||||
} );
|
||||
|
||||
if ( this.subscriptions[envelope.channel] ) {
|
||||
_.each( this.subscriptions[envelope.channel], function ( topic ) {
|
||||
// TODO: research faster ways to handle this than _.clone
|
||||
_.each( _.clone( topic ), function ( subDef ) {
|
||||
if ( postal.configuration.resolver.compare( subDef.topic, envelope.topic ) ) {
|
||||
if ( _.all( subDef.constraints, function ( constraint ) {
|
||||
return constraint.call(subDef.context, envelope.data, envelope );
|
||||
return constraint.call( subDef.context, envelope.data, envelope );
|
||||
} ) ) {
|
||||
if ( typeof subDef.callback === 'function' ) {
|
||||
subDef.callback.call( subDef.context, envelope.data, envelope );
|
||||
subDef.onHandled();
|
||||
}
|
||||
}
|
||||
}
|
||||
} );
|
||||
} );
|
||||
}
|
||||
|
||||
return envelope;
|
||||
},
|
||||
|
||||
reset : function () {
|
||||
|
|
@ -320,13 +280,12 @@
|
|||
|
||||
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] = new Array( 0 );
|
||||
subs = this.subscriptions[subDef.channel][subDef.topic] = [];
|
||||
}
|
||||
subs.push( subDef );
|
||||
return subDef;
|
||||
|
|
@ -334,7 +293,7 @@
|
|||
|
||||
subscriptions : {},
|
||||
|
||||
wireTaps : new Array( 0 ),
|
||||
wireTaps : [],
|
||||
|
||||
unsubscribe : function ( config ) {
|
||||
if ( this.subscriptions[config.channel][config.topic] ) {
|
||||
|
|
@ -349,28 +308,20 @@
|
|||
}
|
||||
}
|
||||
};
|
||||
|
||||
var sessionInfo = {};
|
||||
|
||||
// save some setup time, albeit tiny
|
||||
localBus.subscriptions[SYSTEM_CHANNEL] = {};
|
||||
|
||||
var postal = {
|
||||
configuration : {
|
||||
bus : localBus,
|
||||
resolver : bindingsResolver,
|
||||
DEFAULT_CHANNEL : DEFAULT_CHANNEL,
|
||||
DEFAULT_PRIORITY : DEFAULT_PRIORITY,
|
||||
DEFAULT_DISPOSEAFTER : DEFAULT_DISPOSEAFTER,
|
||||
SYSTEM_CHANNEL : SYSTEM_CHANNEL
|
||||
},
|
||||
|
||||
ChannelDefinition : ChannelDefinition,
|
||||
|
||||
SubscriptionDefinition: SubscriptionDefinition,
|
||||
ChannelDefinition : ChannelDefinition,
|
||||
SubscriptionDefinition : SubscriptionDefinition,
|
||||
|
||||
channel : function ( channelName ) {
|
||||
return new ChannelDefinition( channelName );
|
||||
return new ChannelDefinition( channelName );
|
||||
},
|
||||
|
||||
subscribe : function ( options ) {
|
||||
|
|
@ -378,9 +329,8 @@
|
|||
},
|
||||
|
||||
publish : function ( envelope ) {
|
||||
envelope.channel = envelope.channel || DEFAULT_CHANNEL;
|
||||
postal.configuration.bus.publish( envelope );
|
||||
return envelope;
|
||||
envelope.channel = envelope.channel || DEFAULT_CHANNEL;
|
||||
return postal.configuration.bus.publish( envelope );
|
||||
},
|
||||
|
||||
addWireTap : function ( callback ) {
|
||||
|
|
@ -388,13 +338,9 @@
|
|||
},
|
||||
|
||||
linkChannels : function ( sources, destinations ) {
|
||||
var result = [];
|
||||
if ( !_.isArray( sources ) ) {
|
||||
sources = [sources];
|
||||
}
|
||||
if ( !_.isArray( destinations ) ) {
|
||||
destinations = [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 ) {
|
||||
|
|
@ -420,23 +366,16 @@
|
|||
utils : {
|
||||
getSubscribersFor : function () {
|
||||
var channel = arguments[ 0 ],
|
||||
tpc = arguments[ 1 ],
|
||||
result = [];
|
||||
tpc = arguments[ 1 ];
|
||||
if ( arguments.length === 1 ) {
|
||||
if ( Object.prototype.toString.call( channel ) === "[object String]" ) {
|
||||
channel = postal.configuration.DEFAULT_CHANNEL;
|
||||
tpc = arguments[ 0 ];
|
||||
}
|
||||
else {
|
||||
channel = arguments[ 0 ].channel || postal.configuration.DEFAULT_CHANNEL;
|
||||
tpc = arguments[ 0 ].topic;
|
||||
}
|
||||
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 ) ) {
|
||||
result = postal.configuration.bus.subscriptions[ channel ][ tpc ];
|
||||
postal.configuration.bus.subscriptions[ channel ].hasOwnProperty( tpc ) ) {
|
||||
return postal.configuration.bus.subscriptions[ channel ][ tpc ];
|
||||
}
|
||||
return result;
|
||||
return [];
|
||||
},
|
||||
|
||||
reset : function () {
|
||||
|
|
@ -446,6 +385,5 @@
|
|||
}
|
||||
};
|
||||
|
||||
global.postal = postal;
|
||||
return postal;
|
||||
} ));
|
||||
7
example/standard/js/postal.min.js
vendored
7
example/standard/js/postal.min.js
vendored
File diff suppressed because one or more lines are too long
|
|
@ -6,6 +6,5 @@ div {
|
|||
.results {
|
||||
margin-bottom: 20px;
|
||||
padding: 10px;
|
||||
border-top: 1pt solid lightsteelblue;
|
||||
border-bottom: 1pt solid lightsteelblue;
|
||||
}
|
||||
|
|
@ -13,7 +13,7 @@
|
|||
}
|
||||
} else if ( typeof define === "function" && define.amd ) {
|
||||
// AMD. Register as an anonymous module.
|
||||
define( ["."], function ( _ ) {
|
||||
define( ["underscore"], function ( _ ) {
|
||||
return factory( _, root );
|
||||
} );
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@
|
|||
}
|
||||
} else if ( typeof define === "function" && define.amd ) {
|
||||
// AMD. Register as an anonymous module.
|
||||
define( ["."], function ( _ ) {
|
||||
define( ["underscore"], function ( _ ) {
|
||||
return factory( _, root );
|
||||
} );
|
||||
} else {
|
||||
|
|
|
|||
Loading…
Reference in a new issue