text: !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e=e||self).PublishSubscribe={})}(this,(function(e){"use strict";var t="[object AsyncFunction]",n="[object Function]";class Subscription{constructor(e,t,n=!1){this.callback=t,this.channel=e,this.once=n,this.token=function generateToken(){const e=new Array(16);for(let t=0;t<16;t+=1)e[t]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"[Math.floor(62*Math.random())];return e.join("")}()}}function getPrototypeName(e){return Object.prototype.toString.call(e)}function call(e,i){if(e)switch(getPrototypeName(e)){case t:e(...i).then(()=>{}).catch(()=>{});break;case n:try{e(...i)}catch(e){}}}function clone(e){switch(typeOf(e)){case"array":return e.slice();case"object":return Object.assign({},e);case"date":return new e.constructor(Number(e));case"map":return new Map(e);case"set":return new Set(e);case"buffer":return function cloneBuffer(e){const t=e.length,n=Buffer.allocUnsafe?Buffer.allocUnsafe(t):Buffer.from(t);return e.copy(n),n}(e);case"symbol":return function cloneSymbol(e){return Symbol.prototype.valueOf?Object(Symbol.prototype.valueOf.call(e)):{}}(e);case"arraybuffer":return function cloneArrayBuffer(e){const t=new e.constructor(e.byteLength);return new Uint8Array(t).set(new Uint8Array(e)),t}(e);case"float32array":case"float64array":case"int16array":case"int32array":case"int8array":case"uint16array":case"uint32array":case"uint8clampedarray":case"uint8array":return function cloneTypedArray(e){return new e.constructor(e.buffer,e.byteOffset,e.length)}(e);case"regexp":return function cloneRegExp(e){const t=void 0!==e.flags?e.flags:/\w+$/.exec(e)||void 0,n=new e.constructor(e.source,t);return n.lastIndex=e.lastIndex,n}(e);case"error":return Object.create(e);default:return e}}function typeOf(e){if(void 0===e)return"undefined";if(null===e)return"null";if(!0===e||!1===e||e instanceof Boolean)return"boolean";if("string"==typeof e||e instanceof String)return"string";if("number"==typeof e||e instanceof Number)return"number";if("function"==typeof e||e instanceof Function)return"function";if(void 0!==Array.isArray&&Array.isArray(e))return"array";if(e instanceof RegExp)return"regexp";if(e instanceof Date)return"date";var t=toString.call(e);return"[object RegExp]"===t?"regexp":"[object Date]"===t?"date":"[object Arguments]"===t?"arguments":"[object Error]"===t?"error":function isBuffer(e){return null!==e&&Boolean(e.constructor)&&"function"==typeof e.constructor.isBuffer&&e.constructor.isBuffer(e)}(e)?"buffer":"[object Set]"===t?"set":"[object WeakSet]"===t?"weakset":"[object Map]"===t?"map":"[object WeakMap]"===t?"weakmap":"[object Symbol]"===t?"symbol":"[object Int8Array]"===t?"int8array":"[object Uint8Array]"===t?"uint8array":"[object Uint8ClampedArray]"===t?"uint8clampedarray":"[object Int16Array]"===t?"int16array":"[object Uint16Array]"===t?"uint16array":"[object Int32Array]"===t?"int32array":"[object Uint32Array]"===t?"uint32array":"[object Float32Array]"===t?"float32array":"[object Float64Array]"===t?"float64array":"object"}function isObjectObject(e){return!0===function isObject(e){return"object"==typeof e&&null!==e}(e)&&"[object Object]"===Object.prototype.toString.call(e)}function cloneDeep(e,t){switch(typeOf(e)){case"object":return function cloneObjectDeep(e,t){if("function"==typeof t)return t(e);if(t||function isPlainObject(e){let t,n;return!1!==isObjectObject(e)&&(t=e.constructor,"function"==typeof t&&(n=t.prototype,!1!==isObjectObject(n)&&!1!==n.hasOwnProperty("isPrototypeOf")))}(e)){const n=new e.constructor;for(let i in e)n[i]=cloneDeep(e[i],t);return n}return e}(e,t);case"array":return function cloneArrayDeep(e,t){const n=new e.constructor(e.length);for(let i=0;i<e.length;i++)n[i]=cloneDeep(e[i],t);return n}(e,t);default:return clone(e)}}function clone$1(e){return function isPrimitive(e){return Object(e)!==e}(e)?e:cloneDeep(e)}function objectKeys(e){return Object.keys(e)}function isObjectEmpty(e){return 0===function objectSize(e){return objectKeys(e).length}(e)}function isSame(e,t){return Object.is(e,t)}function newObject(){return Object.create(null)}e.PublishSubscribe=class PublishSubscribe{constructor(){this.__channels=new Set,this.__onSubscribe=new Map,this.__onPublish=()=>{},this.__subscriptions=new Map,this.__isValidCallback=function isValidCallback(e){return!!e&&[t,n].includes(getPrototypeName(e))}.bind(this),this.__isValidChannel=function isValidChannel(e){return"string"==typeof e||"symbol"==typeof e||"number"==typeof e&&Number.isFinite(e)}.bind(this),this.__isValidToken=function isValidToken(e){return"string"==typeof e&&16===e.length}.bind(this),this.__createChannel=function createChannel(e){this.hasChannel(e)||(this.__channels.add(e),this.__subscriptions.set(e,newObject()))}.bind(this),this.__createSubscription=function createSubscription(e,t,n){const i=new Subscription(e,t,n);return this.__subscriptions.get(e)[i.token]=i,i.token}.bind(this),this.disableLogging(),this.dropAll(),this.onPublish()}disableLogging(){this.__logging=()=>{},this.__hasLogging=!1}dropAll(){this.__logging("dropAll"),this.__channels.clear(),this.__onSubscribe.clear(),this.__subscriptions.clear()}dropChannel(e){this.__logging("dropChannel",{channel:e}),this.hasChannel(e)&&(this.__channels.delete(e),this.__subscriptions.set(e,newObject()))}getCallback(e){if(!this.__isValidToken(e))return;let t=void 0;return Array.from(this.__subscriptions.values()).some(n=>e in n&&(t=n[e].callback,!0)),t}getChannels(){return Array.from(this.__channels).filter(e=>"symbol"!=typeof e).sort((e,t)=>e.localeCompare(t))}hasChannel(e){return this.__channels.has(e)}hasSubscription(e){return!!this.hasChannel(e)&&(!!this.__subscriptions.has(e)&&!isObjectEmpty(this.__subscriptions.get(e)))}onPublish(e){void 0!==e?this.__isValidCallback(e)&&(this.__onPublish=(t,n)=>{call(e,[t,n])}):this.__onPublish=()=>{}}onSubscribe(e,t,n=!0){if(this.__logging("onSubscribe",{channel:e,data:t}),!this.__isValidChannel(e))throw new TypeError("Channel name should be String, Symbol, Number.");this.__onSubscribe.set(e,{cloneData:n,data:t})}onSubscribeClear(e){if(this.__logging("onSubscribeClear",{channel:e}),!this.__isValidChannel(e))throw new TypeError("Channel name should be String, Symbol, Number.");this.__onSubscribe.delete(e)}publish(e,i,s=!0,r=!1){let a;if(a=s?()=>clone$1(i):()=>i,this.__logging("publish",{channel:e,data:a()}),this.__onPublish(e,a()),r&&this.onSubscribe(e,i,s),!this.hasChannel(e))return;const c=[],o=this.__subscriptions.get(e);objectKeys(o).forEach(i=>{const s=o[i],r=s.callback;switch(getPrototypeName(r)){case t:c.push(new Promise(t=>{let n;this.__hasLogging&&this.__logging("publish -> send",{channel:e,data:a(),token:i}),r(a(),e,i).then(r=>{n={channel:e,result:r,token:i},t(n),this.__hasLogging&&this.__logging("publish -> receive",{channel:e,result:clone$1(r),token:i}),s.once&&delete o[i]}).catch(r=>{n={channel:e,result:r,token:i},t(n),this.__hasLogging&&this.__logging("publish -> receive",{channel:e,result:r,token:i}),s.once&&delete o[i]})}));break;case n:c.push(new Promise(t=>{let n;this.__hasLogging&&this.__logging("publish -> send",{channel:e,data:a(),token:i});try{n=r(a(),e,i)}catch(e){n=e}t({channel:e,result:n,token:i}),this.__hasLogging&&this.__logging("publish -> receive",{channel:e,result:clone$1(n),token:i}),s.once&&delete o[i]}))}}),Promise.all(c).then(()=>{})}publishAsync(e,i,s=!0,r=!0,a=!1){let c;if(c=r?()=>clone$1(i):()=>i,this.__hasLogging&&this.__logging("publishAsync",{channel:e,data:c()}),this.__onPublish(e,c()),a&&this.onSubscribe(e,i,r),!this.hasChannel(e))return Promise.all([]);const o=[],l=this.__subscriptions.get(e);return objectKeys(l).forEach(i=>{const s=l[i],r=s.callback;switch(getPrototypeName(r)){case t:o.push(new Promise(t=>{let n;this.__hasLogging&&this.__logging("publishAsync -> send",{channel:e,data:c(),token:i}),r(c(),e,i).then(r=>{n={channel:e,result:r,token:i},t(n),this.__hasLogging&&this.__logging("publishAsync -> receive",{channel:e,result:clone$1(r),token:i}),s.once&&delete l[i]}).catch(r=>{n={channel:e,result:r,token:i},t(n),this.__hasLogging&&this.__logging("publishAsync -> receive",{channel:e,result:r,token:i}),s.once&&delete l[i]})}));break;case n:o.push(new Promise(t=>{let n;this.__hasLogging&&this.__logging("publishAsync -> send",{channel:e,data:c(),token:i});try{n=r(c(),e,i)}catch(e){n=e}t({channel:e,result:n,token:i}),this.__hasLogging&&this.__logging("publishAsync -> receive",{channel:e,result:clone$1(n),token:i}),s.once&&delete l[i]}))}}),s?Promise.all(o).then(e=>e.map(e=>e.result)):Promise.all(o)}publishSync(e,t,i=!0,s=!0,r,a=!1){let c,o;if(c=s?()=>clone$1(t):()=>t,this.__hasLogging&&this.__logging("publishSync",{channel:e,data:c()}),this.__onPublish(e,c()),o=r&&this.__isValidCallback(r)?e=>call(r,[e]):()=>{},a&&this.onSubscribe(e,t,s),!this.hasChannel(e))return o([]),[];const l=this.__subscriptions.get(e);let h=[];return objectKeys(l).forEach(t=>{const i=l[t],s=i.callback;if(getPrototypeName(s)!==n)return;let r;this.__hasLogging&&this.__logging("publishSync -> send",{channel:e,data:c(),token:t});try{r=s(c(),e,t)}catch(e){r=e}h.push({channel:e,result:r,token:t}),this.__hasLogging&&this.__logging("publishSync -> receive",{channel:e,result:clone$1(r),token:t}),i.once&&delete l[t]}),i&&(h=h.map(e=>e.result)),o(h),h}setLogging(e){if(this.__isValidCallback(e))switch(getPrototypeName(e)){case t:this.__logging=(t,n)=>{e({...n,method:t}).then(()=>{}).catch(()=>{})},this.__hasLogging=!0;break;case n:this.__logging=(t,n)=>{try{e({...n,method:t})}catch(e){}},this.__hasLogging=!0}}subscribe(e,t,n=!1){if(this.__logging("subscribe",{channel:e,callback:t,once:n}),!this.__isValidChannel(e))return new TypeError("Channel name should be String, Symbol, Number.");if(!this.__isValidCallback(t))return new TypeError("Callback should be a function.");this.__createChannel(e);const i=this.__createSubscription(e,t,n);if(this.__onSubscribe.has(e)){let{cloneData:t,data:n}=this.__onSubscribe.get(e);this.publish(e,n,t)}return i}subscribeOnce(e,t){return this.__logging("subscribeOnce",{channel:e,callback:t}),this.__isValidChannel(e)?this.__isValidCallback(t)?(this.__createChannel(e),this.__createSubscription(e,t)):new TypeError("Callback should be a function."):new TypeError("Channel name should be String, Symbol, Number.")}unsubscribe(e,t){return void 0!==t?this.unsubscribeByChannelAndCallback(e,t):this.__isValidCallback(e)?this.unsubscribeByCallback(e):this.__isValidToken(e)?this.unsubscribeByToken(e):0}unsubscribeByCallback(e){if(this.__logging("unsubscribeByCallback",{callback:e}),!this.__isValidCallback(e))return 0;let t=0;return this.__subscriptions.forEach(n=>{objectKeys(n).forEach(i=>{isSame(n[i].callback,e)&&(delete n[i],t+=1)})}),t}unsubscribeByChannelAndCallback(e,t){if(this.__logging("unsubscribeByChannelAndCallback",{callback:t}),!this.hasSubscription(e))return 0;const n=this.__subscriptions.get(e);let i=0;return objectKeys(n).forEach(e=>{isSame(n[e].callback,t)&&(delete n[e],i+=1)}),i}unsubscribeByToken(e){if(this.__logging("unsubscribeByToken",{token:e}),!this.__isValidToken(e))return 0;let t=0;return this.__subscriptions.forEach(n=>{e in n&&(delete n[e],t+=1)}),t}},Object.defineProperty(e,"__esModule",{value:!0})}));
  //# sourceMappingURL=publish_subscribe.js.map
  