-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmsngr.min.js
1 lines (1 loc) · 11.1 KB
/
msngr.min.js
1
var msngr=msngr||function(){"use strict";var e={},t=function(){var e=Array.prototype.slice.call(arguments,0);return t.message.apply(this,e)};return t.version="7.0.0",t.extend=function(r){if(void 0!==r&&null!==r&&"[object Function]"===Object.prototype.toString.call(r))return r.apply(this,[t,e])},Object.defineProperty(t,"debug",{set:function(r){!0===r?t.internal=e:!1===r&&delete t.internal},get:function(){return void 0!==t.internal}}),t}();msngr.extend(function(e,t){"use strict";var r={arguments:"[object Arguments]",boolean:"[object Boolean]",string:"[object String]",date:"[object Date]",array:"[object Array]",number:"[object Number]",object:"[object Object]",function:"[object Function]",undefined:"[object Undefined]",null:"[object Null]",symbol:"[object Symbol]",nodeList:"[object NodeList]"},n=function(e){return Object.prototype.toString.call(e)},o={htmlElement:function(e){return 0===e.indexOf("[object HTML")||0===e.indexOf("[object global]")},promise:function(e,t){return"[object Promise]"===e||(e===r.object||e===r.function)&&(void 0!==item.then&&n(item.then)===r.function)}},i=function(e,t,i){return i?o[e](n(t),t):n(t)===r[e]},a=function(e,t){switch(e){case r.undefined:case r.null:return!0;case r.string:return 0===t.trim().length;case r.object:return 0===Object.keys(t).length;case r.array:return 0===t.length;default:return!1}},c=function(e){var t={},c=function(r,n){for(var o in r)r.hasOwnProperty(o)&&function(r){Object.defineProperty(t,r,{get:function(){for(var t=0;t<e.length;++t)if(!i(r,e[t],n))return!1;return!0}})}(o)};return c(r,!1),c(o,!0),Object.defineProperty(t,"there",{get:function(){for(var t=0;t<e.length;++t)if(void 0===e[t]||null===e[t])return!1;return!0}}),Object.defineProperty(t,"empty",{get:function(){for(var t=0;t<e.length;++t)if(!a(n(e[t]),e[t]))return!1;return!0}}),t};t.types=r,e.is=function(){var e=Array.prototype.slice.call(arguments,0),t=c(e=e||[void 0]);return t.getType=function(){return Object.prototype.toString.call(e[0])},t.getTypes=function(){for(var t=[],r=0;r<e.length;++r)t.push(Object.prototype.toString.call(e[r]));return t},t},e.is.browser="undefined"!=typeof XMLHttpRequest}),msngr.extend(function(e){"use strict";var t=0,r="Mxxx".replace(/[x]/g,function(){return Math.floor(100*Math.random())});e.id=function(){return++t,r+t},e.uuid=function(){var t=e.now();return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(e){var r=(t+16*Math.random())%16|0;return t=Math.floor(t/16),("x"==e?r:3&r|8).toString(16)})}}),msngr.extend(function(e,t){"use strict";var r;if("undefined"!=typeof window&&void 0!==window.postMessage){var n=[];window.addEventListener("message",function(e){e.source===window&&"__msngr_immediate"===e.data&&(e.stopPropagation(),n.length>0&&n.shift()())},!0)}e.immediate=function(e){void 0===r&&(r="undefined"!=typeof setImmediate?setImmediate:"undefined"!=typeof window&&void 0!==window.postMessage?function(e){n.push(e),window.postMessage("__msngr_immediate","*")}:function(e){setTimeout(e,0)}),r(e)}}),msngr.extend(function(e){"use strict";var t=void 0,r=void 0,n=function(){return performance.now()},o=function(){return process.hrtime()[1]/1e6},i=function(){return Date.now()};e.now=function(a){void 0===t&&(t="undefined"!=typeof performance?n:"undefined"!=typeof process?o:i);var c=t();return!0===a&&r===c?e.now(a):(r=c,c)}}),msngr.extend(function(e,t){"use strict";var r={};r[t.types.string]=function(e){return e},r[t.types.number]=function(e){return e},r[t.types.boolean]=function(e){return e},r[t.types.date]=function(e){return new Date(e)},r[t.types.object]=function(t){var r={};for(var n in t)t.hasOwnProperty(n)&&(r[n]=e.copy(t[n]));return r},r[t.types.array]=function(t){for(var r=[],n=0;n<t.length;n++)r[n]=e.copy(t[n]);return r},r[t.types.function]=function(t){var r=t.bind({});for(var n in t)t.hasOwnProperty(n)&&(r[n]=e.copy(t[n]));return r},e.copy=function(t){if(void 0===t||null===t)return t;var n=e.is(t).getType();return void 0!==r[n]?r[n](t):t}}),msngr.extend(function(e,t){"use strict";var r=[t.types.object,t.types.function,t.types.array],n=[t.types.object,t.types.array],o=function(i,a,c){if(void 0===i||null===i)return a;if(void 0===a||null===a)return i;var u,s=e.is(i).getType(),f=e.is(a).getType();if(-1!==r.indexOf(s)&&-1!==n.indexOf(f)||(u="msngr.merge() - Only objects, arrays or a single function followed by objects can be merged!"),-1===[s,f].indexOf(t.types.array)||s===t.types.array&&f===t.types.array||(u="msngr.merge() - Arrays cannot be merged with objects or functions!"),!0===c)return a;if(u)throw new Error(u);var l=i;if(s===t.types.array&&f===t.types.array)return i.concat(a);for(var p in a)if(a.hasOwnProperty(p)){var d=e.is(a[p]);d.object?(l[p]=l[p]||{},l[p]=o(l[p],a[p])):d.array?(l[p]=l[p]||[],l[p]=l[p].concat(a[p])):l[p]=a[p]}return l};t.merge=function(){var e=Array.prototype.slice.call(arguments,0);if(e.length<=1)return e[0];for(var t=e.shift();e.length>0;)t=o(t,e.shift(),!0);return t},e.merge=function(){var e=Array.prototype.slice.call(arguments,0);if(e.length<=1)return e[0];for(var t=e.shift();e.length>0;)t=o(t,e.shift());return t}}),msngr.extend(function(e,t){"use strict";t.executer=function(t){for(var r=0;r<t.length;++r)e.is(t[r]).function&&(t[r]={method:t[r]});var n=function(t,r,n,o){var i=e.is(r);e.immediate(function(){var e=!1;i.array||(r=i.there?[r]:[]),r.push(function(){return e=!0,function(e){o.apply(n,[e])}});var a=t.apply(n||void 0,r);!0!==e&&o.apply(n,[a])})};return{parallel:function(r){var o=e.is(r),i=[],a=0;if(0===t.length&&o.there)return r.apply(null,[[]]);for(var c=0;c<t.length;++c){var u=t[c].method,s=t[c].params;!function(c,f,l){n(u,s,l,function(n){e.is(n).there&&i.push(n),++a===t.length&&o.there&&r.apply(l||null,[i])})}(0,0,t[c].context)}},series:function(r){var o=e.is(r),i=[];if(0===t.length&&o.there)return r.apply(null,[[]]);var a=function(){var c=t.shift();!function(c,u,s){n(c,u,s,function(n){e.is(n).there&&i.push(n),0===t.length&&o.there?r.apply(s||null,[i]):a()})}(c.method,c.params,c.context)};a()}}},e.parallel=function(e,r){t.executer(e).parallel.apply(this,[r])},e.series=function(e,r){t.executer(e).series.apply(this,[r])}}),msngr.extend(function(e,t){"use strict";var r=function(e){return void 0!==e&&null!==e},n=function(e,t){var r=e.indexOf(t),n=e.length-1;if(r!==n){var o=e[n];e[n]=e[r],e[r]=o}e.pop()};t.memory=function(){var t={},o={},i=0,a={index:function(n){if(r(n)&&r(n.topic)){var a=e.id();return t[a]=e.copy(n),r(o[n.topic])||(o[n.topic]={ids:[],category:{}}),r(o[n.topic].category[n.category])||(o[n.topic].category[n.category]={ids:[],subcategory:{}}),r(o[n.topic].category[n.category].subcategory[n.subcategory])||(o[n.topic].category[n.category].subcategory[n.subcategory]={ids:[]}),r(n.category)||r(n.subcategory)||o[n.topic].ids.push(a),r(n.category)&&!r(n.subcategory)&&o[n.topic].category[n.category].ids.push(a),r(n.category)&&r(n.subcategory)&&o[n.topic].category[n.category].subcategory[n.subcategory].ids.push(a),i++,a}},getById:function(e){return t[e]},delete:function(e){if(r(e)&&r(t[e])){var a=t[e];return n(o[a.topic].ids,e),n(o[a.topic].category[a.category].ids,e),n(o[a.topic].category[a.category].subcategory[a.subcategory].ids,e),delete t[e],i--,!0}return!1},query:function(e){var t=[];if(r(e)&&r(e.topic)&&r(o[e.topic])){var n=o[e.topic],i=((n||{}).category||{})[e.category],a=((i||{}).subcategory||{})[e.subcategory];t=(t=(t=t.concat(n.ids||[])).concat((i||{}).ids||[])).concat((a||{}).ids||[])}for(var c={},u=[],s=t.length,f=0;f<s;++f)void 0===c[t[f]]&&(c[t[f]]=!0,u.push(t[f]));return u},clear:function(){return t={},o={},i=0,!0}};return Object.defineProperty(a,"count",{get:function(){return i}}),a}}),msngr.extend(function(e,t){"use strict";var r=t.memory(),n=t.memory(),o={},i=0,a={},c=0,u={},s=[];Object.defineProperty(t,"handlerCount",{get:function(){return i}}),Object.defineProperty(t,"payloadCount",{get:function(){return c}}),t.reset=function(){o={},i=0,r.clear(),n.clear(),a={},c=0,u={},s=[]};var f=function(t){var r=n.query(t);if(0!==r.length){var o=a[r[0]];if(r.length>1)for(var i=1;i<r.length;++i)o=e.merge(innerPay,fpay);return o}},l=function(e,t,r){for(var n=[],o=e||[],i=0;i<s.length;++i)-1===o.indexOf(s[i])&&o.push(s[i]);for(i=0;i<o.length;++i)void 0!==u[o[i]]&&n.push({method:u[o[i]],params:[t,r]});return n};t.getMiddlewares=l;var p=function(e,r,n,o){var i=l(e,r,n);t.executer(i).series(function(e){return o(t.merge.apply(this,[r].concat(e)))})},d=function(e,t,r,n){p(e,t,r,function(e){n.apply(void 0,[e])})},g=function(n,i,a){var c=e.is(n).array?n:r.query(n);if(c.length>0){for(var u=[],s=[],f=0;f<c.length;++f){var l=e.is(n).object?e.copy(n):e.copy(r.query(c[f])),p=o[c[f]];u.push({method:p.handler,params:[i,l]}),!0===p.once&&s.push({msg:l,handler:p.handler})}for(var d=t.executer(u),f=0;f<s.length;++f)e(s[f].msg).drop(s[f].handler);d.parallel(a)}};e.message=function(t,u,l){var p=e.is(t),y=e.is(u),v=e.is(l);if(!p.there)throw new Error("msngr() - Invalid parameters");if(!p.object&&!p.string)throw new Error("msngr() - Invalid parameters");if(p.empty)throw new Error("msngr() - Invalid parameters");var h;p.object?h=e.copy(t):((h={}).topic=t,!y.empty&&y.string&&(h.category=u),!v.empty&&v.string&&(h.subcategory=l));for(var m in h)h.hasOwnProperty(m)&&(h[m]=h[m].toLowerCase());var b=[],x={use:function(t){if(e.is(t).string){var r=t.toLowerCase();-1===b.indexOf(r)&&b.push(t.toLowerCase())}return x},emit:function(t,r){return e.is(t).function&&(r=t,t=void 0),b.length>0||s.length>0?d(b,t,h,function(e){g(h,e,r)}):g(h,t,r),x},persist:function(t){void 0===t&&(t=null);var r=n.query(h);if(0===r.length){var o=n.index(h);a[o]=t,r=[o]}else for(var i=0;i<r.length;++i)a[r[i]]=e.merge(t,a[r[i]]);return++c,x.emit(f(h))},cease:function(){for(var e=n.query(h),t=0;t<e.length;++t)delete a[e[t]],--c;return x},on:function(e){var t=r.index(h);o[t]={handler:e,context:h.context||this,once:!1},i++;var n=f(h);return void 0!==n&&(b.length>0||s.length>0?d(b,n,h,function(e){g([t],e)}):g([t],n)),x},once:function(e){var t=r.index(h);o[t]={handler:e,context:h.context||this,once:!0},i++;var n=f(h);return void 0!==n&&(b.length>0||s.length>0?d(b,n,h,function(e){g([t],e)}):g([t],n)),x},drop:function(e){var t=r.query(h);if(t.length>0)for(var n=0;n<t.length;++n)o[t[n]].handler===e&&(delete o[t[n]],i--,r.delete(t[n]));return x},dropAll:function(){var e=r.query(h);if(e.length>0)for(var t=0;t<e.length;++t)delete o[e[t]],i--,r.delete(e[t]);return x}};return Object.defineProperty(x,"message",{get:function(){return h}}),Object.defineProperty(x,"topic",{get:function(){return h.topic}}),Object.defineProperty(x,"category",{get:function(){return h.category}}),Object.defineProperty(x,"subcategory",{get:function(){return h.subcategory}}),Object.defineProperty(x,"handlers",{get:function(){return r.query(h).length}}),x},e.middleware=function(t,r,n){var o=e.is(t),i=e.is(r);if(!o.there||!o.string||o.empty||!i.there||!i.function)throw new Error("msngr.middleware() - Invalid parameters");if(e.is(u[t]).there)throw new Error("msngr.middleware() - Invalid parameters");var a=t.toLowerCase();u[a]=r,!0===n&&s.push(a)},e.unmiddleware=function(t){var r=e.is(t);if(!r.there||!r.string||r.empty)throw new Error("msngr.unmiddleware() - Invalid parameters");var n=t.toLowerCase(),o=s.indexOf(n);-1!==o&&s.splice(o,1),void 0!==u[n]&&delete u[n]}}),"undefined"!=typeof module&&void 0!==module.exports&&(module.exports=msngr);