-!function(e,i){"object"==typeof exports&&"undefined"!=typeof module?i(exports):"function"==typeof define&&define.amd?define(["exports"],i):i((e="undefined"!=typeof globalThis?globalThis:e||self).publisher={})}(this,(function(e){"use strict";const i={async:!0,handleExceptions:!1,lenientUnsubscribe:!1},n=new Map,o=new Map,t=new Map;let s=-1;function r(e,t,s){const r=t===Boolean(t)?t:s,c=void 0===r?i.lenientUnsubscribe:r,a=e=>{const i=n.get(e);if(void 0===i){if(!0===c)return;throw new Error(`Unsubscribe failed. Did not find subscriber for token: ${e}`)}l(i.topic.split("/"),i,o),n.delete(e)};if(void 0===e){if(!0===c)return;throw new Error("Unsubscribe failed. No Arguments specified.")}if(Array.isArray(e))e.forEach((e=>a(e)));else if(!isNaN(parseFloat(e))&&isFinite(e))a(e);else{if(void 0===t){if(!0===c)return;throw new Error(`Unsubscribe failed. No handler for topic based unsubscribe specified ${e}`)}for(const[,i]of n)if(i.handler===t&&i.topic===e){l(e.split("/"),i,o),n.delete(i.token);break}}}function c(e,o,t,s={}){if(!n.has(e.token))return;e.options.invocations>0&&(e.options.invocations-=1,e.options.invocations<=0&&r(e.token));const{handler:c}=e;if(!0!==s.handleExceptions&&!0!==i.handleExceptions)return c(t,o);try{return c(t,o)}catch(e){window.console&&window.console.error&&window.console.error("Exception while executing publish handler: ",e)}}function a(e,n,o={},t,s,r=[]){const c=t.get("subscribers")||new Map,d=t.get("topics");for(const[,e]of c){const{condition:t}=e.options;(void 0===t||"[object Function]"===Object.prototype.toString.call(t)&&!0===t(n,s))&&(e.position=r.push(e),e.priority=e.options.priority||0,e.async=Boolean(o.async||e.options.async||i.async))}if(e.length&&d){const i=d.get(e[0]),t=d.get("*");void 0===t&&void 0===i||(void 0!==t&&a(e.slice(1,e.length),n,o,t,s,r),void 0!==i&&a(e.slice(1,e.length),n,o,i,s,r),e.shift())}return r}function d(e,i,n){const[o]=e;void 0===n.get("topics")&&n.set("topics",new Map);const t=n.get("topics");let s=t.get(o);void 0===s&&(s=new Map,t.set(o,s)),e.length<2?(void 0===s.get("subscribers")&&s.set("subscribers",new Map),s.get("subscribers").set(i.token,i)):(e.shift(),d(e,i,s))}function l(e,i,n){const[o]=e,t=n.get("topics"),s=t.get(o),r=s.get("subscribers");e.length<2?(r.delete(i.token),0===r.size&&s.delete("subscribers")):(e.shift(),l(e,i,s)),s.has("topics")&&0===s.get("topics").size&&s.delete("topics"),0===t.get(o).size&&t.delete(o)}e.configure=function(e){if(!e)throw new Error("Publisher configure: no options specified");void 0!==e.async&&(i.async=e.async),void 0!==e.handleExceptions&&(i.handleExceptions=e.handleExceptions),void 0!==e.lenientUnsubscribe&&(i.lenientUnsubscribe=e.lenientUnsubscribe)},e.publish=function(e,n,s={}){if(!0===s.persist&&t.set(e,{data:n,options:s}),e.indexOf("*")>-1)throw new Error("Publish topic cannot contain any wildcards.");const r=a(e.split("/"),n,s,o,e);let d;r.sort(((e,i)=>e.priority===i.priority?e.position-i.position:e.priority>i.priority?-1:1));const l=void 0===s.async?i.async:s.async;for(;d=r.shift();)if(l)setTimeout(c.bind(null,d,e,n,s),0);else if(!1===c(d,e,n,s)&&!1!==s.cancelable)return!1},e.removePersistentMessage=function(e){t.delete(e)},e.subscribe=function(e,r,a={}){const l=s+=1;if(void 0===e)throw new Error('Subscribe failed - "undefined" Topic.');if(e.includes("undefined"))throw new Error(`Subscribe for '${e}' failed - found 'undefined' in topic, this is almost always an error: ${l}`);if(void 0===r)throw new Error(`Subscribe for '${e}' failed - "undefined" Handler`);const f={token:l,topic:e,handler:r,options:a};if(n.set(l,f),d(e.split("/"),f,o),!0!==a.persist)return l;const p=new RegExp(`^${e.replace("*","(.+)")}(/.+)?$`);for(const[e,n]of t){if(e.match(p)){if(void 0===n.options.async?i.async:n.options.async)setTimeout(c.bind(null,f,e,n.data,a),0);else if(!1===c(f,e,n.data,a)&&!0===a.cancelable)break}}return l},e.unsubscribe=r}));
0 commit comments