UNPKG

u-node-mq

Version:

基于发布订阅模型的消息通信插件

3 lines (2 loc) 15.7 kB
var h=(s,e,t)=>new Promise((n,o)=>{var r=a=>{try{u(t.next(a))}catch(l){o(l)}},i=a=>{try{u(t.throw(a))}catch(l){o(l)}},u=a=>a.done?n(a.value):Promise.resolve(a.value).then(r,i);u((t=t.apply(s,e)).next())});var f=()=>String(Math.round(Math.random()*1e10));var ne=Array.isArray;var c=s=>typeof s=="function",N=s=>typeof s=="string";var I=s=>s!==null&&typeof s=="object",S=s=>I(s)&&c(s.then)&&c(s.catch);var w=class{constructor(){this.exchangeCollection=new Map}has(e){return!!this.exchangeCollection.has(e)}setExchangeCollection(e){this.exchangeCollection=new Map(Object.entries(e))}addExchage(e,t){return t.name=e,this.exchangeCollection.set(e,t)}getExchange(e){let t=this.exchangeCollection.get(e);return t===void 0?null:t}getExchangeList(){return[...this.exchangeCollection.values()]}getQueueNameList(e,t){return h(this,null,function*(){let n=this.getExchange(e);return n===null?[]:n.getQueueNameList(t)})}};var Q=class{constructor(){this.queueCollection=new Map}has(e){return!!this.queueCollection.has(e)}setQueueCollection(e){this.queueCollection=new Map(Object.entries(e))}getQueue(e){let t=this.queueCollection.get(e);return t===void 0?null:t}getQueueList(){return[...this.queueCollection.values()]}addQueue(e,t){return t.name=e,this.queueCollection.set(e,t)}pushNewsToQueue(e,t){var n;(n=this.getQueue(e))==null||n.pushNews(t)}pushContentToQueue(e,t){var n;(n=this.getQueue(e))==null||n.pushContent(t)}subscribeQueue(e,t,n){var o;(o=this.getQueue(e))==null||o.pushConsume(t,n)}unsubscribeQueue(e,t){var n,o;return this.has(e)?c(t)?!!((n=this.getQueue(e))!=null&&n.removeConsumer(t)):!!((o=this.getQueue(e))!=null&&o.removeAllConsumer()):!1}};var D=class{constructor(e,t){this.exchangeCollectionHandle=new w;this.queueCollectionHandle=new Q;for(let n in e)e[n].name=n;for(let n in t)t[n].name=n;this.exchangeCollectionHandle.setExchangeCollection(e),this.queueCollectionHandle.setQueueCollection(t)}getExchange(e){return this.exchangeCollectionHandle.getExchange(e)}getExchangeList(){return this.exchangeCollectionHandle.getExchangeList()}addExchage(e,t){return this.exchangeCollectionHandle.addExchage(e,t)}getQueue(e){return this.queueCollectionHandle.getQueue(e)}getQueueList(){return this.queueCollectionHandle.getQueueList()}addQueue(e,t){return this.queueCollectionHandle.addQueue(e,t)}pushNewsListToExchange(e,...t){for(let n of t)this.exchangeCollectionHandle.getQueueNameList(e,n.content).then(o=>{for(let r in o)this.pushNewsListToQueue(r,n)})}pushNewsListToQueue(e,...t){for(let n of t)this.queueCollectionHandle.pushNewsToQueue(e,n)}pushContentListToExchange(e,...t){for(let n of t)this.exchangeCollectionHandle.getQueueNameList(e,n).then(o=>{for(let r of o)this.pushContentListToQueue(r,n)})}pushContentListToQueue(e,...t){for(let n of t)this.queueCollectionHandle.pushContentToQueue(e,n)}subscribeQueue(e,t,n){this.queueCollectionHandle.subscribeQueue(e,t,n)}unsubscribeQueue(e,t){this.queueCollectionHandle.unsubscribeQueue(e,t)}};function J(s,e){return new p(s,e)}var p=class extends D{constructor(t,n){super(t,n);this.installedPlugins=new Set}use(t,...n){return this.installedPlugins.has(t)?console.log("Plugin has already been applied to target unmq."):t&&c(t.install)?(this.installedPlugins.add(t),t.install(this,...n)):c(t)&&(this.installedPlugins.add(t),t(this,...n)),this}emit(t,...n){return super.pushContentListToExchange(t,...n),this}emitToQueue(t,...n){return super.pushContentListToQueue(t,...n),this}on(t,n,o){return super.subscribeQueue(t,n,o),()=>this.off(t,n)}off(t,n){return super.unsubscribeQueue(t,n),this}once(t,n,o){if(c(n)){let r=(i,u,a)=>(this.off(t,r),n(i,u,a));return this.on(t,r,o),this}else return new Promise(r=>{let i=u=>(this.off(t,i),r(u),!0);this.on(t,i,o)})}};function L(s){return new E(s)}var E=class{constructor(e){e instanceof d?this.queue=e:this.queue=new d(e)}emit(...e){for(let t of e)this.queue.pushContent(t);return this}on(e,t){return this.queue.pushConsume(e,t),()=>this.off(e)}off(e){return c(e)?this.queue.removeConsumer(e):this.queue.removeAllConsumer(),this}once(e,t){if(c(e)){let n=(o,r,i)=>(this.off(n),e(o,r,i));return this.on(n,t),this}else return new Promise(n=>{let o=r=>(this.off(o),n(r),!0);this.on(o,t)})}add(...e){return this.queue.add(...e),this}fork(e){let t=L(e);return this.on(n=>(t.emit(n),!0)),t}};function V(s,e){return new b(s,e)}var b=class{constructor(e,t){this.installedPlugins=new Set;e instanceof g?this.exchange=e:this.exchange=new g(e);for(let n in t)t[n].name=n;this.queueCollection=t}use(e,...t){return this.installedPlugins.has(e)?console.log("Plugin has already been applied to target unmq."):e&&c(e.install)?(this.installedPlugins.add(e),e.install(this,...t)):c(e)&&(this.installedPlugins.add(e),e(this,...t)),this}emit(...e){for(let t of e)this.exchange.getQueueNameList(t).then(n=>{for(let o of n)this.queueCollection[o]!==void 0&&this.queueCollection[o].pushContent(t)});return this}emitToQueue(e,...t){for(let n of t)this.queueCollection[e].pushContent(n);return this}on(e,t,n){return this.queueCollection[e].pushConsume(t,n),()=>this.off(e,t)}off(e,t){return c(t)?this.queueCollection[e].removeConsumer(t):this.queueCollection[e].removeAllConsumer(),this}once(e,t,n){if(c(t)){let o=(r,i,u)=>(this.off(e,o),t(r,i,u));return this.on(e,o,n),this}else return new Promise(o=>{let r=i=>(this.off(e,r),o(i),!0);this.on(e,r,n)})}};function P(){let s;function e(){var t;(t=m.getLogsInstance())==null||t.setLogs("queue",{name:s.name,createdTime:s.createdTime,id:s.getId(),newsNum:s.getNews().length,newsIds:s.getNews().map(n=>n.getId()),consumerNum:s.getConsumerList().length,consumerIds:s.getConsumerList().map(n=>n.getId()),message:"queue ok!"})}return{mounted(t){s=t,e()},addedNews(){e()},ejectNews(){return e(),!0},addedConsumer(){e()},removedConsumer(){e()}}}var m=class{static setLogsConfig(e){var t,n,o;this.logs=e.logs,this.types=(t=e.types)!=null?t:["console"],this.logsComponents=(n=e.logsComponents)!=null?n:["exchange","queue","news","consumer"],this.customFunction=(o=e.customFunction)!=null?o:function(r){console.log(r)}}static getLogsInstance(){if(!!this.logs)return{setLogs:this.setLogs.bind(this)}}static setLogs(e,t){this.logsComponents.indexOf(e)!==-1&&this.types.forEach(n=>{n==="custom"?this.customFunction(e,t):n==="console"&&console.log(e,t)})}};m.logs=!1;var g=class{constructor(e){this.id=f();this.routes=[];this.repeater=()=>this.getRoutes();var t;Object.assign(this,e),this.createdTime=new Date().getTime(),(t=m.getLogsInstance())==null||t.setLogs("exchange",{id:this.getId(),name:this.name,createdTime:this.createdTime})}getId(){return this.id}getRoutes(){return this.routes}pushRoutes(e){this.routes=Array.from(new Set(this.routes.concat(e)))}setRoutes(e){this.routes=e}getRepeater(){return this.repeater}setRepeater(e){this.repeater=e}removeRoutes(e){e===void 0?this.routes=[]:this.routes=this.routes.filter(t=>e.indexOf(t)!==-1)}getQueueNameList(e){return h(this,null,function*(){var t,n,o;(t=m.getLogsInstance())==null||t.setLogs("exchange",{id:this.getId(),accepted:1,name:this.name,createdTime:this.createdTime});try{let r=yield this.repeater(e);return(n=m.getLogsInstance())==null||n.setLogs("exchange",{id:this.getId(),send:r.length,queueNames:r,name:this.name,createdTime:this.createdTime}),r}catch(r){return(o=m.getLogsInstance())==null||o.setLogs("exchange",{id:this.getId(),name:this.name,createdTime:this.createdTime,message:JSON.stringify(r)}),[]}})}};function R(s){return["mounted","addedNews","addedConsumer","removedConsumer"].indexOf(s)!==-1}function q(s){return["beforeAddNews","ejectNews"].indexOf(s)!==-1}var k=(t=>(t.Random="Random",t.All="All",t))(k||{}),d=class{constructor(e){this.id=f();this.ask=!1;this.rcn=3;this.mode="All";this.async=!1;this.state=!1;this.maxTime=3e3;this.news=[];this.consumerList=[];this.operators=[];Object.assign(this,e),this.createdTime=new Date().getTime(),this.add(P())}getId(){return this.id}getNews(){return this.news}removeAllNews(){return this.news=[],!0}pushNews(e){e.consumedTimes===-1&&(e.consumedTimes=this.rcn),e.consumedTimes>0&&this.news.findIndex(t=>t.getId()===e.getId())===-1&&this.operate("beforeAddNews",e).then(t=>{!t||(this.news.push(e),this.operate("addedNews",e),this.news.length>0&&this.consumerList.length>0&&this.consumeNews())})}eject(e=0){return h(this,null,function*(){if(this.news.length>0){let t=this.news.splice(e,1)[0];return(yield this.operate("ejectNews",t))?t:null}else return null})}pushContent(e){let t=new C(e);this.pushNews(t)}removeNewsById(e){let t=this.news.findIndex(n=>n.getId()===e);return t===-1?!1:(this.eject(t),!0)}getConsumerList(){return this.consumerList}pushConsumer(e){this.consumerList.findIndex(t=>t.getId()===e.getId())===-1&&(this.consumerList.push(e),this.operate("addedConsumer",e),this.news.length>0&&this.consumerList.length>0&&this.consumeNews())}removeConsumer(e){let t=this.consumerList.findIndex(o=>o.consume===e);if(t===-1)return!1;let n=this.consumerList.splice(t,1);return this.operate("removedConsumer",n),!0}pushConsume(e,t){let n=new x(e,t);this.pushConsumer(n)}removeConsumerById(e){let t=this.consumerList.findIndex(o=>o.getId()===e);if(t===-1)return!1;let n=this.consumerList.splice(t,1);return this.operate("removedConsumer",n),!0}removeAllConsumer(){let e=this.consumerList.splice(0);return this.operate("removedConsumer",e),!0}add(...e){return e.forEach(t=>{this.operators.push(t!=null?t:{}),t!=null&&t.mounted&&t.mounted(this)}),this}operate(e,...t){return h(this,null,function*(){let n=this.operators.filter(o=>o[e]).map(o=>o[e]);if(R(e))for(let o of n)o==null||o(t[0]);else if(q(e)){for(let o of n)if(!(yield o==null?void 0:o(t[0])))return!1}else throw"operate error";return!0})}consumeNews(){if(this.news.length===0||this.consumerList.length===0||!this.async&&this.state)return;this.state=!0;let e=this.mode==="Random"?[this.consumerList[Math.round(Math.random()*(this.consumerList.length-1))]]:[...this.consumerList];this.eject().then(t=>{if(t===null){this.state=!1,this.consumeNews();return}Promise.all(e.map(n=>this.consumption(t,n))).then(()=>{}).catch(()=>{t.consumedTimes--,this.pushNews(t)}).finally(()=>{this.state=!1,this.consumeNews()})}),this.async&&this.consumeNews()}consumption(e,t){return new Promise((n,o)=>{let r=this.maxTime,i=r>=0?setTimeout(()=>{o(!1)},r):void 0;t.consumption(e,this.ask).then(u=>{u?n(u):o(u),r>=0&&clearTimeout(i)})})}};var x=class{constructor(e,t){this.id=f();var n;this.createdTime=new Date().getTime(),this.consume=e,this.payload=t,(n=m.getLogsInstance())==null||n.setLogs("consumer",{id:this.getId(),createdTime:this.createdTime})}getId(){return this.id}consumption(e,t){var o;return(o=m.getLogsInstance())==null||o.setLogs("consumer",{id:this.getId(),createdTime:this.createdTime,accepted:1}),{then:r=>{var i;try{if(!t)return this.consume(e.content,this.payload),r(!0);let u=(l=!0)=>r(l),a=this.consume(e.content,u,this.payload);S(a)?a.then(l=>{r(Boolean(l))}).catch(()=>{r(!1)}):typeof a=="boolean"&&r(a)}catch(u){(i=m.getLogsInstance())==null||i.setLogs("consumer",{id:this.getId(),createdTime:this.createdTime,message:JSON.stringify(u)}),r(!t)}}}}};var C=class{constructor(e){this.id=f();this.consumedTimes=-1;var t;this.createdTime=new Date().getTime(),this.content=e,(t=m.getLogsInstance())==null||t.setLogs("news",{id:this.getId(),createdTime:this.createdTime})}getId(){return this.id}};function A(s,e){let t=0,n=null,o;return{beforeAddNews(){let r=new Date().getTime();if(e){let i=t;return t=r,r>i+s}return n!==null&&r<=t+s&&(o(!1),clearTimeout(n)),t=r,new Promise(i=>{o=i,n=setTimeout(()=>{n=null,i(!0)},s)})}}}function U(s){return{beforeAddNews(e){return s(e.content)}}}function H(){return{mounted(s){if(s.mode!=="All")throw`${s.name} \u961F\u5217 mode \u9700\u8981\u8BBE\u7F6E\u6210All`;s.pushConsume(()=>!0)}}}function F(s=1e3,e=!0){s<0&&(s=0);let t=0,n=null,o={go:()=>{},stop:()=>{}},r;return{mounted(i){r=i,o={go(){n=setInterval(()=>{t++,r.pushContent(t)},s)},stop(){n!==null&&(clearInterval(n),n=null)}},r.getConsumerList.length>0?o.go():e||o.go()},addedConsumer(){!e||n===null&&o.go()},removedConsumer(){if(!!e&&n!==null)return r.getConsumerList.length===0&&o.stop(),""}}}function j(s){let e=0;return{beforeAddNews(t){return t.content=s(t.content,e),e++,!0}}}function z(s){return{ejectNews(e){return new Date().getTime()<s+e.createdTime}}}function B(...s){return{mounted(e){s.forEach(t=>{e.pushContent(t)})}}}function G(s){let e=[];return{beforeAddNews(t){let n=s(t.content);return e.indexOf(n)===-1?(e.push(n),!0):!1}}}function O(s){let e=null;if(s===void 0)e=document.documentElement;else if(N(s)){if(e=document.getElementById(s),e===null)throw`id\uFF1A${s}\u4E0D\u5B58\u5728`}else e=s;return{mounted(t){if(e===null)return;new ResizeObserver(o=>{for(let r of o)t.pushContent(r)}).observe(e)}}}function W(s){let e=0;return{beforeAddNews(){return++e<=s}}}function X(s,e){let t=0,n=null;return{beforeAddNews(){let o=new Date().getTime();return e?o<=t+s?!1:(t=o,!0):n!==null?!1:new Promise(r=>{n=setTimeout(()=>{n=null,r(!0)},s)})}}}var $="u-node-mq-plugin";var _=s=>s+"_Iframe_Message",Y=s=>s+"_Iframe_Wait_Message",T=class{constructor(e,t){this.name=e;this.unmq=null;t!=null&&t.autoSize&&L().add(O(t.arg)).on(o=>{let r=ee();if(r===void 0)throw"selfFrame is undefined";r.y!==0&&r.window.parent!==window&&(document.body.style.overflow="hidden",this.postMessage(r.window.parent,4,{width:o.contentRect.width,height:o.contentRect.height}))})}install(e){if(!e.getExchange(this.name))throw`${this.name}\u4EA4\u6362\u673A\u4E0D\u5B58\u5728`;this.unmq=e;let o=e.getExchangeList().filter(r=>r.name!==this.name);for(let r of o){if(r.name===void 0)throw"\u7CFB\u7EDF\u9519\u8BEF";let i=_(r.name),u=Y(r.name);r.setRepeater(()=>[i,u]),e.addQueue(i,new d({async:!0})),e.addQueue(u,new d({async:!0})),e.on(u,()=>{this.broadcastMessage(1,{exchangeName:r.name,msg:`who is ${r.name} ?`})})}this.broadcastMessage(3,{msg:`${this.name} is online`}),window.addEventListener("message",this.receiveMessage.bind(this),!1)}receiveMessage({source:e,data:t,origin:n}){if(this.unmq===null)throw`${this.name} iframe \u672A\u5B89\u88C5`;if(!I(t))return!1;let{mask:o,type:r,message:i,fromName:u}=t;if(o!==$||e==null)return!1;let a=this.unmq.getExchange(u);if(a===null||N(a.origin)&&a.origin!==n)return!1;if([3,2].indexOf(r)!==-1){let l=this.unmq.on(_(u),y=>{this.postMessage(e,0,y,n)});setTimeout(l)}else if(r===0)this.unmq.emit(this.name,i);else if(r===1&&i.exchangeName===this.name)this.postMessage(e,2,{msg:`my name is ${this.name}`},n);else if(r===4){let l=0;for(let v=0;v<window.frames.length;v++)if(e===window.frames){l=v;break}let y=document.getElementsByTagName("iframe");if(y.length-1<l)throw"dom\u8282\u70B9\u4E0Eiframe\u6570\u91CF\u4E0D\u5339\u914D";y[l].width=i.width,y[l].height=i.height}else return!1;return!0}postMessage(e,t,n,o="*",r){e.postMessage({mask:$,type:t,message:n,fromName:this.name},o,r)}broadcastMessage(e,t){Z().forEach(o=>{this.postMessage(o.window,e,t,"*")})}};function Z(){if(window.top===null)throw"window.top is null";return K().filter(e=>e.window!==window.self)}function ee(){if(window.top===null)throw"window.top is null";return K().find(e=>e.window===window.self)}function K(){let s=[];function n(o,r,i){s[i]===void 0?s[i]=r:r=++s[i];let u=[];u.push({window:o,x:r,y:i}),i+=1;for(let a=0;a<o.frames.length;a++)u.push(...n(o[a],r,i)),r+=1;return u}if(window.top===null)throw"window.top is null";return n(window.top,0,0)}var Lt=p;export{k as ConsumMode,x as Consumer,g as Exchange,T as IframePlugin,m as Logs,C as News,d as Queue,b as QuickUNodeMQ,E as SingleUNodeMQ,V as createQuickUnmq,L as createSingleUnmq,J as createUnmq,A as debounceTime,Lt as default,U as filter,H as instant,F as interval,j as map,z as newsTime,B as of,G as removeDuplicates,O as resizeObserver,W as task,X as throttleTime}; //# sourceMappingURL=index.js.map