mirror of
https://github.com/Hopiu/postal.js.git
synced 2026-03-16 22:20:23 +00:00
utils and configuration refactor
This commit is contained in:
parent
300115ce9a
commit
e25d9196ec
43 changed files with 1370 additions and 618 deletions
|
|
@ -1,27 +1,84 @@
|
|||
// This is the amd module version of postal.diagnostics.js
|
||||
// If you need the standard lib version, go to http://github.com/ifandelse/postal.js
|
||||
define(["postal", "underscore"], function(postal, _, undefined) {
|
||||
|
||||
define( [ "postal", "underscore" ], function ( postal, _, undefined ) {
|
||||
|
||||
var filters = [],
|
||||
applyFilter = function ( filter, env ) {
|
||||
var match = 0, possible = 0;
|
||||
_.each( filter, function ( item, key ) {
|
||||
if ( env[key] ) {
|
||||
possible++;
|
||||
if ( _.isObject( env[key] ) && !_.isArray( env[key] ) ) {
|
||||
if ( applyFilter( item, env[key] ) ) {
|
||||
match++;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ( _.isEqual( env[key], item ) ) {
|
||||
match++;
|
||||
}
|
||||
}
|
||||
}
|
||||
} );
|
||||
return match === possible;
|
||||
};
|
||||
|
||||
// this returns a callback that, if invoked, removes the wireTap
|
||||
postal.diagnostics = postal.addWireTap(function(data, envelope) {
|
||||
var all = _.extend(envelope, { data: data });
|
||||
if(!JSON) {
|
||||
throw "This browser or environment does not provide JSON support";
|
||||
}
|
||||
try {
|
||||
console.log(JSON.stringify(all));
|
||||
}
|
||||
catch(exception) {
|
||||
var wireTap = postal.addWireTap( function ( data, envelope ) {
|
||||
if ( !filters.length || _.any( filters, function ( filter ) {
|
||||
return applyFilter( filter, envelope );
|
||||
} ) ) {
|
||||
if ( !JSON ) {
|
||||
throw "This browser or environment does not provide JSON support";
|
||||
}
|
||||
try {
|
||||
all.data = "ERROR: " + exception.message;
|
||||
console.log(JSON.stringify(all));
|
||||
console.log( JSON.stringify( envelope ) );
|
||||
}
|
||||
catch(ex) {
|
||||
console.log("Unable to parse data to JSON: " + exception);
|
||||
catch ( exception ) {
|
||||
try {
|
||||
var env = _.extend( {}, envelope );
|
||||
delete env.data;
|
||||
console.log( JSON.stringify( env ) + "\n\t" + "JSON.stringify Error: " + exception.message );
|
||||
}
|
||||
catch ( ex ) {
|
||||
console.log( "Unable to parse data to JSON: " + exception );
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
} );
|
||||
|
||||
postal.diagnostics = {
|
||||
clearFilters : function () {
|
||||
filters = [];
|
||||
},
|
||||
removeFilter : function ( filter ) {
|
||||
filters = _.filter( filters, function ( item ) {
|
||||
return !_.isEqual( item, filter );
|
||||
} );
|
||||
},
|
||||
addFilter : function ( constraint ) {
|
||||
if ( !_.isArray( constraint ) ) {
|
||||
constraint = [ constraint ];
|
||||
}
|
||||
_.each( constraint, function( item ){
|
||||
if ( filters.length === 0 || !_.any( filters, function ( filter ) {
|
||||
return _.isEqual( filter, item );
|
||||
} ) ) {
|
||||
filters.push( item );
|
||||
}
|
||||
});
|
||||
|
||||
},
|
||||
getCurrentFilters : function () {
|
||||
return filters;
|
||||
},
|
||||
removeWireTap : function () {
|
||||
if ( wireTap ) {
|
||||
wireTap();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
});
|
||||
} );
|
||||
|
|
@ -1 +1 @@
|
|||
define(["postal","underscore"],function(a,b,c){a.diagnostics=a.addWireTap(function(a,c){var d=b.extend(c,{data:a});if(!JSON)throw"This browser or environment does not provide JSON support";try{console.log(JSON.stringify(d))}catch(e){try{d.data="ERROR: "+e.message,console.log(JSON.stringify(d))}catch(f){console.log("Unable to parse data to JSON: "+e)}}})})
|
||||
define(["postal","underscore"],function(a,b,c){var d=[],e=function(a,c){var d=0,f=0;return b.each(a,function(a,g){c[g]&&(f++,b.isObject(c[g])&&!b.isArray(c[g])?e(a,c[g])&&d++:b.isEqual(c[g],a)&&d++)}),d===f},f=a.addWireTap(function(a,c){if(!d.length||b.any(d,function(a){return e(a,c)})){if(!JSON)throw"This browser or environment does not provide JSON support";try{console.log(JSON.stringify(c))}catch(f){try{var g=b.extend({},c);delete g.data,console.log(JSON.stringify(g)+"\n "+"JSON.stringify Error: "+f.message)}catch(h){console.log("Unable to parse data to JSON: "+f)}}}});a.diagnostics={clearFilters:function(){d=[]},removeFilter:function(a){d=b.filter(d,function(c){return!b.isEqual(c,a)})},addFilter:function(a){b.isArray(a)||(a=[a]),b.each(a,function(a){(d.length===0||!b.any(d,function(c){return b.isEqual(c,a)}))&&d.push(a)})},getCurrentFilters:function(){return d},removeWireTap:function(){f&&f()}}})
|
||||
|
|
@ -225,14 +225,25 @@ var bindingsResolver = {
|
|||
this.cache[topic][binding] = true;
|
||||
}
|
||||
return result;
|
||||
},
|
||||
|
||||
reset: function() {
|
||||
this.cache = {};
|
||||
}
|
||||
};
|
||||
|
||||
var localBus = {
|
||||
|
||||
subscriptions: {},
|
||||
|
||||
wireTaps: new Array(0),
|
||||
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) {
|
||||
_.each(this.wireTaps,function(tap) {
|
||||
|
|
@ -253,6 +264,19 @@ var localBus = {
|
|||
});
|
||||
},
|
||||
|
||||
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;
|
||||
|
||||
|
|
@ -278,6 +302,10 @@ var localBus = {
|
|||
return subDef;
|
||||
},
|
||||
|
||||
subscriptions: {},
|
||||
|
||||
wireTaps: new Array(0),
|
||||
|
||||
unsubscribe: function(config) {
|
||||
if(this.subscriptions[config.channel][config.topic]) {
|
||||
var len = this.subscriptions[config.channel][config.topic].length,
|
||||
|
|
@ -289,17 +317,6 @@ 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);
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -351,8 +368,7 @@ var publishPicker = {
|
|||
return new postal.channelTypes[ options.type || "local" ]( channel, topic );
|
||||
}
|
||||
},
|
||||
sessionId = undefined,
|
||||
lastSessionId = undefined;
|
||||
sessionInfo = {};
|
||||
|
||||
// save some setup time, albeit tiny
|
||||
localBus.subscriptions[SYSTEM_CHANNEL] = {};
|
||||
|
|
@ -361,6 +377,13 @@ var postal = {
|
|||
configuration: {
|
||||
bus: localBus,
|
||||
resolver: bindingsResolver,
|
||||
getSessionIdAction: function( callback ) {
|
||||
callback( sessionInfo );
|
||||
},
|
||||
setSessionIdAction: function( info, callback ) {
|
||||
sessionInfo = info;
|
||||
callback( sessionInfo );
|
||||
},
|
||||
DEFAULT_CHANNEL: DEFAULT_CHANNEL,
|
||||
DEFAULT_PRIORITY: DEFAULT_PRIORITY,
|
||||
DEFAULT_DISPOSEAFTER: DEFAULT_DISPOSEAFTER,
|
||||
|
|
@ -428,19 +451,25 @@ var postal = {
|
|||
|
||||
utils: {
|
||||
getSessionId: function( callback ) {
|
||||
callback( sessionId );
|
||||
},
|
||||
|
||||
getLastSessionId: function( callback ) {
|
||||
callback( lastSessionId );
|
||||
postal.configuration.getSessionIdAction.call( this, callback );
|
||||
},
|
||||
|
||||
setSessionId: function( value, callback ) {
|
||||
lastSessionId = sessionId;
|
||||
sessionId = value;
|
||||
if( callback ) {
|
||||
callback();
|
||||
}
|
||||
postal.utils.getSessionId( function( info ) {
|
||||
// get the session info to move id to last id
|
||||
info.lastId = info.id;
|
||||
info.id = value;
|
||||
// invoke the callback the user provided to handle storing session
|
||||
postal.configuration.setSessionIdAction( info, function( session ) {
|
||||
callback( session );
|
||||
// publish postal event msg about the change
|
||||
postal.publish({
|
||||
channel: SYSTEM_CHANNEL,
|
||||
topic: "sessionId.changed",
|
||||
data: session
|
||||
});
|
||||
} );
|
||||
});
|
||||
},
|
||||
|
||||
getSubscribersFor: function() {
|
||||
|
|
@ -464,26 +493,12 @@ var postal = {
|
|||
return result;
|
||||
},
|
||||
|
||||
reset: function( callback ) {
|
||||
// we check first in case a custom bus or resolver
|
||||
// doesn't expose subscriptions or a resolver cache
|
||||
if(postal.configuration.bus.subscriptions) {
|
||||
_.each( postal.configuration.bus.subscriptions , function( channel ){
|
||||
_.each( channel, function( topic ){
|
||||
while( topic.length ) {
|
||||
topic.pop().unsubscribe();
|
||||
}
|
||||
});
|
||||
});
|
||||
postal.configuration.bus.subscriptions = {};
|
||||
}
|
||||
if(postal.configuration.resolver.cache) {
|
||||
postal.configuration.resolver.cache = {};
|
||||
}
|
||||
reset: function() {
|
||||
postal.configuration.bus.reset();
|
||||
postal.configuration.resolver.reset();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
return postal;
|
||||
});
|
||||
2
example/amd/js/libs/postal/postal.min.js
vendored
2
example/amd/js/libs/postal/postal.min.js
vendored
File diff suppressed because one or more lines are too long
|
|
@ -10,20 +10,15 @@ define([
|
|||
'views/mention-count',
|
||||
'views/mentioner-count',
|
||||
'views/hash-tag-count',
|
||||
'views/profanity-percentage'
|
||||
'views/profanity-percentage',
|
||||
'views/search-requests'
|
||||
], function( $, Backbone, bus, Router, ViewManager, ContainerView, MenuView, TweetCountView, MentionCountView,
|
||||
MentionerCountView, HashTagCountView, ProfanityPercentage ) {
|
||||
MentionerCountView, HashTagCountView, ProfanityPercentage, SearchRequestView ) {
|
||||
var app = {
|
||||
bus: bus,
|
||||
router: new Router()
|
||||
};
|
||||
|
||||
postal.configuration.getSessionIdentifier(
|
||||
function( id ) {
|
||||
console.log("SessionID: " + id);
|
||||
}
|
||||
);
|
||||
|
||||
// Set up UI concerns
|
||||
app.viewManager = new ViewManager();
|
||||
app.bus.viewManager.subscribe( "ui.show", function( data, env ) {
|
||||
|
|
@ -37,11 +32,12 @@ define([
|
|||
{ name: "mentionCount", ctor: MentionCountView },
|
||||
{ name: "mentionerCount", ctor: MentionerCountView },
|
||||
{ name: "hashTagCount", ctor: HashTagCountView },
|
||||
{ name: "profanityPercentage", ctor: ProfanityPercentage }
|
||||
{ name: "profanityPercentage", ctor: ProfanityPercentage },
|
||||
{ name: "searchRequests", ctor: SearchRequestView }
|
||||
]);
|
||||
app.viewManager.defineUIs([
|
||||
{ name: "homeUI", dependencies: [ "container", "menu", "tweetCount", "mentionCount", "mentionerCount", "hashTagCount", "profanityPercentage" ] },
|
||||
{ name: "statSelectionUI", dependencies: [ "container", "menu" ] },
|
||||
{ name: "searchRequestUI", dependencies: [ "container", "menu", "searchRequests" ] },
|
||||
{ name: "wireTapLogUI", dependencies: [ "menu" ], options: { noHide: true } }
|
||||
]);
|
||||
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
/*
|
||||
postal.socket
|
||||
Author: Jim Cowart
|
||||
License: Dual licensed MIT (http://www.opensource.org/licenses/mit-license) & GPL (http://www.opensource.org/licenses/gpl-license)
|
||||
Version 0.1.0
|
||||
*/
|
||||
/*
|
||||
postal.socket
|
||||
Author: Jim Cowart
|
||||
License: Dual licensed MIT (http://www.opensource.org/licenses/mit-license) & GPL (http://www.opensource.org/licenses/gpl-license)
|
||||
Version 0.1.0
|
||||
*/
|
||||
|
||||
(function( root, doc, factory ) {
|
||||
if ( typeof define === "function" && define.amd ) {
|
||||
|
|
@ -17,40 +17,6 @@
|
|||
}
|
||||
}(this, document, function( _, machina, postal, global, document, undefined ) {
|
||||
|
||||
var sessionId = undefined;
|
||||
|
||||
// Default implementation passes back undefined, which means
|
||||
// the socket.io socket sessionid will be used....
|
||||
postal.configuration.getSessionIdentifier = function( callback ) {
|
||||
callback( sessionId );
|
||||
};
|
||||
|
||||
postal.configuration.setSessionIdentifier = function( value ) {
|
||||
sessionId = value;
|
||||
};
|
||||
|
||||
postal.configuration.lastSessionId = null;
|
||||
|
||||
postal.getSubscribersFor = function() {
|
||||
var channel = arguments[ 0 ],
|
||||
tpc = arguments[ 1 ],
|
||||
result = [];
|
||||
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;
|
||||
}
|
||||
}
|
||||
if( postal.configuration.bus.subscriptions[ channel ] &&
|
||||
postal.configuration.bus.subscriptions[ channel ].hasOwnProperty( tpc )) {
|
||||
result = postal.configuration.bus.subscriptions[ channel ][ tpc ];
|
||||
}
|
||||
return result;
|
||||
};
|
||||
/*
|
||||
adding a socket namespace to postal
|
||||
which provides the following members:
|
||||
|
|
@ -84,6 +50,9 @@
|
|||
var socketNamespace,
|
||||
fsm = new machina.Fsm({
|
||||
retryFn: undefined,
|
||||
|
||||
session: undefined,
|
||||
|
||||
wireUpSocketEvents: function() {
|
||||
var self = this;
|
||||
_.each([ "connect", "connecting", "connect_failed", "disconnect", "reconnect", "reconnect_failed",
|
||||
|
|
@ -94,6 +63,7 @@
|
|||
});
|
||||
});
|
||||
},
|
||||
|
||||
states: {
|
||||
uninitialized: {
|
||||
tryConnect: function() {
|
||||
|
|
@ -104,7 +74,7 @@
|
|||
_onEnter: function() {
|
||||
socketNamespace = io.connect(postalSocket.config.url, { "auto connect": false });
|
||||
this.wireUpSocketEvents();
|
||||
this.transition("probing");
|
||||
this.transition("probing")
|
||||
},
|
||||
socketTransmit: function() {
|
||||
this.deferUntilTransition("online");
|
||||
|
|
@ -116,9 +86,12 @@
|
|||
if(!socketNamespace.socket.connecting && !socketNamespace.socket.reconnecting) {
|
||||
socketNamespace.socket.connect();
|
||||
}
|
||||
else {
|
||||
this.transition("settingSessionInfo");
|
||||
}
|
||||
},
|
||||
connect: function(){
|
||||
this.transition("identifying");
|
||||
this.transition("settingSessionInfo");
|
||||
},
|
||||
connect_failed: function() {
|
||||
this.transition("disconnected");
|
||||
|
|
@ -130,7 +103,7 @@
|
|||
this.deferUntilTransition("online");
|
||||
},
|
||||
reconnect: function(){
|
||||
this.transition("identifying");
|
||||
this.transition("settingSessionInfo");
|
||||
},
|
||||
reconnect_failed: function() {
|
||||
this.transition("disconnected");
|
||||
|
|
@ -139,24 +112,40 @@
|
|||
this.deferUntilTransition("online");
|
||||
}
|
||||
},
|
||||
settingSessionInfo: {
|
||||
_onEnter: function() {
|
||||
var self = this;
|
||||
postal.utils.getSessionId( function( session ) {
|
||||
if( !session || !session.id ) {
|
||||
self.handle("useFallbackSessionId" );
|
||||
} else {
|
||||
self.session = session;
|
||||
self.transition("identifying");
|
||||
}
|
||||
});
|
||||
},
|
||||
useFallbackSessionId : function () {
|
||||
var self = this;
|
||||
postal.utils.setSessionId( socketNamespace.socket.sessionid , function( session ) {
|
||||
self.session = session;
|
||||
self.transition("identifying");
|
||||
});
|
||||
}
|
||||
},
|
||||
identifying: {
|
||||
_onEnter: function() {
|
||||
var self = this;
|
||||
self.retryFn = setTimeout(function() {
|
||||
self.handle( "timeout.identifying" );
|
||||
},postalSocket.config.reconnectInterval );
|
||||
postal.configuration.getSessionIdentifier( function( id ) {
|
||||
if( !id ) {
|
||||
postal.configuration.setSessionIdentifier( socketNamespace.socket.sessionid );
|
||||
}
|
||||
self.handle( "client.identifier", { sessionId: id || socketNamespace.socket.sessionid } );
|
||||
});
|
||||
self.handle( "client.identifier" );
|
||||
},
|
||||
"client.identifier" : function( data ) {
|
||||
"client.identifier" : function() {
|
||||
clearTimeout( this.retryFn );
|
||||
var lastSessionId = postal.configuration.lastSessionId;
|
||||
postal.configuration.lastSessionId = data.sessionId;
|
||||
socketNamespace.emit( "postal.clientId", { sessionId: postal.configuration.lastSessionId, lastSessionId: lastSessionId } );
|
||||
socketNamespace.emit( "postal.clientId", { sessionId: this.session.id, lastSessionId: this.session.lastId } );
|
||||
},
|
||||
"postal.session.changed" : function() {
|
||||
socketNamespace.socket.disconnect();
|
||||
},
|
||||
connect_failed: function() {
|
||||
this.transition("disconnected");
|
||||
|
|
@ -184,7 +173,7 @@
|
|||
// we risk mutating the message here, so extend
|
||||
// and add the correlationId to the extended copy
|
||||
var socketEnv = _.extend( {}, envelope );
|
||||
socketEnv.correlationId = postal.configuration.lastSessionId;
|
||||
socketEnv.correlationId = this.session.id;
|
||||
socketNamespace.emit(evntName, socketEnv);
|
||||
}
|
||||
else {
|
||||
|
|
@ -199,6 +188,9 @@
|
|||
disconnect: function() {
|
||||
this.transition("probing");
|
||||
},
|
||||
"postal.session.changed" : function() {
|
||||
socketNamespace.socket.disconnect();
|
||||
},
|
||||
goOffline: function() {
|
||||
this.transition("offline");
|
||||
},
|
||||
|
|
@ -209,7 +201,7 @@
|
|||
// we risk mutating the message here, so extend
|
||||
// and add the correlationId to the extended copy
|
||||
var socketEnv = _.extend( {}, envelope );
|
||||
socketEnv.correlationId = postal.configuration.lastSessionId;
|
||||
socketEnv.correlationId = this.session.id;
|
||||
socketNamespace.emit(evntName, socketEnv);
|
||||
}
|
||||
},
|
||||
|
|
@ -243,6 +235,13 @@
|
|||
}
|
||||
}
|
||||
});
|
||||
postal.subscribe({
|
||||
channel: "postal",
|
||||
topic: "sessionId.changed",
|
||||
callback: function() {
|
||||
fsm.handle("postal.session.changed");
|
||||
}
|
||||
});
|
||||
return {
|
||||
config : {
|
||||
url: window.location.origin,
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ define([
|
|||
return Backbone.Router.extend({
|
||||
routes: {
|
||||
"" : "home",
|
||||
"select" : "select",
|
||||
"requests" : "requests",
|
||||
"wiretap" : "wiretap",
|
||||
"*anything" : "redirect"
|
||||
},
|
||||
|
|
@ -37,8 +37,8 @@ define([
|
|||
this.activateUI( "homeUI" );
|
||||
},
|
||||
|
||||
select: function() {
|
||||
this.activateUI( "statSelectionUI" );
|
||||
requests: function() {
|
||||
this.activateUI( "searchRequestUI" );
|
||||
},
|
||||
|
||||
wiretap: function() {
|
||||
|
|
|
|||
|
|
@ -1,27 +1,84 @@
|
|||
// This is the amd module version of postal.diagnostics.js
|
||||
// If you need the standard lib version, go to http://github.com/ifandelse/postal.js
|
||||
define(["postal", "underscore"], function(postal, _, undefined) {
|
||||
|
||||
define( [ "postal", "underscore" ], function ( postal, _, undefined ) {
|
||||
|
||||
var filters = [],
|
||||
applyFilter = function ( filter, env ) {
|
||||
var match = 0, possible = 0;
|
||||
_.each( filter, function ( item, key ) {
|
||||
if ( env[key] ) {
|
||||
possible++;
|
||||
if ( _.isObject( env[key] ) && !_.isArray( env[key] ) ) {
|
||||
if ( applyFilter( item, env[key] ) ) {
|
||||
match++;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ( _.isEqual( env[key], item ) ) {
|
||||
match++;
|
||||
}
|
||||
}
|
||||
}
|
||||
} );
|
||||
return match === possible;
|
||||
};
|
||||
|
||||
// this returns a callback that, if invoked, removes the wireTap
|
||||
postal.diagnostics = postal.addWireTap(function(data, envelope) {
|
||||
var all = _.extend(envelope, { data: data });
|
||||
if(!JSON) {
|
||||
throw "This browser or environment does not provide JSON support";
|
||||
}
|
||||
try {
|
||||
console.log(JSON.stringify(all));
|
||||
}
|
||||
catch(exception) {
|
||||
var wireTap = postal.addWireTap( function ( data, envelope ) {
|
||||
if ( !filters.length || _.any( filters, function ( filter ) {
|
||||
return applyFilter( filter, envelope );
|
||||
} ) ) {
|
||||
if ( !JSON ) {
|
||||
throw "This browser or environment does not provide JSON support";
|
||||
}
|
||||
try {
|
||||
all.data = "ERROR: " + exception.message;
|
||||
console.log(JSON.stringify(all));
|
||||
console.log( JSON.stringify( envelope ) );
|
||||
}
|
||||
catch(ex) {
|
||||
console.log("Unable to parse data to JSON: " + exception);
|
||||
catch ( exception ) {
|
||||
try {
|
||||
var env = _.extend( {}, envelope );
|
||||
delete env.data;
|
||||
console.log( JSON.stringify( env ) + "\n\t" + "JSON.stringify Error: " + exception.message );
|
||||
}
|
||||
catch ( ex ) {
|
||||
console.log( "Unable to parse data to JSON: " + exception );
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
} );
|
||||
|
||||
postal.diagnostics = {
|
||||
clearFilters : function () {
|
||||
filters = [];
|
||||
},
|
||||
removeFilter : function ( filter ) {
|
||||
filters = _.filter( filters, function ( item ) {
|
||||
return !_.isEqual( item, filter );
|
||||
} );
|
||||
},
|
||||
addFilter : function ( constraint ) {
|
||||
if ( !_.isArray( constraint ) ) {
|
||||
constraint = [ constraint ];
|
||||
}
|
||||
_.each( constraint, function( item ){
|
||||
if ( filters.length === 0 || !_.any( filters, function ( filter ) {
|
||||
return _.isEqual( filter, item );
|
||||
} ) ) {
|
||||
filters.push( item );
|
||||
}
|
||||
});
|
||||
|
||||
},
|
||||
getCurrentFilters : function () {
|
||||
return filters;
|
||||
},
|
||||
removeWireTap : function () {
|
||||
if ( wireTap ) {
|
||||
wireTap();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
});
|
||||
} );
|
||||
|
|
@ -225,14 +225,25 @@ var bindingsResolver = {
|
|||
this.cache[topic][binding] = true;
|
||||
}
|
||||
return result;
|
||||
},
|
||||
|
||||
reset: function() {
|
||||
this.cache = {};
|
||||
}
|
||||
};
|
||||
|
||||
var localBus = {
|
||||
|
||||
subscriptions: {},
|
||||
|
||||
wireTaps: new Array(0),
|
||||
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) {
|
||||
_.each(this.wireTaps,function(tap) {
|
||||
|
|
@ -253,6 +264,19 @@ var localBus = {
|
|||
});
|
||||
},
|
||||
|
||||
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;
|
||||
|
||||
|
|
@ -278,6 +302,10 @@ var localBus = {
|
|||
return subDef;
|
||||
},
|
||||
|
||||
subscriptions: {},
|
||||
|
||||
wireTaps: new Array(0),
|
||||
|
||||
unsubscribe: function(config) {
|
||||
if(this.subscriptions[config.channel][config.topic]) {
|
||||
var len = this.subscriptions[config.channel][config.topic].length,
|
||||
|
|
@ -289,17 +317,6 @@ 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);
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -351,8 +368,7 @@ var publishPicker = {
|
|||
return new postal.channelTypes[ options.type || "local" ]( channel, topic );
|
||||
}
|
||||
},
|
||||
sessionId = undefined,
|
||||
lastSessionId = undefined;
|
||||
sessionInfo = {};
|
||||
|
||||
// save some setup time, albeit tiny
|
||||
localBus.subscriptions[SYSTEM_CHANNEL] = {};
|
||||
|
|
@ -361,6 +377,13 @@ var postal = {
|
|||
configuration: {
|
||||
bus: localBus,
|
||||
resolver: bindingsResolver,
|
||||
getSessionIdAction: function( callback ) {
|
||||
callback( sessionInfo );
|
||||
},
|
||||
setSessionIdAction: function( info, callback ) {
|
||||
sessionInfo = info;
|
||||
callback( sessionInfo );
|
||||
},
|
||||
DEFAULT_CHANNEL: DEFAULT_CHANNEL,
|
||||
DEFAULT_PRIORITY: DEFAULT_PRIORITY,
|
||||
DEFAULT_DISPOSEAFTER: DEFAULT_DISPOSEAFTER,
|
||||
|
|
@ -428,19 +451,25 @@ var postal = {
|
|||
|
||||
utils: {
|
||||
getSessionId: function( callback ) {
|
||||
callback( sessionId );
|
||||
},
|
||||
|
||||
getLastSessionId: function( callback ) {
|
||||
callback( lastSessionId );
|
||||
postal.configuration.getSessionIdAction.call( this, callback );
|
||||
},
|
||||
|
||||
setSessionId: function( value, callback ) {
|
||||
lastSessionId = sessionId;
|
||||
sessionId = value;
|
||||
if( callback ) {
|
||||
callback();
|
||||
}
|
||||
postal.utils.getSessionId( function( info ) {
|
||||
// get the session info to move id to last id
|
||||
info.lastId = info.id;
|
||||
info.id = value;
|
||||
// invoke the callback the user provided to handle storing session
|
||||
postal.configuration.setSessionIdAction( info, function( session ) {
|
||||
callback( session );
|
||||
// publish postal event msg about the change
|
||||
postal.publish({
|
||||
channel: SYSTEM_CHANNEL,
|
||||
topic: "sessionId.changed",
|
||||
data: session
|
||||
});
|
||||
} );
|
||||
});
|
||||
},
|
||||
|
||||
getSubscribersFor: function() {
|
||||
|
|
@ -464,26 +493,12 @@ var postal = {
|
|||
return result;
|
||||
},
|
||||
|
||||
reset: function( callback ) {
|
||||
// we check first in case a custom bus or resolver
|
||||
// doesn't expose subscriptions or a resolver cache
|
||||
if(postal.configuration.bus.subscriptions) {
|
||||
_.each( postal.configuration.bus.subscriptions , function( channel ){
|
||||
_.each( channel, function( topic ){
|
||||
while( topic.length ) {
|
||||
topic.pop().unsubscribe();
|
||||
}
|
||||
});
|
||||
});
|
||||
postal.configuration.bus.subscriptions = {};
|
||||
}
|
||||
if(postal.configuration.resolver.cache) {
|
||||
postal.configuration.resolver.cache = {};
|
||||
}
|
||||
reset: function() {
|
||||
postal.configuration.bus.reset();
|
||||
postal.configuration.resolver.reset();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
return postal;
|
||||
});
|
||||
|
|
@ -19,12 +19,12 @@ require( [ 'backbone', 'jquery', 'underscore', 'machina', 'postal', 'lib/postal.
|
|||
// for debugging purposes ONLY for now:
|
||||
window.postal = postal;
|
||||
|
||||
postal.addWireTap( function( d, e ){
|
||||
if( e.topic === "search.info" ) {
|
||||
/*postal.addWireTap( function( d, e ){
|
||||
if( /search/.test(e.topic) ) {
|
||||
console.log( JSON.stringify( e ) );
|
||||
}
|
||||
});
|
||||
|
||||
*/
|
||||
postal.connections.socket.socketMgr.on( "*", function( evnt, data ){
|
||||
var args = [].slice.call( arguments,1 );
|
||||
if( args[0] === "postal.remote" ) {
|
||||
|
|
|
|||
|
|
@ -10,14 +10,15 @@ define( [
|
|||
sessionId: "",
|
||||
searchOwnership: "",
|
||||
searchTerm: "",
|
||||
requests: []
|
||||
requests: false
|
||||
},
|
||||
|
||||
initialize: function() {
|
||||
_.bindAll( this );
|
||||
this.subscriptions = [
|
||||
bus.app.subscribe( "search.info", this.setCurrentSearch ),
|
||||
bus.app.subscribe( "search.new.ask", this.updateRequests )
|
||||
bus.app.subscribe( "search.info", this.setCurrentSearch ).defer(),
|
||||
bus.app.subscribe( "search.new.ask", this.updateReqCount).defer(),
|
||||
bus.app.subscribe( "search.requests", this.updateReqCount ).defer()
|
||||
];
|
||||
bus.app.publish({
|
||||
topic: "get.search.info",
|
||||
|
|
@ -35,11 +36,11 @@ define( [
|
|||
setCurrentSearch: function( data, env ) {
|
||||
var self = this;
|
||||
self.set( "searchTerm", data.searchTerm );
|
||||
postal.configuration.getSessionIdentifier(
|
||||
function( id ) {
|
||||
self.set( "sessionId", id, { silent: true } );
|
||||
postal.utils.getSessionId(
|
||||
function( session ) {
|
||||
self.set( "sessionId", session.id, { silent: true } );
|
||||
self.set( "searchOwnership",
|
||||
(id === data.id)
|
||||
(session.id === data.id)
|
||||
? "You own the search."
|
||||
: "You do not own the search."
|
||||
);
|
||||
|
|
@ -47,15 +48,14 @@ define( [
|
|||
);
|
||||
},
|
||||
|
||||
updateRequests: function( data, env ) {
|
||||
var reqs = this.get( "requests" );
|
||||
if( !_.any( reqs, function( req ){
|
||||
return req.correlationId === data.correlationId &&
|
||||
req.searchTerm === data.searchTerm
|
||||
})) {
|
||||
reqs.push( data );
|
||||
this.set( "requests", _.sortBy( reqs, function( item ) { return item.searchTerm; } ) );
|
||||
updateReqCount: function( data, env ) {
|
||||
if((_.isArray(data) && data.length) || data.searchTerm) {
|
||||
this.set("requests", true);
|
||||
}
|
||||
else {
|
||||
this.set("requests", false);
|
||||
}
|
||||
this.change("requests");
|
||||
}
|
||||
});
|
||||
});
|
||||
67
example/node/client/js/models/search-requests-model.js
Normal file
67
example/node/client/js/models/search-requests-model.js
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
define( [
|
||||
'backbone',
|
||||
'bus'
|
||||
],
|
||||
function( Backbone, bus ) {
|
||||
"use strict";
|
||||
|
||||
return Backbone.Model.extend({
|
||||
defaults: {
|
||||
requests: [],
|
||||
ownerId: undefined,
|
||||
sessionId: undefined
|
||||
},
|
||||
|
||||
initialize: function() {
|
||||
_.bindAll( this );
|
||||
this.setSessionId();
|
||||
this.subscriptions = [
|
||||
bus.app.subscribe( "search.info", this.setOwner ),
|
||||
bus.app.subscribe( "search.requests", this.updateRequests ),
|
||||
bus.app.subscribe( "search.new.ask", this.addRequest )
|
||||
];
|
||||
bus.app.publish({
|
||||
topic: "get.search.requests",
|
||||
data: {}
|
||||
});
|
||||
},
|
||||
|
||||
dispose: function(){
|
||||
_.each( this.subscriptions, function( subscription ){
|
||||
subscription.unsubscribe();
|
||||
});
|
||||
this.clear( { silent: true } );
|
||||
},
|
||||
|
||||
setOwner: function( data, env ) {
|
||||
this.set("ownerId", data.id);
|
||||
this.setSessionId();
|
||||
},
|
||||
|
||||
setSessionId: function() {
|
||||
var self = this;
|
||||
postal.utils.getSessionId(
|
||||
function( session ) {
|
||||
self.set( "sessionId", session.id, { silent: true } );
|
||||
}
|
||||
);
|
||||
},
|
||||
|
||||
updateRequests: function( data, env ) {
|
||||
this.set( "requests", _.sortBy( data, function( item ) { return item.searchTerm; } ) );
|
||||
},
|
||||
|
||||
addRequest: function( data, env ) {
|
||||
var reqs = this.get("requests");
|
||||
if( _.any( reqs, function( item ){
|
||||
return item.searchTerm === data.searchTerm &&
|
||||
item.correlationId === data.correlationId
|
||||
})) {
|
||||
return;
|
||||
}
|
||||
reqs.push( data );
|
||||
this.set( "requests", _.sortBy( reqs, function( item ) { return item.searchTerm; } ), { silent: true } );
|
||||
this.change("change:requests");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
@ -45,8 +45,7 @@ define( [
|
|||
updateView: function() {
|
||||
this.$el.find( "#currentSearch" ).text( this.model.get("searchTerm") );
|
||||
this.$el.find( "#search-ownership").text( this.model.get("searchOwnership" ));
|
||||
var reqs = this.model.get("requests").length
|
||||
this.$el.find( "#request-indicator").text( reqs ? " *" : "" );
|
||||
this.$el.find( "#request-indicator").text( this.model.get("requests") ? " *" : "" );
|
||||
}
|
||||
} );
|
||||
} );
|
||||
49
example/node/client/js/views/search-requests.js
Normal file
49
example/node/client/js/views/search-requests.js
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
define( [
|
||||
'jquery',
|
||||
'backbone',
|
||||
'text!views/templates/search-requests.html',
|
||||
'models/search-requests-model',
|
||||
'bus'
|
||||
],
|
||||
function( $, Backbone, template, SearchRequestsModel, bus ) {
|
||||
"use strict";
|
||||
|
||||
return Backbone.View.extend( {
|
||||
el: "#requests",
|
||||
|
||||
events: {
|
||||
"click a.req-allow": "allowSearch"
|
||||
},
|
||||
|
||||
initialize: function() {
|
||||
_.bindAll( this );
|
||||
this.template = _.template( template );
|
||||
this.model = new SearchRequestsModel();
|
||||
this.model.bind( "change", this.render );
|
||||
},
|
||||
|
||||
render: function() {
|
||||
this.$el.html( this.template( this.model.toJSON() ) );
|
||||
},
|
||||
|
||||
show: function( data ) {
|
||||
this.$el.show();
|
||||
},
|
||||
|
||||
hide: function( data ) {
|
||||
this.$el.hide();
|
||||
},
|
||||
|
||||
allowSearch: function ( e ) {
|
||||
var idx = $( e.currentTarget).attr('href'),
|
||||
search = this.model.get("requests")[idx];
|
||||
if( search ) {
|
||||
bus.app.publish({
|
||||
topic: "search.new.confirm",
|
||||
data: search
|
||||
})
|
||||
}
|
||||
e.preventDefault();
|
||||
}
|
||||
} );
|
||||
} );
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
<div id="menu"></div>
|
||||
|
||||
<div id="info"></div>
|
||||
<div id="requests"></div>
|
||||
|
||||
<div id="stats"></div>
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
<div class="top-bar">
|
||||
<ul class="nav-menu">
|
||||
<li><a class="ps-nav" href="">Home</a></li>
|
||||
<li><a class="ps-nav" href="select">Requested Searches<span id="request-indicator"></span></a></li>
|
||||
<li><a class="ps-nav" href="requests">Requested Searches</a><span id="request-indicator"></span></li>
|
||||
<li><a class="ps-nav" href="wiretap">Show Wiretap</a></li>
|
||||
</ul>
|
||||
|
||||
|
|
|
|||
18
example/node/client/js/views/templates/search-requests.html
Normal file
18
example/node/client/js/views/templates/search-requests.html
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
<div>
|
||||
<% if( requests.length > 0 ) { %>
|
||||
<div>
|
||||
<h4>Search Requests <% if( sessionId === ownerId ) { %>(click one to give control to the requester)<% } %></h4>
|
||||
</div>
|
||||
<% _.each( requests, function ( request, idx ) { %>
|
||||
<div>(Client ID: <%= request.correlationId %>) -
|
||||
<% if( sessionId === ownerId ) { %>
|
||||
<a class="req-allow" href="<%= idx %>"><%= request.searchTerm %></a>
|
||||
<% } else { %>
|
||||
<%= request.searchTerm %>
|
||||
<% } %>
|
||||
</div>
|
||||
<% }) %>
|
||||
<% } else { %>
|
||||
<div><em>No other searches have been requested at this time.</em></div>
|
||||
<% } %>
|
||||
</div>
|
||||
|
|
@ -54,6 +54,8 @@ var TwitterSocketStats = function( port, refreshinterval ) {
|
|||
|
||||
searchAgent: new BusAdapter(new TwitterSearch( refreshinterval), searchChannel, searchChannel ),
|
||||
|
||||
requestedSearches: [],
|
||||
|
||||
stats: collectors.load( searchChannel, statsChannel ),
|
||||
|
||||
postal: postal,
|
||||
|
|
@ -75,7 +77,8 @@ var TwitterSocketStats = function( port, refreshinterval ) {
|
|||
id: correlationId,
|
||||
searchTerm: searchTerm
|
||||
};
|
||||
this.searchAgent.search(searchTerm);
|
||||
this.searchAgent.search( searchTerm );
|
||||
this.removeSearchRequest( correlationId, searchTerm );
|
||||
this.appChannel.publish({
|
||||
topic: "search.info",
|
||||
data: this.currentSearch
|
||||
|
|
@ -90,6 +93,35 @@ var TwitterSocketStats = function( port, refreshinterval ) {
|
|||
});
|
||||
},
|
||||
|
||||
getSearchRequests: function ( env ) {
|
||||
env || (env = {});
|
||||
this.appChannel.publish({
|
||||
topic: "search.requests",
|
||||
correlationId: env.correlationId,
|
||||
data: this.requestedSearches
|
||||
});
|
||||
},
|
||||
|
||||
addSearchRequest: function( correlationId, searchTerm ) {
|
||||
if( !_.any( this.searchRequests, function( item ) {
|
||||
return item.correlationId === request.correlationId &&
|
||||
item.searchTerm === request.searchTerm;
|
||||
})) {
|
||||
this.requestedSearches.push( { correlationId: correlationId, searchTerm: searchTerm } );
|
||||
}
|
||||
},
|
||||
|
||||
removeSearchRequest: function ( correlationId, searchTerm ) {
|
||||
if(_.any( this.requestedSearches, function ( item ) {
|
||||
return item.correlationId = correlationId && item.searchTerm === searchTerm;
|
||||
})) {
|
||||
this.requestedSearches = _.filter( this.requestedSearches, function ( item ){
|
||||
return item.correlationId !== correlationId && item.searchTerm !== searchTerm;
|
||||
});
|
||||
this.getSearchRequests();
|
||||
}
|
||||
},
|
||||
|
||||
states: {
|
||||
uninitialized: {
|
||||
start: function() {
|
||||
|
|
@ -113,14 +145,15 @@ var TwitterSocketStats = function( port, refreshinterval ) {
|
|||
},
|
||||
searching: {
|
||||
"search.new.request" : function( data, envelope ) {
|
||||
if(envelope.correlationId === this.currentSearch.id) {
|
||||
if( envelope.correlationId === this.currentSearch.id ) {
|
||||
this.setSearch( envelope.correlationId, data.searchTerm );
|
||||
}
|
||||
else {
|
||||
this.addSearchRequest( envelope.correlationId, data.searchTerm );
|
||||
this.appChannel.publish({
|
||||
topic: "search.new.ask",
|
||||
data: {
|
||||
correlationId: this.currentSearch.id,
|
||||
correlationId: envelope.correlationId,
|
||||
searchTerm: data.searchTerm
|
||||
}
|
||||
});
|
||||
|
|
@ -134,6 +167,9 @@ var TwitterSocketStats = function( port, refreshinterval ) {
|
|||
"get.search.info": function( data, env ) {
|
||||
this.getSearchInfo( env );
|
||||
},
|
||||
"get.search.requests": function( data, env ) {
|
||||
this.getSearchRequests( env );
|
||||
},
|
||||
"get.available" : function( data, envelope ) {
|
||||
this.getAvailableStats( envelope.correlationId );
|
||||
},
|
||||
|
|
|
|||
|
|
@ -1,25 +1,80 @@
|
|||
var _ = require('underscore');
|
||||
|
||||
module.exports = function(postal) {
|
||||
module.exports = function( _, postal ) {
|
||||
var filters = [],
|
||||
applyFilter = function ( filter, env ) {
|
||||
var match = 0, possible = 0;
|
||||
_.each( filter, function ( item, key ) {
|
||||
if ( env[key] ) {
|
||||
possible++;
|
||||
if ( _.isObject( env[key] ) && !_.isArray( env[key] ) ) {
|
||||
if ( applyFilter( item, env[key] ) ) {
|
||||
match++;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ( _.isEqual( env[key], item ) ) {
|
||||
match++;
|
||||
}
|
||||
}
|
||||
}
|
||||
} );
|
||||
return match === possible;
|
||||
};
|
||||
|
||||
// this returns a callback that, if invoked, removes the wireTap
|
||||
postal.diagnostics = postal.addWireTap(function(data, envelope) {
|
||||
var all = _.extend(envelope, { data: data });
|
||||
if(!JSON) {
|
||||
// this returns a callback that, if invoked, removes the wireTap
|
||||
var wireTap = postal.addWireTap( function ( data, envelope ) {
|
||||
if ( !filters.length || _.any( filters, function ( filter ) {
|
||||
return applyFilter( filter, envelope );
|
||||
} ) ) {
|
||||
if ( !JSON ) {
|
||||
throw "This browser or environment does not provide JSON support";
|
||||
}
|
||||
try {
|
||||
console.log(JSON.stringify(all));
|
||||
console.log( JSON.stringify( envelope ) );
|
||||
}
|
||||
catch(exception) {
|
||||
catch ( exception ) {
|
||||
try {
|
||||
all.data = "ERROR: " + exception.message;
|
||||
console.log(JSON.stringify(all));
|
||||
var env = _.extend( {}, envelope );
|
||||
delete env.data;
|
||||
console.log( JSON.stringify( env ) + "\n\t" + "JSON.stringify Error: " + exception.message );
|
||||
}
|
||||
catch(ex) {
|
||||
console.log("Unable to parse data to JSON: " + exception);
|
||||
catch ( ex ) {
|
||||
console.log( "Unable to parse data to JSON: " + exception );
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
} );
|
||||
|
||||
postal.diagnostics = {
|
||||
clearFilters : function () {
|
||||
filters = [];
|
||||
},
|
||||
removeFilter : function ( filter ) {
|
||||
filters = _.filter( filters, function ( item ) {
|
||||
return !_.isEqual( item, filter );
|
||||
} );
|
||||
},
|
||||
addFilter : function ( constraint ) {
|
||||
if ( !_.isArray( constraint ) ) {
|
||||
constraint = [ constraint ];
|
||||
}
|
||||
_.each( constraint, function( item ){
|
||||
if ( filters.length === 0 || !_.any( filters, function ( filter ) {
|
||||
return _.isEqual( filter, item );
|
||||
} ) ) {
|
||||
filters.push( item );
|
||||
}
|
||||
});
|
||||
|
||||
},
|
||||
getCurrentFilters : function () {
|
||||
return filters;
|
||||
},
|
||||
removeWireTap : function () {
|
||||
if ( wireTap ) {
|
||||
wireTap();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
};
|
||||
|
|
@ -225,14 +225,25 @@ var bindingsResolver = {
|
|||
this.cache[topic][binding] = true;
|
||||
}
|
||||
return result;
|
||||
},
|
||||
|
||||
reset: function() {
|
||||
this.cache = {};
|
||||
}
|
||||
};
|
||||
|
||||
var localBus = {
|
||||
|
||||
subscriptions: {},
|
||||
|
||||
wireTaps: new Array(0),
|
||||
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) {
|
||||
_.each(this.wireTaps,function(tap) {
|
||||
|
|
@ -253,6 +264,19 @@ var localBus = {
|
|||
});
|
||||
},
|
||||
|
||||
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;
|
||||
|
||||
|
|
@ -278,6 +302,10 @@ var localBus = {
|
|||
return subDef;
|
||||
},
|
||||
|
||||
subscriptions: {},
|
||||
|
||||
wireTaps: new Array(0),
|
||||
|
||||
unsubscribe: function(config) {
|
||||
if(this.subscriptions[config.channel][config.topic]) {
|
||||
var len = this.subscriptions[config.channel][config.topic].length,
|
||||
|
|
@ -289,17 +317,6 @@ 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);
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -351,8 +368,7 @@ var publishPicker = {
|
|||
return new postal.channelTypes[ options.type || "local" ]( channel, topic );
|
||||
}
|
||||
},
|
||||
sessionId = undefined,
|
||||
lastSessionId = undefined;
|
||||
sessionInfo = {};
|
||||
|
||||
// save some setup time, albeit tiny
|
||||
localBus.subscriptions[SYSTEM_CHANNEL] = {};
|
||||
|
|
@ -361,6 +377,13 @@ var postal = {
|
|||
configuration: {
|
||||
bus: localBus,
|
||||
resolver: bindingsResolver,
|
||||
getSessionIdAction: function( callback ) {
|
||||
callback( sessionInfo );
|
||||
},
|
||||
setSessionIdAction: function( info, callback ) {
|
||||
sessionInfo = info;
|
||||
callback( sessionInfo );
|
||||
},
|
||||
DEFAULT_CHANNEL: DEFAULT_CHANNEL,
|
||||
DEFAULT_PRIORITY: DEFAULT_PRIORITY,
|
||||
DEFAULT_DISPOSEAFTER: DEFAULT_DISPOSEAFTER,
|
||||
|
|
@ -428,19 +451,25 @@ var postal = {
|
|||
|
||||
utils: {
|
||||
getSessionId: function( callback ) {
|
||||
callback( sessionId );
|
||||
},
|
||||
|
||||
getLastSessionId: function( callback ) {
|
||||
callback( lastSessionId );
|
||||
postal.configuration.getSessionIdAction.call( this, callback );
|
||||
},
|
||||
|
||||
setSessionId: function( value, callback ) {
|
||||
lastSessionId = sessionId;
|
||||
sessionId = value;
|
||||
if( callback ) {
|
||||
callback();
|
||||
}
|
||||
postal.utils.getSessionId( function( info ) {
|
||||
// get the session info to move id to last id
|
||||
info.lastId = info.id;
|
||||
info.id = value;
|
||||
// invoke the callback the user provided to handle storing session
|
||||
postal.configuration.setSessionIdAction( info, function( session ) {
|
||||
callback( session );
|
||||
// publish postal event msg about the change
|
||||
postal.publish({
|
||||
channel: SYSTEM_CHANNEL,
|
||||
topic: "sessionId.changed",
|
||||
data: session
|
||||
});
|
||||
} );
|
||||
});
|
||||
},
|
||||
|
||||
getSubscribersFor: function() {
|
||||
|
|
@ -464,25 +493,11 @@ var postal = {
|
|||
return result;
|
||||
},
|
||||
|
||||
reset: function( callback ) {
|
||||
// we check first in case a custom bus or resolver
|
||||
// doesn't expose subscriptions or a resolver cache
|
||||
if(postal.configuration.bus.subscriptions) {
|
||||
_.each( postal.configuration.bus.subscriptions , function( channel ){
|
||||
_.each( channel, function( topic ){
|
||||
while( topic.length ) {
|
||||
topic.pop().unsubscribe();
|
||||
}
|
||||
});
|
||||
});
|
||||
postal.configuration.bus.subscriptions = {};
|
||||
}
|
||||
if(postal.configuration.resolver.cache) {
|
||||
postal.configuration.resolver.cache = {};
|
||||
}
|
||||
reset: function() {
|
||||
postal.configuration.bus.reset();
|
||||
postal.configuration.resolver.reset();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
module.exports = postal;
|
||||
|
|
@ -60,10 +60,10 @@ var RemoteClientProxy = function( postal, socketClient, bridge ) {
|
|||
"*" : function() {
|
||||
this.deferUntilTransition();
|
||||
},
|
||||
clientId : function( data ) {
|
||||
this.sessionId = data.sessionId;
|
||||
if( bridge.enableMigration && data.lastSessionId && data.lastSessionId !== data.sessionId ) {
|
||||
this.lastSessionId = data.lastSessionId;
|
||||
clientId : function( session ) {
|
||||
this.sessionId = session.id;
|
||||
if( bridge.enableMigration && session.lastId && session.lastId !== session.id ) {
|
||||
this.lastSessionId = session.lastId;
|
||||
this.transition( "migrating" );
|
||||
}
|
||||
else {
|
||||
|
|
|
|||
|
|
@ -2,26 +2,83 @@
|
|||
// If you need the amd-module style version, go to http://github.com/ifandelse/postal.js
|
||||
(function(postal, _, undefined) {
|
||||
|
||||
|
||||
var filters = [],
|
||||
applyFilter = function ( filter, env ) {
|
||||
var match = 0, possible = 0;
|
||||
_.each( filter, function ( item, key ) {
|
||||
if ( env[key] ) {
|
||||
possible++;
|
||||
if ( _.isObject( env[key] ) && !_.isArray( env[key] ) ) {
|
||||
if ( applyFilter( item, env[key] ) ) {
|
||||
match++;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ( _.isEqual( env[key], item ) ) {
|
||||
match++;
|
||||
}
|
||||
}
|
||||
}
|
||||
} );
|
||||
return match === possible;
|
||||
};
|
||||
|
||||
// this returns a callback that, if invoked, removes the wireTap
|
||||
postal.diagnostics = postal.addWireTap(function(data, envelope) {
|
||||
var all = _.extend(envelope, { data: data });
|
||||
if(!JSON) {
|
||||
throw "This browser or environment does not provide JSON support";
|
||||
}
|
||||
try {
|
||||
console.log(JSON.stringify(all));
|
||||
}
|
||||
catch(exception) {
|
||||
var wireTap = postal.addWireTap( function ( data, envelope ) {
|
||||
if ( !filters.length || _.any( filters, function ( filter ) {
|
||||
return applyFilter( filter, envelope );
|
||||
} ) ) {
|
||||
if ( !JSON ) {
|
||||
throw "This browser or environment does not provide JSON support";
|
||||
}
|
||||
try {
|
||||
all.data = "ERROR: " + exception.message;
|
||||
console.log(JSON.stringify(all));
|
||||
console.log( JSON.stringify( envelope ) );
|
||||
}
|
||||
catch(ex) {
|
||||
console.log("Unable to parse data to JSON: " + exception);
|
||||
catch ( exception ) {
|
||||
try {
|
||||
var env = _.extend( {}, envelope );
|
||||
delete env.data;
|
||||
console.log( JSON.stringify( env ) + "\n\t" + "JSON.stringify Error: " + exception.message );
|
||||
}
|
||||
catch ( ex ) {
|
||||
console.log( "Unable to parse data to JSON: " + exception );
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
} );
|
||||
|
||||
postal.diagnostics = {
|
||||
clearFilters : function () {
|
||||
filters = [];
|
||||
},
|
||||
removeFilter : function ( filter ) {
|
||||
filters = _.filter( filters, function ( item ) {
|
||||
return !_.isEqual( item, filter );
|
||||
} );
|
||||
},
|
||||
addFilter : function ( constraint ) {
|
||||
if ( !_.isArray( constraint ) ) {
|
||||
constraint = [ constraint ];
|
||||
}
|
||||
_.each( constraint, function( item ){
|
||||
if ( filters.length === 0 || !_.any( filters, function ( filter ) {
|
||||
return _.isEqual( filter, item );
|
||||
} ) ) {
|
||||
filters.push( item );
|
||||
}
|
||||
});
|
||||
|
||||
},
|
||||
getCurrentFilters : function () {
|
||||
return filters;
|
||||
},
|
||||
removeWireTap : function () {
|
||||
if ( wireTap ) {
|
||||
wireTap();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
})( postal, _ );
|
||||
|
|
@ -1 +1 @@
|
|||
(function(a,b,c){a.diagnostics=a.addWireTap(function(a,c){var d=b.extend(c,{data:a});if(!JSON)throw"This browser or environment does not provide JSON support";try{console.log(JSON.stringify(d))}catch(e){try{d.data="ERROR: "+e.message,console.log(JSON.stringify(d))}catch(f){console.log("Unable to parse data to JSON: "+e)}}})})(postal,_)
|
||||
(function(a,b,c){var d=[],e=function(a,c){var d=0,f=0;return b.each(a,function(a,g){c[g]&&(f++,b.isObject(c[g])&&!b.isArray(c[g])?e(a,c[g])&&d++:b.isEqual(c[g],a)&&d++)}),d===f},f=a.addWireTap(function(a,c){if(!d.length||b.any(d,function(a){return e(a,c)})){if(!JSON)throw"This browser or environment does not provide JSON support";try{console.log(JSON.stringify(c))}catch(f){try{var g=b.extend({},c);delete g.data,console.log(JSON.stringify(g)+"\n "+"JSON.stringify Error: "+f.message)}catch(h){console.log("Unable to parse data to JSON: "+f)}}}});a.diagnostics={clearFilters:function(){d=[]},removeFilter:function(a){d=b.filter(d,function(c){return!b.isEqual(c,a)})},addFilter:function(a){b.isArray(a)||(a=[a]),b.each(a,function(a){(d.length===0||!b.any(d,function(c){return b.isEqual(c,a)}))&&d.push(a)})},getCurrentFilters:function(){return d},removeWireTap:function(){f&&f()}}})(postal,_)
|
||||
|
|
@ -225,14 +225,25 @@ var bindingsResolver = {
|
|||
this.cache[topic][binding] = true;
|
||||
}
|
||||
return result;
|
||||
},
|
||||
|
||||
reset: function() {
|
||||
this.cache = {};
|
||||
}
|
||||
};
|
||||
|
||||
var localBus = {
|
||||
|
||||
subscriptions: {},
|
||||
|
||||
wireTaps: new Array(0),
|
||||
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) {
|
||||
_.each(this.wireTaps,function(tap) {
|
||||
|
|
@ -253,6 +264,19 @@ var localBus = {
|
|||
});
|
||||
},
|
||||
|
||||
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;
|
||||
|
||||
|
|
@ -278,6 +302,10 @@ var localBus = {
|
|||
return subDef;
|
||||
},
|
||||
|
||||
subscriptions: {},
|
||||
|
||||
wireTaps: new Array(0),
|
||||
|
||||
unsubscribe: function(config) {
|
||||
if(this.subscriptions[config.channel][config.topic]) {
|
||||
var len = this.subscriptions[config.channel][config.topic].length,
|
||||
|
|
@ -289,17 +317,6 @@ 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);
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -351,8 +368,7 @@ var publishPicker = {
|
|||
return new postal.channelTypes[ options.type || "local" ]( channel, topic );
|
||||
}
|
||||
},
|
||||
sessionId = undefined,
|
||||
lastSessionId = undefined;
|
||||
sessionInfo = {};
|
||||
|
||||
// save some setup time, albeit tiny
|
||||
localBus.subscriptions[SYSTEM_CHANNEL] = {};
|
||||
|
|
@ -361,6 +377,13 @@ var postal = {
|
|||
configuration: {
|
||||
bus: localBus,
|
||||
resolver: bindingsResolver,
|
||||
getSessionIdAction: function( callback ) {
|
||||
callback( sessionInfo );
|
||||
},
|
||||
setSessionIdAction: function( info, callback ) {
|
||||
sessionInfo = info;
|
||||
callback( sessionInfo );
|
||||
},
|
||||
DEFAULT_CHANNEL: DEFAULT_CHANNEL,
|
||||
DEFAULT_PRIORITY: DEFAULT_PRIORITY,
|
||||
DEFAULT_DISPOSEAFTER: DEFAULT_DISPOSEAFTER,
|
||||
|
|
@ -428,19 +451,25 @@ var postal = {
|
|||
|
||||
utils: {
|
||||
getSessionId: function( callback ) {
|
||||
callback( sessionId );
|
||||
},
|
||||
|
||||
getLastSessionId: function( callback ) {
|
||||
callback( lastSessionId );
|
||||
postal.configuration.getSessionIdAction.call( this, callback );
|
||||
},
|
||||
|
||||
setSessionId: function( value, callback ) {
|
||||
lastSessionId = sessionId;
|
||||
sessionId = value;
|
||||
if( callback ) {
|
||||
callback();
|
||||
}
|
||||
postal.utils.getSessionId( function( info ) {
|
||||
// get the session info to move id to last id
|
||||
info.lastId = info.id;
|
||||
info.id = value;
|
||||
// invoke the callback the user provided to handle storing session
|
||||
postal.configuration.setSessionIdAction( info, function( session ) {
|
||||
callback( session );
|
||||
// publish postal event msg about the change
|
||||
postal.publish({
|
||||
channel: SYSTEM_CHANNEL,
|
||||
topic: "sessionId.changed",
|
||||
data: session
|
||||
});
|
||||
} );
|
||||
});
|
||||
},
|
||||
|
||||
getSubscribersFor: function() {
|
||||
|
|
@ -464,27 +493,13 @@ var postal = {
|
|||
return result;
|
||||
},
|
||||
|
||||
reset: function( callback ) {
|
||||
// we check first in case a custom bus or resolver
|
||||
// doesn't expose subscriptions or a resolver cache
|
||||
if(postal.configuration.bus.subscriptions) {
|
||||
_.each( postal.configuration.bus.subscriptions , function( channel ){
|
||||
_.each( channel, function( topic ){
|
||||
while( topic.length ) {
|
||||
topic.pop().unsubscribe();
|
||||
}
|
||||
});
|
||||
});
|
||||
postal.configuration.bus.subscriptions = {};
|
||||
}
|
||||
if(postal.configuration.resolver.cache) {
|
||||
postal.configuration.resolver.cache = {};
|
||||
}
|
||||
reset: function() {
|
||||
postal.configuration.bus.reset();
|
||||
postal.configuration.resolver.reset();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
global.postal = postal;
|
||||
|
||||
})(_, window);
|
||||
2
example/standard/js/postal.min.js
vendored
2
example/standard/js/postal.min.js
vendored
File diff suppressed because one or more lines are too long
|
|
@ -1,27 +1,84 @@
|
|||
// This is the amd module version of postal.diagnostics.js
|
||||
// If you need the standard lib version, go to http://github.com/ifandelse/postal.js
|
||||
define(["postal", "underscore"], function(postal, _, undefined) {
|
||||
|
||||
define( [ "postal", "underscore" ], function ( postal, _, undefined ) {
|
||||
|
||||
var filters = [],
|
||||
applyFilter = function ( filter, env ) {
|
||||
var match = 0, possible = 0;
|
||||
_.each( filter, function ( item, key ) {
|
||||
if ( env[key] ) {
|
||||
possible++;
|
||||
if ( _.isObject( env[key] ) && !_.isArray( env[key] ) ) {
|
||||
if ( applyFilter( item, env[key] ) ) {
|
||||
match++;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ( _.isEqual( env[key], item ) ) {
|
||||
match++;
|
||||
}
|
||||
}
|
||||
}
|
||||
} );
|
||||
return match === possible;
|
||||
};
|
||||
|
||||
// this returns a callback that, if invoked, removes the wireTap
|
||||
postal.diagnostics = postal.addWireTap(function(data, envelope) {
|
||||
var all = _.extend(envelope, { data: data });
|
||||
if(!JSON) {
|
||||
throw "This browser or environment does not provide JSON support";
|
||||
}
|
||||
try {
|
||||
console.log(JSON.stringify(all));
|
||||
}
|
||||
catch(exception) {
|
||||
var wireTap = postal.addWireTap( function ( data, envelope ) {
|
||||
if ( !filters.length || _.any( filters, function ( filter ) {
|
||||
return applyFilter( filter, envelope );
|
||||
} ) ) {
|
||||
if ( !JSON ) {
|
||||
throw "This browser or environment does not provide JSON support";
|
||||
}
|
||||
try {
|
||||
all.data = "ERROR: " + exception.message;
|
||||
console.log(JSON.stringify(all));
|
||||
console.log( JSON.stringify( envelope ) );
|
||||
}
|
||||
catch(ex) {
|
||||
console.log("Unable to parse data to JSON: " + exception);
|
||||
catch ( exception ) {
|
||||
try {
|
||||
var env = _.extend( {}, envelope );
|
||||
delete env.data;
|
||||
console.log( JSON.stringify( env ) + "\n\t" + "JSON.stringify Error: " + exception.message );
|
||||
}
|
||||
catch ( ex ) {
|
||||
console.log( "Unable to parse data to JSON: " + exception );
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
} );
|
||||
|
||||
postal.diagnostics = {
|
||||
clearFilters : function () {
|
||||
filters = [];
|
||||
},
|
||||
removeFilter : function ( filter ) {
|
||||
filters = _.filter( filters, function ( item ) {
|
||||
return !_.isEqual( item, filter );
|
||||
} );
|
||||
},
|
||||
addFilter : function ( constraint ) {
|
||||
if ( !_.isArray( constraint ) ) {
|
||||
constraint = [ constraint ];
|
||||
}
|
||||
_.each( constraint, function( item ){
|
||||
if ( filters.length === 0 || !_.any( filters, function ( filter ) {
|
||||
return _.isEqual( filter, item );
|
||||
} ) ) {
|
||||
filters.push( item );
|
||||
}
|
||||
});
|
||||
|
||||
},
|
||||
getCurrentFilters : function () {
|
||||
return filters;
|
||||
},
|
||||
removeWireTap : function () {
|
||||
if ( wireTap ) {
|
||||
wireTap();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
});
|
||||
} );
|
||||
2
lib/amd/postal.diagnostics.min.js
vendored
2
lib/amd/postal.diagnostics.min.js
vendored
|
|
@ -1 +1 @@
|
|||
define(["postal","underscore"],function(a,b,c){a.diagnostics=a.addWireTap(function(a,c){var d=b.extend(c,{data:a});if(!JSON)throw"This browser or environment does not provide JSON support";try{console.log(JSON.stringify(d))}catch(e){try{d.data="ERROR: "+e.message,console.log(JSON.stringify(d))}catch(f){console.log("Unable to parse data to JSON: "+e)}}})})
|
||||
define(["postal","underscore"],function(a,b,c){var d=[],e=function(a,c){var d=0,f=0;return b.each(a,function(a,g){c[g]&&(f++,b.isObject(c[g])&&!b.isArray(c[g])?e(a,c[g])&&d++:b.isEqual(c[g],a)&&d++)}),d===f},f=a.addWireTap(function(a,c){if(!d.length||b.any(d,function(a){return e(a,c)})){if(!JSON)throw"This browser or environment does not provide JSON support";try{console.log(JSON.stringify(c))}catch(f){try{var g=b.extend({},c);delete g.data,console.log(JSON.stringify(g)+"\n "+"JSON.stringify Error: "+f.message)}catch(h){console.log("Unable to parse data to JSON: "+f)}}}});a.diagnostics={clearFilters:function(){d=[]},removeFilter:function(a){d=b.filter(d,function(c){return!b.isEqual(c,a)})},addFilter:function(a){b.isArray(a)||(a=[a]),b.each(a,function(a){(d.length===0||!b.any(d,function(c){return b.isEqual(c,a)}))&&d.push(a)})},getCurrentFilters:function(){return d},removeWireTap:function(){f&&f()}}})
|
||||
|
|
@ -225,14 +225,25 @@ var bindingsResolver = {
|
|||
this.cache[topic][binding] = true;
|
||||
}
|
||||
return result;
|
||||
},
|
||||
|
||||
reset: function() {
|
||||
this.cache = {};
|
||||
}
|
||||
};
|
||||
|
||||
var localBus = {
|
||||
|
||||
subscriptions: {},
|
||||
|
||||
wireTaps: new Array(0),
|
||||
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) {
|
||||
_.each(this.wireTaps,function(tap) {
|
||||
|
|
@ -253,6 +264,19 @@ var localBus = {
|
|||
});
|
||||
},
|
||||
|
||||
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;
|
||||
|
||||
|
|
@ -278,6 +302,10 @@ var localBus = {
|
|||
return subDef;
|
||||
},
|
||||
|
||||
subscriptions: {},
|
||||
|
||||
wireTaps: new Array(0),
|
||||
|
||||
unsubscribe: function(config) {
|
||||
if(this.subscriptions[config.channel][config.topic]) {
|
||||
var len = this.subscriptions[config.channel][config.topic].length,
|
||||
|
|
@ -289,17 +317,6 @@ 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);
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -351,8 +368,7 @@ var publishPicker = {
|
|||
return new postal.channelTypes[ options.type || "local" ]( channel, topic );
|
||||
}
|
||||
},
|
||||
sessionId = undefined,
|
||||
lastSessionId = undefined;
|
||||
sessionInfo = {};
|
||||
|
||||
// save some setup time, albeit tiny
|
||||
localBus.subscriptions[SYSTEM_CHANNEL] = {};
|
||||
|
|
@ -361,6 +377,13 @@ var postal = {
|
|||
configuration: {
|
||||
bus: localBus,
|
||||
resolver: bindingsResolver,
|
||||
getSessionIdAction: function( callback ) {
|
||||
callback( sessionInfo );
|
||||
},
|
||||
setSessionIdAction: function( info, callback ) {
|
||||
sessionInfo = info;
|
||||
callback( sessionInfo );
|
||||
},
|
||||
DEFAULT_CHANNEL: DEFAULT_CHANNEL,
|
||||
DEFAULT_PRIORITY: DEFAULT_PRIORITY,
|
||||
DEFAULT_DISPOSEAFTER: DEFAULT_DISPOSEAFTER,
|
||||
|
|
@ -428,19 +451,25 @@ var postal = {
|
|||
|
||||
utils: {
|
||||
getSessionId: function( callback ) {
|
||||
callback( sessionId );
|
||||
},
|
||||
|
||||
getLastSessionId: function( callback ) {
|
||||
callback( lastSessionId );
|
||||
postal.configuration.getSessionIdAction.call( this, callback );
|
||||
},
|
||||
|
||||
setSessionId: function( value, callback ) {
|
||||
lastSessionId = sessionId;
|
||||
sessionId = value;
|
||||
if( callback ) {
|
||||
callback();
|
||||
}
|
||||
postal.utils.getSessionId( function( info ) {
|
||||
// get the session info to move id to last id
|
||||
info.lastId = info.id;
|
||||
info.id = value;
|
||||
// invoke the callback the user provided to handle storing session
|
||||
postal.configuration.setSessionIdAction( info, function( session ) {
|
||||
callback( session );
|
||||
// publish postal event msg about the change
|
||||
postal.publish({
|
||||
channel: SYSTEM_CHANNEL,
|
||||
topic: "sessionId.changed",
|
||||
data: session
|
||||
});
|
||||
} );
|
||||
});
|
||||
},
|
||||
|
||||
getSubscribersFor: function() {
|
||||
|
|
@ -464,26 +493,12 @@ var postal = {
|
|||
return result;
|
||||
},
|
||||
|
||||
reset: function( callback ) {
|
||||
// we check first in case a custom bus or resolver
|
||||
// doesn't expose subscriptions or a resolver cache
|
||||
if(postal.configuration.bus.subscriptions) {
|
||||
_.each( postal.configuration.bus.subscriptions , function( channel ){
|
||||
_.each( channel, function( topic ){
|
||||
while( topic.length ) {
|
||||
topic.pop().unsubscribe();
|
||||
}
|
||||
});
|
||||
});
|
||||
postal.configuration.bus.subscriptions = {};
|
||||
}
|
||||
if(postal.configuration.resolver.cache) {
|
||||
postal.configuration.resolver.cache = {};
|
||||
}
|
||||
reset: function() {
|
||||
postal.configuration.bus.reset();
|
||||
postal.configuration.resolver.reset();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
return postal;
|
||||
});
|
||||
2
lib/amd/postal.min.js
vendored
2
lib/amd/postal.min.js
vendored
File diff suppressed because one or more lines are too long
|
|
@ -1,25 +1,80 @@
|
|||
var _ = require('underscore');
|
||||
|
||||
module.exports = function(postal) {
|
||||
module.exports = function( _, postal ) {
|
||||
var filters = [],
|
||||
applyFilter = function ( filter, env ) {
|
||||
var match = 0, possible = 0;
|
||||
_.each( filter, function ( item, key ) {
|
||||
if ( env[key] ) {
|
||||
possible++;
|
||||
if ( _.isObject( env[key] ) && !_.isArray( env[key] ) ) {
|
||||
if ( applyFilter( item, env[key] ) ) {
|
||||
match++;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ( _.isEqual( env[key], item ) ) {
|
||||
match++;
|
||||
}
|
||||
}
|
||||
}
|
||||
} );
|
||||
return match === possible;
|
||||
};
|
||||
|
||||
// this returns a callback that, if invoked, removes the wireTap
|
||||
postal.diagnostics = postal.addWireTap(function(data, envelope) {
|
||||
var all = _.extend(envelope, { data: data });
|
||||
if(!JSON) {
|
||||
// this returns a callback that, if invoked, removes the wireTap
|
||||
var wireTap = postal.addWireTap( function ( data, envelope ) {
|
||||
if ( !filters.length || _.any( filters, function ( filter ) {
|
||||
return applyFilter( filter, envelope );
|
||||
} ) ) {
|
||||
if ( !JSON ) {
|
||||
throw "This browser or environment does not provide JSON support";
|
||||
}
|
||||
try {
|
||||
console.log(JSON.stringify(all));
|
||||
console.log( JSON.stringify( envelope ) );
|
||||
}
|
||||
catch(exception) {
|
||||
catch ( exception ) {
|
||||
try {
|
||||
all.data = "ERROR: " + exception.message;
|
||||
console.log(JSON.stringify(all));
|
||||
var env = _.extend( {}, envelope );
|
||||
delete env.data;
|
||||
console.log( JSON.stringify( env ) + "\n\t" + "JSON.stringify Error: " + exception.message );
|
||||
}
|
||||
catch(ex) {
|
||||
console.log("Unable to parse data to JSON: " + exception);
|
||||
catch ( ex ) {
|
||||
console.log( "Unable to parse data to JSON: " + exception );
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
} );
|
||||
|
||||
postal.diagnostics = {
|
||||
clearFilters : function () {
|
||||
filters = [];
|
||||
},
|
||||
removeFilter : function ( filter ) {
|
||||
filters = _.filter( filters, function ( item ) {
|
||||
return !_.isEqual( item, filter );
|
||||
} );
|
||||
},
|
||||
addFilter : function ( constraint ) {
|
||||
if ( !_.isArray( constraint ) ) {
|
||||
constraint = [ constraint ];
|
||||
}
|
||||
_.each( constraint, function( item ){
|
||||
if ( filters.length === 0 || !_.any( filters, function ( filter ) {
|
||||
return _.isEqual( filter, item );
|
||||
} ) ) {
|
||||
filters.push( item );
|
||||
}
|
||||
});
|
||||
|
||||
},
|
||||
getCurrentFilters : function () {
|
||||
return filters;
|
||||
},
|
||||
removeWireTap : function () {
|
||||
if ( wireTap ) {
|
||||
wireTap();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
};
|
||||
|
|
@ -225,14 +225,25 @@ var bindingsResolver = {
|
|||
this.cache[topic][binding] = true;
|
||||
}
|
||||
return result;
|
||||
},
|
||||
|
||||
reset: function() {
|
||||
this.cache = {};
|
||||
}
|
||||
};
|
||||
|
||||
var localBus = {
|
||||
|
||||
subscriptions: {},
|
||||
|
||||
wireTaps: new Array(0),
|
||||
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) {
|
||||
_.each(this.wireTaps,function(tap) {
|
||||
|
|
@ -253,6 +264,19 @@ var localBus = {
|
|||
});
|
||||
},
|
||||
|
||||
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;
|
||||
|
||||
|
|
@ -278,6 +302,10 @@ var localBus = {
|
|||
return subDef;
|
||||
},
|
||||
|
||||
subscriptions: {},
|
||||
|
||||
wireTaps: new Array(0),
|
||||
|
||||
unsubscribe: function(config) {
|
||||
if(this.subscriptions[config.channel][config.topic]) {
|
||||
var len = this.subscriptions[config.channel][config.topic].length,
|
||||
|
|
@ -289,17 +317,6 @@ 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);
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -351,8 +368,7 @@ var publishPicker = {
|
|||
return new postal.channelTypes[ options.type || "local" ]( channel, topic );
|
||||
}
|
||||
},
|
||||
sessionId = undefined,
|
||||
lastSessionId = undefined;
|
||||
sessionInfo = {};
|
||||
|
||||
// save some setup time, albeit tiny
|
||||
localBus.subscriptions[SYSTEM_CHANNEL] = {};
|
||||
|
|
@ -361,6 +377,13 @@ var postal = {
|
|||
configuration: {
|
||||
bus: localBus,
|
||||
resolver: bindingsResolver,
|
||||
getSessionIdAction: function( callback ) {
|
||||
callback( sessionInfo );
|
||||
},
|
||||
setSessionIdAction: function( info, callback ) {
|
||||
sessionInfo = info;
|
||||
callback( sessionInfo );
|
||||
},
|
||||
DEFAULT_CHANNEL: DEFAULT_CHANNEL,
|
||||
DEFAULT_PRIORITY: DEFAULT_PRIORITY,
|
||||
DEFAULT_DISPOSEAFTER: DEFAULT_DISPOSEAFTER,
|
||||
|
|
@ -428,19 +451,25 @@ var postal = {
|
|||
|
||||
utils: {
|
||||
getSessionId: function( callback ) {
|
||||
callback( sessionId );
|
||||
},
|
||||
|
||||
getLastSessionId: function( callback ) {
|
||||
callback( lastSessionId );
|
||||
postal.configuration.getSessionIdAction.call( this, callback );
|
||||
},
|
||||
|
||||
setSessionId: function( value, callback ) {
|
||||
lastSessionId = sessionId;
|
||||
sessionId = value;
|
||||
if( callback ) {
|
||||
callback();
|
||||
}
|
||||
postal.utils.getSessionId( function( info ) {
|
||||
// get the session info to move id to last id
|
||||
info.lastId = info.id;
|
||||
info.id = value;
|
||||
// invoke the callback the user provided to handle storing session
|
||||
postal.configuration.setSessionIdAction( info, function( session ) {
|
||||
callback( session );
|
||||
// publish postal event msg about the change
|
||||
postal.publish({
|
||||
channel: SYSTEM_CHANNEL,
|
||||
topic: "sessionId.changed",
|
||||
data: session
|
||||
});
|
||||
} );
|
||||
});
|
||||
},
|
||||
|
||||
getSubscribersFor: function() {
|
||||
|
|
@ -464,25 +493,11 @@ var postal = {
|
|||
return result;
|
||||
},
|
||||
|
||||
reset: function( callback ) {
|
||||
// we check first in case a custom bus or resolver
|
||||
// doesn't expose subscriptions or a resolver cache
|
||||
if(postal.configuration.bus.subscriptions) {
|
||||
_.each( postal.configuration.bus.subscriptions , function( channel ){
|
||||
_.each( channel, function( topic ){
|
||||
while( topic.length ) {
|
||||
topic.pop().unsubscribe();
|
||||
}
|
||||
});
|
||||
});
|
||||
postal.configuration.bus.subscriptions = {};
|
||||
}
|
||||
if(postal.configuration.resolver.cache) {
|
||||
postal.configuration.resolver.cache = {};
|
||||
}
|
||||
reset: function() {
|
||||
postal.configuration.bus.reset();
|
||||
postal.configuration.resolver.reset();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
module.exports = postal;
|
||||
|
|
@ -2,26 +2,83 @@
|
|||
// If you need the amd-module style version, go to http://github.com/ifandelse/postal.js
|
||||
(function(postal, _, undefined) {
|
||||
|
||||
|
||||
var filters = [],
|
||||
applyFilter = function ( filter, env ) {
|
||||
var match = 0, possible = 0;
|
||||
_.each( filter, function ( item, key ) {
|
||||
if ( env[key] ) {
|
||||
possible++;
|
||||
if ( _.isObject( env[key] ) && !_.isArray( env[key] ) ) {
|
||||
if ( applyFilter( item, env[key] ) ) {
|
||||
match++;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ( _.isEqual( env[key], item ) ) {
|
||||
match++;
|
||||
}
|
||||
}
|
||||
}
|
||||
} );
|
||||
return match === possible;
|
||||
};
|
||||
|
||||
// this returns a callback that, if invoked, removes the wireTap
|
||||
postal.diagnostics = postal.addWireTap(function(data, envelope) {
|
||||
var all = _.extend(envelope, { data: data });
|
||||
if(!JSON) {
|
||||
throw "This browser or environment does not provide JSON support";
|
||||
}
|
||||
try {
|
||||
console.log(JSON.stringify(all));
|
||||
}
|
||||
catch(exception) {
|
||||
var wireTap = postal.addWireTap( function ( data, envelope ) {
|
||||
if ( !filters.length || _.any( filters, function ( filter ) {
|
||||
return applyFilter( filter, envelope );
|
||||
} ) ) {
|
||||
if ( !JSON ) {
|
||||
throw "This browser or environment does not provide JSON support";
|
||||
}
|
||||
try {
|
||||
all.data = "ERROR: " + exception.message;
|
||||
console.log(JSON.stringify(all));
|
||||
console.log( JSON.stringify( envelope ) );
|
||||
}
|
||||
catch(ex) {
|
||||
console.log("Unable to parse data to JSON: " + exception);
|
||||
catch ( exception ) {
|
||||
try {
|
||||
var env = _.extend( {}, envelope );
|
||||
delete env.data;
|
||||
console.log( JSON.stringify( env ) + "\n\t" + "JSON.stringify Error: " + exception.message );
|
||||
}
|
||||
catch ( ex ) {
|
||||
console.log( "Unable to parse data to JSON: " + exception );
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
} );
|
||||
|
||||
postal.diagnostics = {
|
||||
clearFilters : function () {
|
||||
filters = [];
|
||||
},
|
||||
removeFilter : function ( filter ) {
|
||||
filters = _.filter( filters, function ( item ) {
|
||||
return !_.isEqual( item, filter );
|
||||
} );
|
||||
},
|
||||
addFilter : function ( constraint ) {
|
||||
if ( !_.isArray( constraint ) ) {
|
||||
constraint = [ constraint ];
|
||||
}
|
||||
_.each( constraint, function( item ){
|
||||
if ( filters.length === 0 || !_.any( filters, function ( filter ) {
|
||||
return _.isEqual( filter, item );
|
||||
} ) ) {
|
||||
filters.push( item );
|
||||
}
|
||||
});
|
||||
|
||||
},
|
||||
getCurrentFilters : function () {
|
||||
return filters;
|
||||
},
|
||||
removeWireTap : function () {
|
||||
if ( wireTap ) {
|
||||
wireTap();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
})( postal, _ );
|
||||
2
lib/standard/postal.diagnostics.min.js
vendored
2
lib/standard/postal.diagnostics.min.js
vendored
|
|
@ -1 +1 @@
|
|||
(function(a,b,c){a.diagnostics=a.addWireTap(function(a,c){var d=b.extend(c,{data:a});if(!JSON)throw"This browser or environment does not provide JSON support";try{console.log(JSON.stringify(d))}catch(e){try{d.data="ERROR: "+e.message,console.log(JSON.stringify(d))}catch(f){console.log("Unable to parse data to JSON: "+e)}}})})(postal,_)
|
||||
(function(a,b,c){var d=[],e=function(a,c){var d=0,f=0;return b.each(a,function(a,g){c[g]&&(f++,b.isObject(c[g])&&!b.isArray(c[g])?e(a,c[g])&&d++:b.isEqual(c[g],a)&&d++)}),d===f},f=a.addWireTap(function(a,c){if(!d.length||b.any(d,function(a){return e(a,c)})){if(!JSON)throw"This browser or environment does not provide JSON support";try{console.log(JSON.stringify(c))}catch(f){try{var g=b.extend({},c);delete g.data,console.log(JSON.stringify(g)+"\n "+"JSON.stringify Error: "+f.message)}catch(h){console.log("Unable to parse data to JSON: "+f)}}}});a.diagnostics={clearFilters:function(){d=[]},removeFilter:function(a){d=b.filter(d,function(c){return!b.isEqual(c,a)})},addFilter:function(a){b.isArray(a)||(a=[a]),b.each(a,function(a){(d.length===0||!b.any(d,function(c){return b.isEqual(c,a)}))&&d.push(a)})},getCurrentFilters:function(){return d},removeWireTap:function(){f&&f()}}})(postal,_)
|
||||
|
|
@ -225,14 +225,25 @@ var bindingsResolver = {
|
|||
this.cache[topic][binding] = true;
|
||||
}
|
||||
return result;
|
||||
},
|
||||
|
||||
reset: function() {
|
||||
this.cache = {};
|
||||
}
|
||||
};
|
||||
|
||||
var localBus = {
|
||||
|
||||
subscriptions: {},
|
||||
|
||||
wireTaps: new Array(0),
|
||||
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) {
|
||||
_.each(this.wireTaps,function(tap) {
|
||||
|
|
@ -253,6 +264,19 @@ var localBus = {
|
|||
});
|
||||
},
|
||||
|
||||
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;
|
||||
|
||||
|
|
@ -278,6 +302,10 @@ var localBus = {
|
|||
return subDef;
|
||||
},
|
||||
|
||||
subscriptions: {},
|
||||
|
||||
wireTaps: new Array(0),
|
||||
|
||||
unsubscribe: function(config) {
|
||||
if(this.subscriptions[config.channel][config.topic]) {
|
||||
var len = this.subscriptions[config.channel][config.topic].length,
|
||||
|
|
@ -289,17 +317,6 @@ 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);
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -351,8 +368,7 @@ var publishPicker = {
|
|||
return new postal.channelTypes[ options.type || "local" ]( channel, topic );
|
||||
}
|
||||
},
|
||||
sessionId = undefined,
|
||||
lastSessionId = undefined;
|
||||
sessionInfo = {};
|
||||
|
||||
// save some setup time, albeit tiny
|
||||
localBus.subscriptions[SYSTEM_CHANNEL] = {};
|
||||
|
|
@ -361,6 +377,13 @@ var postal = {
|
|||
configuration: {
|
||||
bus: localBus,
|
||||
resolver: bindingsResolver,
|
||||
getSessionIdAction: function( callback ) {
|
||||
callback( sessionInfo );
|
||||
},
|
||||
setSessionIdAction: function( info, callback ) {
|
||||
sessionInfo = info;
|
||||
callback( sessionInfo );
|
||||
},
|
||||
DEFAULT_CHANNEL: DEFAULT_CHANNEL,
|
||||
DEFAULT_PRIORITY: DEFAULT_PRIORITY,
|
||||
DEFAULT_DISPOSEAFTER: DEFAULT_DISPOSEAFTER,
|
||||
|
|
@ -428,19 +451,25 @@ var postal = {
|
|||
|
||||
utils: {
|
||||
getSessionId: function( callback ) {
|
||||
callback( sessionId );
|
||||
},
|
||||
|
||||
getLastSessionId: function( callback ) {
|
||||
callback( lastSessionId );
|
||||
postal.configuration.getSessionIdAction.call( this, callback );
|
||||
},
|
||||
|
||||
setSessionId: function( value, callback ) {
|
||||
lastSessionId = sessionId;
|
||||
sessionId = value;
|
||||
if( callback ) {
|
||||
callback();
|
||||
}
|
||||
postal.utils.getSessionId( function( info ) {
|
||||
// get the session info to move id to last id
|
||||
info.lastId = info.id;
|
||||
info.id = value;
|
||||
// invoke the callback the user provided to handle storing session
|
||||
postal.configuration.setSessionIdAction( info, function( session ) {
|
||||
callback( session );
|
||||
// publish postal event msg about the change
|
||||
postal.publish({
|
||||
channel: SYSTEM_CHANNEL,
|
||||
topic: "sessionId.changed",
|
||||
data: session
|
||||
});
|
||||
} );
|
||||
});
|
||||
},
|
||||
|
||||
getSubscribersFor: function() {
|
||||
|
|
@ -464,27 +493,13 @@ var postal = {
|
|||
return result;
|
||||
},
|
||||
|
||||
reset: function( callback ) {
|
||||
// we check first in case a custom bus or resolver
|
||||
// doesn't expose subscriptions or a resolver cache
|
||||
if(postal.configuration.bus.subscriptions) {
|
||||
_.each( postal.configuration.bus.subscriptions , function( channel ){
|
||||
_.each( channel, function( topic ){
|
||||
while( topic.length ) {
|
||||
topic.pop().unsubscribe();
|
||||
}
|
||||
});
|
||||
});
|
||||
postal.configuration.bus.subscriptions = {};
|
||||
}
|
||||
if(postal.configuration.resolver.cache) {
|
||||
postal.configuration.resolver.cache = {};
|
||||
}
|
||||
reset: function() {
|
||||
postal.configuration.bus.reset();
|
||||
postal.configuration.resolver.reset();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
global.postal = postal;
|
||||
|
||||
})(_, window);
|
||||
2
lib/standard/postal.min.js
vendored
2
lib/standard/postal.min.js
vendored
File diff suppressed because one or more lines are too long
|
|
@ -1,3 +1,4 @@
|
|||
QUnit.reorder = false;
|
||||
QUnit.specify("postal.js", function(){
|
||||
describe("Postal", function(){
|
||||
var subscription,
|
||||
|
|
@ -713,71 +714,85 @@ QUnit.specify("postal.js", function(){
|
|||
assert(_.isEmpty(postal.configuration.resolver.cache)).isTrue();
|
||||
});
|
||||
});
|
||||
describe("When calling postal.utils.getSessionId", function() {
|
||||
var sid, last;
|
||||
describe("with default implementation", function() {
|
||||
describe("before any value is set", function() {
|
||||
it("should have an initial value of undefined", async(function(){
|
||||
postal.utils.getSessionId(function(id){
|
||||
sid = id;
|
||||
assert(sid == undefined).isTrue();
|
||||
describe( "When calling postal.utils.getSessionInfo", function() {
|
||||
describe( "with default implementation", function() {
|
||||
describe( "before any value is set", function() {
|
||||
it( "should have an initial value of undefined", async( function(){
|
||||
postal.utils.getSessionId( function( info ){
|
||||
assert( _.isEmpty( info ) ).isTrue();
|
||||
resume();
|
||||
})
|
||||
}));
|
||||
});
|
||||
describe("after a value is set", function(){
|
||||
it("should have a value matching expected result", async(function(){
|
||||
postal.utils.setSessionId("123456", function() {
|
||||
postal.utils.getSessionId(function(id){
|
||||
sid = id;
|
||||
postal.utils.getLastSessionId(function(last){
|
||||
assert(last == undefined).isTrue();
|
||||
assert(sid).equals("123456");
|
||||
resume();
|
||||
});
|
||||
});
|
||||
describe( "after a value is set", function(){
|
||||
before(function(){
|
||||
postal.subscribe({
|
||||
channel: SYSTEM_CHANNEL,
|
||||
topic: "sessionId.changed",
|
||||
callback: function( data, env ) {
|
||||
assert( data.lastId == undefined ).isTrue();
|
||||
assert( data.id ).equals( "123456" );
|
||||
resume();
|
||||
}
|
||||
});
|
||||
});
|
||||
after(function() {
|
||||
postal.utils.reset();
|
||||
});
|
||||
it( "should have a value matching expected result", async( function(){
|
||||
postal.utils.setSessionId( "123456", function( info ) {
|
||||
assert( info.lastId == undefined ).isTrue();
|
||||
assert( info.id ).equals( "123456" );
|
||||
});
|
||||
}));
|
||||
});
|
||||
describe( "after a value is set a second time", function(){
|
||||
before(function(){
|
||||
postal.subscribe({
|
||||
channel: SYSTEM_CHANNEL,
|
||||
topic: "sessionId.changed",
|
||||
callback: function( data, env ) {
|
||||
assert( data.lastId ).equals( "123456" );
|
||||
assert( data.id ).equals( "98765" );
|
||||
resume();
|
||||
}
|
||||
});
|
||||
});
|
||||
after(function() {
|
||||
postal.utils.reset();
|
||||
});
|
||||
it( "should have a value matching expected result", async( function(){
|
||||
postal.utils.setSessionId( "98765", function() {
|
||||
postal.utils.getSessionId( function( id ){
|
||||
sid = id;
|
||||
postal.utils.getLastSessionId( function( last ){
|
||||
assert( last ).equals( "123456" );
|
||||
assert( sid ).equals( "98765" );
|
||||
resume();
|
||||
});
|
||||
});
|
||||
postal.utils.setSessionId( "98765", function( info ) {
|
||||
assert( info.lastId ).equals( "123456" );
|
||||
assert( info.id ).equals( "98765" );
|
||||
});
|
||||
}));
|
||||
});
|
||||
});
|
||||
});
|
||||
describe("When calling utils.getSubscribersFor", function() {
|
||||
describe( "When calling utils.getSubscribersFor", function() {
|
||||
var subs = [], i;
|
||||
before(function(){
|
||||
before( function(){
|
||||
i = 10;
|
||||
var ch1 = postal.channel({ channel: "MyChannel", topic: "MyTopic" }),
|
||||
ch2 = postal.channel({ channel: "MyChannel2", topic: "MyTopic2" });
|
||||
while(i) {
|
||||
subs.push(ch1.subscribe(function() { }));
|
||||
subs.push(ch2.subscribe(function() { }));
|
||||
subs.push( ch1.subscribe( function() { }));
|
||||
subs.push( ch2.subscribe( function() { }));
|
||||
i--;
|
||||
}
|
||||
});
|
||||
after(function(){
|
||||
after( function(){
|
||||
sub = [];
|
||||
postal.utils.reset();
|
||||
});
|
||||
it("should return expected results for MyChannel/MyTopic", function(){
|
||||
it( "should return expected results for MyChannel/MyTopic", function(){
|
||||
var results = postal.utils.getSubscribersFor({ channel: "MyChannel", topic: "MyTopic" });
|
||||
assert(results.length).equals(10);
|
||||
assert( results.length ).equals( 10 );
|
||||
});
|
||||
it("should return expected results for MyChannel2/MyTopic2", function(){
|
||||
it( "should return expected results for MyChannel2/MyTopic2", function(){
|
||||
var results = postal.utils.getSubscribersFor({ channel: "MyChannel2", topic: "MyTopic2" });
|
||||
assert(results.length).equals(10);
|
||||
assert( results.length ).equals( 10 );
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
// This is the amd module version of postal.diagnostics.js
|
||||
// If you need the standard lib version, go to http://github.com/ifandelse/postal.js
|
||||
define(["postal", "underscore"], function(postal, _, undefined) {
|
||||
define( [ "postal", "underscore" ], function ( postal, _, undefined ) {
|
||||
|
||||
//import("postal.diagnostics.js");
|
||||
//import("postal.diagnostics.js");
|
||||
|
||||
});
|
||||
} );
|
||||
|
|
@ -1,20 +1,79 @@
|
|||
var filters = [],
|
||||
applyFilter = function ( filter, env ) {
|
||||
var match = 0, possible = 0;
|
||||
_.each( filter, function ( item, key ) {
|
||||
if ( env[key] ) {
|
||||
possible++;
|
||||
if ( _.isObject( env[key] ) && !_.isArray( env[key] ) ) {
|
||||
if ( applyFilter( item, env[key] ) ) {
|
||||
match++;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ( _.isEqual( env[key], item ) ) {
|
||||
match++;
|
||||
}
|
||||
}
|
||||
}
|
||||
} );
|
||||
return match === possible;
|
||||
};
|
||||
|
||||
// this returns a callback that, if invoked, removes the wireTap
|
||||
postal.diagnostics = postal.addWireTap(function(data, envelope) {
|
||||
var all = _.extend(envelope, { data: data });
|
||||
if(!JSON) {
|
||||
// this returns a callback that, if invoked, removes the wireTap
|
||||
var wireTap = postal.addWireTap( function ( data, envelope ) {
|
||||
if ( !filters.length || _.any( filters, function ( filter ) {
|
||||
return applyFilter( filter, envelope );
|
||||
} ) ) {
|
||||
if ( !JSON ) {
|
||||
throw "This browser or environment does not provide JSON support";
|
||||
}
|
||||
try {
|
||||
console.log(JSON.stringify(all));
|
||||
console.log( JSON.stringify( envelope ) );
|
||||
}
|
||||
catch(exception) {
|
||||
catch ( exception ) {
|
||||
try {
|
||||
all.data = "ERROR: " + exception.message;
|
||||
console.log(JSON.stringify(all));
|
||||
var env = _.extend( {}, envelope );
|
||||
delete env.data;
|
||||
console.log( JSON.stringify( env ) + "\n\t" + "JSON.stringify Error: " + exception.message );
|
||||
}
|
||||
catch(ex) {
|
||||
console.log("Unable to parse data to JSON: " + exception);
|
||||
catch ( ex ) {
|
||||
console.log( "Unable to parse data to JSON: " + exception );
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
} );
|
||||
|
||||
postal.diagnostics = postal.diagnostics || {};
|
||||
|
||||
postal.diagnostics.console = {
|
||||
clearFilters : function () {
|
||||
filters = [];
|
||||
},
|
||||
removeFilter : function ( filter ) {
|
||||
filters = _.filter( filters, function ( item ) {
|
||||
return !_.isEqual( item, filter );
|
||||
} );
|
||||
},
|
||||
addFilter : function ( constraint ) {
|
||||
if ( !_.isArray( constraint ) ) {
|
||||
constraint = [ constraint ];
|
||||
}
|
||||
_.each( constraint, function( item ){
|
||||
if ( filters.length === 0 || !_.any( filters, function ( filter ) {
|
||||
return _.isEqual( filter, item );
|
||||
} ) ) {
|
||||
filters.push( item );
|
||||
}
|
||||
});
|
||||
|
||||
},
|
||||
getCurrentFilters : function () {
|
||||
return filters;
|
||||
},
|
||||
removeWireTap : function () {
|
||||
if ( wireTap ) {
|
||||
wireTap();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
var _ = require('underscore');
|
||||
|
||||
module.exports = function(postal) {
|
||||
module.exports = function( _, postal ) {
|
||||
//import("postal.diagnostics.js");
|
||||
};
|
||||
|
|
@ -2,6 +2,6 @@
|
|||
// If you need the amd-module style version, go to http://github.com/ifandelse/postal.js
|
||||
(function(postal, _, undefined) {
|
||||
|
||||
//import("postal.diagnostics.js");
|
||||
//import("postal.diagnostics.js");
|
||||
|
||||
})( postal, _ );
|
||||
|
|
@ -46,8 +46,7 @@ var publishPicker = {
|
|||
return new postal.channelTypes[ options.type || "local" ]( channel, topic );
|
||||
}
|
||||
},
|
||||
sessionId = undefined,
|
||||
lastSessionId = undefined;
|
||||
sessionInfo = {};
|
||||
|
||||
// save some setup time, albeit tiny
|
||||
localBus.subscriptions[SYSTEM_CHANNEL] = {};
|
||||
|
|
@ -56,6 +55,13 @@ var postal = {
|
|||
configuration: {
|
||||
bus: localBus,
|
||||
resolver: bindingsResolver,
|
||||
getSessionIdAction: function( callback ) {
|
||||
callback( sessionInfo );
|
||||
},
|
||||
setSessionIdAction: function( info, callback ) {
|
||||
sessionInfo = info;
|
||||
callback( sessionInfo );
|
||||
},
|
||||
DEFAULT_CHANNEL: DEFAULT_CHANNEL,
|
||||
DEFAULT_PRIORITY: DEFAULT_PRIORITY,
|
||||
DEFAULT_DISPOSEAFTER: DEFAULT_DISPOSEAFTER,
|
||||
|
|
@ -123,19 +129,25 @@ var postal = {
|
|||
|
||||
utils: {
|
||||
getSessionId: function( callback ) {
|
||||
callback( sessionId );
|
||||
},
|
||||
|
||||
getLastSessionId: function( callback ) {
|
||||
callback( lastSessionId );
|
||||
postal.configuration.getSessionIdAction.call( this, callback );
|
||||
},
|
||||
|
||||
setSessionId: function( value, callback ) {
|
||||
lastSessionId = sessionId;
|
||||
sessionId = value;
|
||||
if( callback ) {
|
||||
callback();
|
||||
}
|
||||
postal.utils.getSessionId( function( info ) {
|
||||
// get the session info to move id to last id
|
||||
info.lastId = info.id;
|
||||
info.id = value;
|
||||
// invoke the callback the user provided to handle storing session
|
||||
postal.configuration.setSessionIdAction( info, function( session ) {
|
||||
callback( session );
|
||||
// publish postal event msg about the change
|
||||
postal.publish({
|
||||
channel: SYSTEM_CHANNEL,
|
||||
topic: "sessionId.changed",
|
||||
data: session
|
||||
});
|
||||
} );
|
||||
});
|
||||
},
|
||||
|
||||
getSubscribersFor: function() {
|
||||
|
|
@ -159,22 +171,9 @@ var postal = {
|
|||
return result;
|
||||
},
|
||||
|
||||
reset: function( callback ) {
|
||||
// we check first in case a custom bus or resolver
|
||||
// doesn't expose subscriptions or a resolver cache
|
||||
if(postal.configuration.bus.subscriptions) {
|
||||
_.each( postal.configuration.bus.subscriptions , function( channel ){
|
||||
_.each( channel, function( topic ){
|
||||
while( topic.length ) {
|
||||
topic.pop().unsubscribe();
|
||||
}
|
||||
});
|
||||
});
|
||||
postal.configuration.bus.subscriptions = {};
|
||||
}
|
||||
if(postal.configuration.resolver.cache) {
|
||||
postal.configuration.resolver.cache = {};
|
||||
}
|
||||
reset: function() {
|
||||
postal.configuration.bus.reset();
|
||||
postal.configuration.resolver.reset();
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
|
|
@ -17,5 +17,9 @@ var bindingsResolver = {
|
|||
this.cache[topic][binding] = true;
|
||||
}
|
||||
return result;
|
||||
},
|
||||
|
||||
reset: function() {
|
||||
this.cache = {};
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,8 +1,15 @@
|
|||
var localBus = {
|
||||
|
||||
subscriptions: {},
|
||||
|
||||
wireTaps: new Array(0),
|
||||
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) {
|
||||
_.each(this.wireTaps,function(tap) {
|
||||
|
|
@ -23,6 +30,19 @@ var localBus = {
|
|||
});
|
||||
},
|
||||
|
||||
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;
|
||||
|
||||
|
|
@ -48,6 +68,10 @@ var localBus = {
|
|||
return subDef;
|
||||
},
|
||||
|
||||
subscriptions: {},
|
||||
|
||||
wireTaps: new Array(0),
|
||||
|
||||
unsubscribe: function(config) {
|
||||
if(this.subscriptions[config.channel][config.topic]) {
|
||||
var len = this.subscriptions[config.channel][config.topic].length,
|
||||
|
|
@ -59,16 +83,5 @@ 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);
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in a new issue