/** * postal - Pub/Sub library providing wildcard subscriptions, complex message handling, etc. Works server and client-side. * Author: Jim Cowart (http://ifandelse.com) * Version: v2.0.6 * Url: http://github.com/postaljs/postal.js * License(s): MIT */ (function(e,t){"function"==typeof define&&define.amd?define(["lodash"],function(n){return t(n,e)}):"object"==typeof module&&module.exports?module.exports=t(require("lodash"),this):e.postal=t(e._,e)})(this,function(e,t,n){function i(){for(;m.length;)l.unsubscribe(m.shift())}function c(e,t,n){return function(i,c,r){i===e&&r.splice(c,1),0===r.length&&delete n[t]}}function r(e,t,n,i,c){var r=c&&c.headers||{};return function(c){var o;f.resolver.compare(c.topic,e,r)&&(r.resolverNoCache||(o=t[n]=t[n]||[],o.push(c),c.cacheKeys.push(n)),i&&i(c))}}function o(e,t){return{channel:f.SYSTEM_CHANNEL,topic:"subscription."+e,data:{event:"subscription."+e,channel:t.channel,topic:t.topic}}}function s(t,n){return"function"==typeof t?t:t?function(i){var c=0,r=0;return e.each(t,function(e,o){c+=1,("topic"===o&&n.compare(i.topic,t.topic,{resolverNoCache:!0})||"context"===o&&t.context===i._context||i[o]===t[o])&&(r+=1)}),c===r}:function(){return!0}}var a=t&&t.postal,u=t&&t._;u&&u!==e&&(e=e.noConflict());var h={DEFAULT_CHANNEL:"/",SYSTEM_CHANNEL:"postal",enableSystemMessages:!0,cacheKeyDelimiter:"|",autoCompactResolver:!1},l={configuration:e.extend({},h)},f=l.configuration,p=function(e,t){this.bus=t,this.channel=e||f.DEFAULT_CHANNEL};p.prototype.subscribe=function(){return this.bus.subscribe({channel:this.channel,topic:1===arguments.length?arguments[0].topic:arguments[0],callback:1===arguments.length?arguments[0].callback:arguments[1]})},p.prototype.publish=function(){var t,n={};if("string"==typeof arguments[0]?(n.topic=arguments[0],n.data=arguments[1],t=arguments[2]):(n=arguments[0],t=arguments[1]),"object"!=typeof n)throw new Error("The first argument to ChannelDefinition.publish should be either an envelope object or a string topic.");n.headers=e.extend(n.headers||{resolverNoCache:f.resolverNoCache}),n.channel=this.channel,this.bus.publish(n,t)};var b=function(e,t,i){if(3!==arguments.length)throw new Error("You must provide a channel, topic and callback when creating a SubscriptionDefinition instance.");if(0===t.length)throw new Error("Topics cannot be empty");this.channel=e,this.topic=t,this.callback=i,this.pipeline=[],this.cacheKeys=[],this._context=n},d=function(){var t;return function(n){var i=!1;return"string"==typeof n?(i=n===t,t=n):(i=e.isEqual(n,t),t=e.extend({},n)),!i}},v=function(){var t=[];return function(n){var i=!e.some(t,function(t){return e.isEqual(n,t)});return i&&t.push(n),i}};b.prototype={"catch":function(e){var t=this.callback,n=function(){try{t.apply(this,arguments)}catch(n){e(n,arguments[0])}};return this.callback=n,this},defer:function(){return this.delay(0)},disposeAfter:function(t){if("number"!=typeof t||t<=0)throw new Error("The value provided to disposeAfter (maxCalls) must be a number greater than zero.");var n=e.after(t,this.unsubscribe.bind(this));return this.pipeline.push(function(e,t,i){i(e,t),n()}),this},distinct:function(){return this.constraint(new v)},distinctUntilChanged:function(){return this.constraint(new d)},invokeSubscriber:function(e,t){if(!this.inactive){var n=this,i=n.pipeline,c=i.length,r=n._context,o=-1,s=!1;if(c){i=i.concat([n.callback]);var a=function u(e,t){o+=1,o=0&&g===h?(f.resolver.purge({compact:!0}),g=0):h>=0&&g