| 12345678 |
- /**
- * 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.5
- * 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<c?i[o].call(r,e,t,u):(n.callback.call(r,e,t),s=!0)};a(e,t,0)}else n.callback.call(r,e,t),s=!0;return s}},logError:function(){if(console){var e;e=console.warn?console.warn:console.log,this["catch"](e)}return this},once:function(){return this.disposeAfter(1)},subscribe:function(e){return this.callback=e,this},unsubscribe:function(){this.inactive||l.unsubscribe(this)},constraint:function(e){if("function"!=typeof e)throw new Error("Predicate constraint must be a function");return this.pipeline.push(function(t,n,i){e.call(this,t,n)&&i(t,n)}),this},constraints:function(t){var n=this;return e.each(t,function(e){n.constraint(e)}),n},context:function(e){return this._context=e,this},debounce:function(t,n){if("number"!=typeof t)throw new Error("Milliseconds must be a number");var i={};return!!n==!0&&(i.leading=!0,i.trailing=!1),this.pipeline.push(e.debounce(function(e,t,n){n(e,t)},t,i)),this},delay:function(e){if("number"!=typeof e)throw new Error("Milliseconds must be a number");var t=this;return t.pipeline.push(function(t,n,i){setTimeout(function(){i(t,n)},e)}),this},throttle:function(t){if("number"!=typeof t)throw new Error("Milliseconds must be a number");var n=function(e,t,n){n(e,t)};return this.pipeline.push(e.throttle(n,t)),this}};var y=(f.resolver={cache:{},regex:{},enableCache:!0,compare:function(t,n,i){var c,r,o,s=n+f.cacheKeyDelimiter+t,a=this.cache[s],u=i||{},h=this.enableCache&&!u.resolverNoCache;return a===!0?a:t.indexOf("#")===-1&&t.indexOf("*")===-1?(a=n===t,h&&(this.cache[s]=a),a):((r=this.regex[t])||(c="^"+e.map(t.split("."),function(e){var t="";return o&&(t="#"!==o?"\\.\\b":"\\b"),t+="#"===e?"[\\s\\S]*":"*"===e?"[^.]+":e,o=e,t}).join("")+"$",r=this.regex[t]=new RegExp(c)),a=r.test(n),h&&(this.cache[s]=a),a)},reset:function(){this.cache={},this.regex={}},purge:function(t){var n=this,i=f.cacheKeyDelimiter,c=function(e,c){var r=c.split(i),o=r[0],s=r[1];"undefined"!=typeof t.topic&&t.topic!==o||"undefined"!=typeof t.binding&&t.binding!==s||delete n.cache[c]},r=function(e,t){var c=t.split(i);0===l.getSubscribersFor({topic:c[0]}).length&&delete n.cache[t]};if("undefined"==typeof t)this.reset();else{var o=t.compact===!0?r:c;e.each(this.cache,o)}}},0),m=[],g=0,w=o.bind(n,"created"),_=o.bind(n,"removed");if(e.extend(l,{cache:{},subscriptions:{},wireTaps:[],ChannelDefinition:p,SubscriptionDefinition:b,channel:function(e){return new p(e,this)},addWireTap:function(e){var t=this;return t.wireTaps.push(e),function(){var n=t.wireTaps.indexOf(e);n!==-1&&t.wireTaps.splice(n,1)}},noConflict:function(){if("undefined"==typeof window||"undefined"!=typeof window&&"function"==typeof define&&define.amd)throw new Error("noConflict can only be used in browser clients which aren't using AMD modules");return t.postal=a,this},getSubscribersFor:function(t){var n=[],i=this;return e.each(i.subscriptions,function(i){e.each(i,function(i){n=n.concat(e.filter(i,s(t,f.resolver)))})}),n},publish:function(t,n){++y;var c=t.channel=t.channel||f.DEFAULT_CHANNEL,o=t.topic;t.timeStamp=new Date,this.wireTaps.length&&e.each(this.wireTaps,function(e){e(t.data,t,y)});var s=c+f.cacheKeyDelimiter+o,a=this.cache[s],u=0,h=0;if(a)e.each(a,function(e){e.invokeSubscriber(t.data,t)?h++:u++});else{var l=r(o,this.cache,s,function(e){e.invokeSubscriber(t.data,t)?h++:u++},t);e.each(this.subscriptions[c],function(t){e.each(t,l)})}0===--y&&i(),n&&n({activated:h,skipped:u})},reset:function(){this.unsubscribeFor(),f.resolver.reset(),this.subscriptions={},this.cache={}},subscribe:function(t){var n,i=this.subscriptions,c=new b(t.channel||f.DEFAULT_CHANNEL,t.topic,t.callback),o=i[c.channel],s=c.channel.length;o||(o=i[c.channel]={}),n=i[c.channel][c.topic],n||(n=i[c.channel][c.topic]=[]),n.push(c);var a=this.cache;return e.each(e.keys(a),function(e){e.substr(0,s)===c.channel&&r(e.split(f.cacheKeyDelimiter)[1],a,e)(c)}),f.enableSystemMessages&&this.publish(w(c)),c},unsubscribe:function(){for(var t,n,i,r,o=arguments.length,s=0;s<o;s++){if(t=arguments[s],t.inactive=!0,y)return void m.push(t);if(n=this.subscriptions[t.channel],i=n&&n[t.topic]){var a=i.length;for(r=0;r<a;){if(i[r]===t){i.splice(r,1);break}r+=1}if(0===i.length&&(delete n[t.topic],e.keys(n).length||delete this.subscriptions[t.channel]),t.cacheKeys&&t.cacheKeys.length)for(var u;u=t.cacheKeys.pop();)e.each(this.cache[u],c(t,u,this.cache));if("function"==typeof f.resolver.purge){var h=f.autoCompactResolver===!0?0:"number"==typeof f.autoCompactResolver&&f.autoCompactResolver-1;h>=0&&g===h?(f.resolver.purge({compact:!0}),g=0):h>=0&&g<h&&(g+=1)}}f.enableSystemMessages&&this.publish(_(t))}},unsubscribeFor:function(e){var t=[];this.subscriptions&&(t=this.getSubscribersFor(e),this.unsubscribe.apply(this,t))}}),t&&Object.prototype.hasOwnProperty.call(t,"__postalReady__")&&e.isArray(t.__postalReady__))for(;t.__postalReady__.length;)t.__postalReady__.shift().onReady(l);return l});
|