insite-subscriptions-client 2.1.3 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +1 -1
  2. package/package.json +5 -5
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- var Jt=Object.defineProperty;var Tt=o=>{throw TypeError(o)};var qt=(o,s,t)=>s in o?Jt(o,s,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[s]=t;var i=(o,s,t)=>qt(o,typeof s!="symbol"?s+"":s,t),yt=(o,s,t)=>s.has(o)||Tt("Cannot "+t);var a=(o,s,t)=>(yt(o,s,"read from private field"),t?t.call(o):s.get(o)),f=(o,s,t)=>s.has(o)?Tt("Cannot add the same private member more than once"):s instanceof WeakSet?s.add(o):s.set(o,t),c=(o,s,t,e)=>(yt(o,s,"write to private field"),e?e.call(o,t):s.set(o,t),t),g=(o,s,t)=>(yt(o,s,"access private method"),t);var wt,et,k,q=class{constructor(s,t,e=[],r,n=!0){i(this,"i");i(this,"type");i(this,"publicationName");i(this,"args");i(this,"handler");i(this,"immediately");i(this,"isActive");i(this,"start",()=>{k.sendMessage("s-s",this.type,this.publicationName,this.i,this.args,this.immediately),this.immediately=!0});if(!k)throw new Error("Subscription is not binded to WS. Do Subscription.bindTo(ws) first");this.i=wt++,this.type=s,this.publicationName=t,this.args=e,this.handler=r,this.immediately=n&&!globalThis.__initials,et.set(this.i,this),k.on("open",this.start),k.isOpen&&this.start(),globalThis.__initials?(this.isActive=!0,this.handler(globalThis.__initials.splice(0,1)[0])):this.isActive=!1}cancel(){et.delete(this.i),k.off("open",this.start),k.sendMessage("s-u",this.i)}static bindTo(s){k=s,wt=0,et=new Map,k.on("message:s-c",(t,e)=>{let r=et.get(t);r&&(r.isActive=!0,r.handler(e))}),k.on("close",()=>{for(let t of et.values())t.isActive=!1}),k.on("server-change",()=>{for(let t of et.values())t.handler(null)})}};function it(){}function lt(o,s=0,t={}){let{leading:e=!1,trailing:r=!0}=t,{maxWait:n=1/0}=t,p,h,u,d,b,l,m,S,ft=async()=>{let tt=Date.now();tt>=u?(p=void 0,u=void 0,clearTimeout(h),typeof m=="function"&&await m(),V.resolve(S),V.promise.isResolved=!0,l=e):(clearTimeout(p),p=setTimeout(ft,u-tt))},ut=Number.isFinite(n)?()=>{m===X&&(m=!1),X(),h=setTimeout(ut,n)}:null;async function X(){return S=await o.apply(d,b)}e&&typeof e=="boolean"&&(e=X),l=e,r&&typeof r=="boolean"&&(r=X),m=r;let V=Object.assign(function(...tt){return b=tt,d=this,u=Date.now()+s,m=r,V.promise.isResolved&&(V.promise=Object.assign(new Promise(Vt=>{V.resolve=Vt,typeof l=="function"&&(l(),l=!1,m===X&&(m=!1)),p||(p=setTimeout(ft,s),ut&&(h=setTimeout(ut,n)))}),{isResolved:!1})),V.promise},{callback:o,promise:Object.assign(Promise.resolve(),{isResolved:!0}),resolve:it,run:X,clear:async tt=>{clearTimeout(p),p=void 0,clearTimeout(h),V.promise.isResolved=!0,V.resolve(tt?await X():S)}});return V}lt.noop=Object.assign(()=>{},{callback:it,promise:Object.assign(Promise.resolve(),{isResolved:!0}),resolve:it,run:it,clear:it});function At(o,s,t,e,r=!0){return Array.isArray(o)&&(o=o.length),typeof e!="string"&&(r=e??!0,e=t),(r?`${o}\xA0`:"")+At.raw(o,s,t,e)}At.raw=(o,s,t,e=t)=>{o=Math.abs(o);let r=o%10,n=o%100;return r===1&&n!==11?s:r>=2&&r<=4&&!(n>=12&&n<=15)?t:e};function Q(o,s){let t=o.indexOf(s);return t>-1?(o.splice(t,1),!0):!1}var dt=new WeakMap,x=class extends Promise{constructor(t){let e,r;super((n,p)=>{e=n,r=p});i(this,"isPending",!0);i(this,"isFulfilled",!1);i(this,"isRejected",!1);i(this,"state","pending");i(this,"result");i(this,"resolve",t=>{if(this.isPending){this.isPending=!1,this.isFulfilled=!0,this.state="fulfilled",this.result=t;let e=dt.get(this);e&&(e.resolve.call(this,t),dt.delete(this))}});i(this,"reject",t=>{if(this.isPending){this.isPending=!1,this.isRejected=!0,this.state="rejected",this.result=t;let e=dt.get(this);e&&(e.reject.call(this,t),dt.delete(this))}});dt.set(this,{resolve:e,reject:r}),t?.(this.resolve,this.reject)}};function Ot(o){return[...new Set(o)]}var A=Symbol("update"),O=Symbol("getHandleUpdate"),U=Symbol("setHandleUpdate"),D=Symbol("getAsUpdates"),H=Symbol("getAsInitialUpdated"),L=Symbol("clear"),y=Symbol("subscribe"),I=Symbol("unsubscribe"),j=Symbol("renew"),G=Symbol("getSubscription"),zt=Symbol("getHandleSubscription"),Kt=Symbol("setHandleSubscription");var Xt=(o,s)=>o>s?1:o<s?-1:0,Qt=(o,s)=>o>s?-1:o<s?1:0,Dt,Ht,jt,Pt,P=class extends Array{constructor(t,e){super();i(this,"sorted",[]);i(this,"handleUpdate");i(this,"sortDirection",0);i(this,"sortCompareFunction",null);i(this,Pt,this.update);i(this,jt,this.getAsUpdates);i(this,Ht,this.getAsInitialUpdated);i(this,Dt,this.clear);e&&(this.handleUpdate=e),Array.isArray(t)&&this.update(t)}[O](){return this.handleUpdate}[U](t){return this.handleUpdate=t}update(t){let e=Object.assign([],{deleted:[],added:[]});if(t){let r=!1;for(let n of t)switch(n?.[0]){case"i":{let[,p,h]=n;h?(this.sortDirection=h,this.sortCompareFunction=h>0?Xt:Qt,r=!0):(this.sortDirection=0,this.sortCompareFunction=null),this.length=0;for(let u of p)this.push(u),e.push(u),e.added.push(u);break}case"a":{let[,p]=n;this.includes(p)||(this.push(p),this.sortDirection&&(r=!0),e.push(p),e.added.push(p));break}case"d":{let[,p]=n;Q(this,p)&&e.deleted.push(p);break}default:this.clear()}r&&this.sortCompareFunction&&this.sort(this.sortCompareFunction)}else this.clear();return this.handleUpdate?.(this,e,t?.[0]?t:null),this}getAsUpdates(){return this.length||this.sortDirection?[["i",[...this],this.sortDirection]]:null}getAsInitialUpdated(){return Object.assign([...this.sorted],{added:[...this.sorted],deleted:[]})}clear(){this.length=0,this.sortDirection=0,this.sortCompareFunction=null}valueOf(){return this}get[(Pt=A,jt=D,Ht=H,Dt=L,Symbol.toStringTag)](){return"SubscriptionArray"}};i(P,"updateSymbol",A),i(P,"getHandleUpdateSymbol",O),i(P,"setHandleUpdateSymbol",U),i(P,"getAsUpdatesSymbol",D),i(P,"getAsInitialUpdatedSymbol",H),i(P,"clearSymbol",L),i(P,"WithSubscription");var B=class extends P{constructor(t,e,r,n=!0){super(null,r);i(this,"publicationName");i(this,"params");i(this,"subscription",null);this.publicationName=t,this.params=e,n&&this.subscribe()}subscribe(){this.subscription||(this.subscription=new q("array",this.publicationName,this.params,t=>this.update(t)))}get[y](){return this.subscribe}unsubscribe(){this.subscription&&(this.subscription.cancel(),this.subscription=null)}get[I](){return this.unsubscribe}renew(t,e){this.subscription&&(this.unsubscribe(),t&&(Array.isArray(t)?e=t:this.publicationName=t),e&&(this.params=e),this.subscribe())}get[j](){return this.renew}[G](){return this.subscription}get[Symbol.toStringTag](){return"SubscriptionArrayWithSubscription"}};i(B,"subscribeSymbol",y),i(B,"unsubscribeSymbol",I),i(B,"renewSymbol",j),i(B,"getSubscriptionSymbol",G);P.WithSubscription=B;import te from"eventemitter3";var v={};import Zt from"eventemitter3";function Yt(o){let s=document.createElement("script");s.textContent=`window.__insite_subscription_map_sort_function = function sort(a, b) { return ${Object.entries(o).map(([e,r])=>r>0?`a.${e} > b.${e} ? 1 : a.${e} < b.${e} ? -1 :`:`a.${e} < b.${e} ? 1 : a.${e} > b.${e} ? -1 :`).concat("0").join(" ")};}`,document.head.append(s);let t=window.__insite_subscription_map_sort_function;return s.remove(),delete window.__insite_subscription_map_sort_function,t}var _t,Lt,kt,Gt,st,Y,rt,T=class extends Map{constructor(t,e){super();i(this,"handleUpdate");f(this,st,new Map);i(this,"sortList",null);i(this,"sortCompareFunction",null);f(this,Y,[]);i(this,"sorted",[]);f(this,rt);i(this,Gt,this.update);i(this,kt,this.getAsUpdates);i(this,Lt,this.getAsInitialUpdated);i(this,_t,this.clear);e&&(this.handleUpdate=e),t&&this.update(t)}sort(){this.sortCompareFunction&&this.sorted.sort(this.sortCompareFunction)}[O](){return this.handleUpdate}[U](t){return this.handleUpdate=t}set Item(t){if(c(this,rt,t),this.size)for(let[e,r]of this)this.set(e,new t(this,r))}get Item(){return a(this,rt)}update(t){let e=Object.assign([],{added:[],deleted:[]});if(t){let r=!1,n=a(this,rt),p=a(this,st);for(let h of t)switch(h?.[0]){case"i":{let[,u,d]=h;if(d?(this.sortList=d,this.sortCompareFunction=Yt(d),c(this,Y,Ot(Object.keys(d).map(b=>b.replace(/\..+/,"")))),r=!0):(this.sortList=null,this.sortCompareFunction=null,c(this,Y,[])),this.size){let b=new Map;for(let l of u)b.set(l._id,l);for(let[l,m]of this){let S=b.get(l);if(S){let ft=p.get(l);if(n)m.update(S);else{for(let ut in ft)delete m[ut];Object.assign(m,S)}p.set(l,S),e.push(m),b.delete(l)}else n?m.delete():this.delete(l),p.delete(l),e.deleted.push(l)}for(let[l,m]of b){let S=n?new n(this,m):{...m};this.set(l,S),p.set(l,m),e.push(S),e.added.push(S)}this.sortList&&(this.sorted=u.map(l=>this.get(l._id)))}else for(let b of u){let l=n?new n(this,b):{...b};this.set(l._id,l),this.sorted.push(l),p.set(l._id,b),e.push(l),e.added.push(l)}break}case"c":case"u":{let[,u]=h,d=this.get(u._id);if(d){let b=h[2]===!0,l=b?Object.keys(u):h[2],m=p.get(d._id);if(n)d.update(u);else{if(!b)for(let S in m)delete d[S];Object.assign(d,u)}b&&(u=Object.assign(m,u)),this.sortList&&!r&&(!l||l.some(S=>a(this,Y).includes(S)))&&(r=!0)}else d=n?new n(this,u):{...u},this.set(d._id,d),this.sorted.push(d),this.sortList&&(r=!0),e.added.push(d);p.set(d._id,u),e.push(d);break}case"d":{let[,u]=h,d=this.get(u);d&&(n?d.delete():this.delete(u),p.delete(u),Q(this.sorted,d),e.deleted.push(u));break}default:this.clear()}r&&this.sort()}else this.clear();return this.handleUpdate?.(this,e,t?.[0]?t:null),this}getAsUpdates(){return this.size||this.sortList?[["i",[...a(this,st).values()],this.sortList]]:null}getAsInitialUpdated(){return Object.assign([...this.sorted],{added:[...this.sorted],deleted:[]})}clear(){super.clear(),a(this,st).clear(),this.sorted=[],this.sortList=null,this.sortCompareFunction=null,c(this,Y,[])}valueOf(){return this.sorted}get[(Gt=A,kt=D,Lt=H,_t=L,Symbol.toStringTag)](){return"SubscriptionMap"}};st=new WeakMap,Y=new WeakMap,rt=new WeakMap,i(T,"updateSymbol",A),i(T,"getHandleUpdateSymbol",O),i(T,"setHandleUpdateSymbol",U),i(T,"getAsUpdatesSymbol",D),i(T,"getAsInitialUpdatedSymbol",H),i(T,"clearSymbol",L),i(T,"WithSubscription"),i(T,"Item");var M=class extends T{constructor(t,e,r,n=!0){super(null,r);i(this,"publicationName");i(this,"params");i(this,"subscription",null);this.publicationName=t,this.params=e,n&&this.subscribe()}subscribe(){this.subscription||(this.subscription=new q("map",this.publicationName,this.params,t=>this.update(t)))}get[y](){return this.subscribe}unsubscribe(){this.subscription&&(this.subscription.cancel(),this.subscription=null)}get[I](){return this.unsubscribe}renew(t,e){this.subscription&&(this.unsubscribe(),t&&(Array.isArray(t)?e=t:this.publicationName=t),e&&(this.params=e),this.subscribe())}get[j](){return this.renew}[G](){return this.subscription}get[Symbol.toStringTag](){return"SubscriptionMapWithSubscription"}};i(M,"subscribeSymbol",y),i(M,"unsubscribeSymbol",I),i(M,"renewSymbol",j),i(M,"getSubscriptionSymbol",G);T.WithSubscription=M;var ct,gt=class{constructor(s,t){i(this,"_id");f(this,ct);c(this,ct,s),this.update(t)}update(s){Object.assign(this,s)}delete(){a(this,ct).delete(this._id)}};ct=new WeakMap;T.Item=gt;var ht=Symbol("handleUpdate"),z=Symbol("updates"),Bt,Mt,_=class{constructor(s,t){i(this,Mt);i(this,Bt,null);t&&(this[ht]=t),s&&this[A](s)}[(Mt=ht,Bt=z,O)](){return this[ht]}[U](s){return this[ht]=s}[A](s){if(s){for(let t in this[z])delete this[t];for(let t in s)this[t]=s[t];this[z]=s}else this[L]();return this[ht]?.(this,this,this[z]),this}[D](){return this[z]&&{...this[z]}}[H](){return this.valueOf()}[L](){for(let s in this)delete this[s];this[z]=null}valueOf(){return this[z]&&this}get[Symbol.toStringTag](){return"SubscriptionObject"}};i(_,"updateSymbol",A),i(_,"getHandleUpdateSymbol",O),i(_,"setHandleUpdateSymbol",U),i(_,"getAsUpdatesSymbol",D),i(_,"getAsInitialUpdatedSymbol",H),i(_,"clearSymbol",L),i(_,"WithSubscription");var ot,nt,$,F=class extends _{constructor(t,e,r,n=!0){super(null,r);f(this,ot);f(this,nt);f(this,$,null);c(this,ot,t),c(this,nt,e),n&&this[y]()}[y](){a(this,$)||c(this,$,new q("object",a(this,ot),a(this,nt),t=>this[A](t)))}[I](){a(this,$)&&(a(this,$).cancel(),c(this,$,null))}[j](t,e){a(this,$)&&(this[I](),t&&(Array.isArray(t)?e=t:c(this,ot,t)),e&&c(this,nt,e),this[y]())}[G](){return a(this,$)}get[Symbol.toStringTag](){return"SubscriptionObjectWithSubscription"}};ot=new WeakMap,nt=new WeakMap,$=new WeakMap,i(F,"subscribeSymbol",y),i(F,"unsubscribeSymbol",I),i(F,"renewSymbol",j),i(F,"getSubscriptionSymbol",G);_.WithSubscription=F;function $t(o){return Array.isArray(o)}var W,K,N,w,xt,Ft,J,at,Wt,Nt,Ut,bt=class bt extends Zt{constructor(t,e){super();f(this,w);i(this,"name");i(this,"group");i(this,"value");i(this,"type");i(this,"publicationName");i(this,"params");i(this,"handle");i(this,"onBeforeInit");i(this,"preventBind");i(this,"emitUpdate");i(this,"isLoaded",!1);i(this,"isInited",!1);f(this,W);f(this,K);f(this,N);f(this,J,null);f(this,at,(t,e,r)=>{if(r)if(this.group.isLoaded){if(this.handle)switch(this.type){case"array":this.handle.call(this.group,e,this.group);break;case"map":this.handle.call(this.group,e,this.group);break;default:this.handle.call(this.group,e,this.group)}this.emitUpdate(e)}else c(this,J,e),this.isLoaded=!0,v.groupLoad(this.group);else this.isLoaded&&(this.isLoaded=!1,v.groupUnload(this.group));this.isInited||(this.isInited=!0,v.groupInit(this.group))});this.name=t.name,this.define(t,e)}define(t,e=this.group){if(!e)throw new Error("group is undefined");let{value:r,type:n="object",publicationName:p,params:h,handle:u,onBeforeInit:d,debounce:b=bt.debounceLimit,preventBind:l}=t;if(this.group){if((!r||this.value===r)&&this.type===n&&this.publicationName===p&&JSON.stringify(this.params)===JSON.stringify(h)&&this.handle===u&&this.onBeforeInit===d)return g(this,w,xt).call(this,{debounceLimit:b,preventBind:l});this.detach(),delete this.value,delete this.publicationName,delete this.params}return this.type=n,this.handle=u,this.onBeforeInit=d,r?this.value=r:(this.publicationName=p??this.name,this.params=h??[]),g(this,w,xt).call(this,{debounceLimit:b,preventBind:l}),g(this,w,Ft).call(this,e),null}detach(){this.group&&(this.value?.[I](),this.isLoaded=!1,g(this,w,Ut).call(this),delete this.group.values[this.name],Q(this.group.values,this.value),delete this.group.items[this.name],Q(this.group.items,this),delete this.group)}loaded(){return a(this,W)}inited(){return a(this,K)}unloaded(){return a(this,N)}subscribe(){if(this.value){if(this.value[y])return this.value[y]();{let t=this.value[O]();if(t){let e=this.value[H](),r=this.value[D]();switch(this.type){case"array":return t(this.value,e,r);case"map":return t(this.value,e,r);default:return t(this.value,e,r)}}}}return null}unsubscribe(){return this.value?.[I]?.()}renew(t,e){return this.value?.[j]?.(t,e)}valueOf(){return this.value}};W=new WeakMap,K=new WeakMap,N=new WeakMap,w=new WeakSet,xt=function({debounceLimit:t,preventBind:e}){let r=n=>{this.emit("update",this.value,n),this.group?.emit(`update.${this.name}`,this.value,n),this.group?.emitUpdate()};if(typeof t=="number"){let n,p,h=lt(()=>{let u=!this.group;u&&(this.group=p),r(n),u&&delete this.group,n=void 0},t);this.emitUpdate=u=>{if(u!==void 0)if($t(u))if($t(n))for(let d of u)n.push(d);else n=Object.assign([...u],{added:[...u.added],deleted:[...u.deleted]});else typeof u=="object"?n?Object.assign(n,u):n={...u}:n=u;return p=this.group,h()}}else this.emitUpdate=r;this.preventBind===void 0?delete this.preventBind:this.preventBind=e},Ft=function(t){if(!this.group){switch(this.group=t,this.group.items.push(this),this.group.items[this.name]=this,(!a(this,W)||a(this,W).isFulfilled)&&c(this,W,new x),(!a(this,K)||a(this,K).isFulfilled)&&c(this,K,new x),(!a(this,N)||a(this,N).isFulfilled)&&c(this,N,new x),this.type){case"array":{let e=a(this,at);this.value?this.value[U](e):this.value=new B(this.publicationName,this.params,e,!1);break}case"map":{let e=a(this,at);this.value?this.value[U](e):this.value=new M(this.publicationName,this.params,e,!1);break}default:{let e=a(this,at);this.value?this.value[U](e):this.value=new F(this.publicationName,this.params,e,!1)}}if(this.group.values.push(this.value),this.group.values[this.name]=this.value,this.onBeforeInit)switch(this.type){case"array":this.onBeforeInit.call(this.group,this.value);break;case"map":this.onBeforeInit.call(this.group,this.value);break;default:this.onBeforeInit.call(this.group,this.value)}}},J=new WeakMap,at=new WeakMap,Wt=function(){if(a(this,J)){if(this.handle)switch(this.type){case"array":this.handle.call(this.group,a(this,J),this.group);break;case"map":this.handle.call(this.group,a(this,J),this.group);break;default:this.handle.call(this.group,a(this,J),this.group)}c(this,J,null),this.emit("load",this.value),a(this,W).resolve(this),a(this,N).isFulfilled&&c(this,N,new x)}},Nt=function(){this.emit("init",this.value),a(this,K).resolve(this)},Ut=function(){if(this.handle)switch(this.type){case"array":this.handle.call(this.group,null,this.group);break;case"map":this.handle.call(this.group,null,this.group);break;default:this.handle.call(this.group,null,this.group)}this.emit("unload",this.value),a(this,N).resolve(this),a(this,W).isFulfilled&&c(this,W,new x)},i(bt,"debounceLimit",4),v.itemLoad=t=>{var e;return g(e=t,w,Wt).call(e)},v.itemInit=t=>{var e;return g(e=t,w,Nt).call(e)},v.itemUnload=t=>{var e;return g(e=t,w,Ut).call(e)};var mt=bt;function ee(o){return Array.isArray(o)}function Et(o){if(ee(o)){let s=o.slice(),t={};if(t.name=s.shift(),typeof s[0]=="object"&&!Array.isArray(s[0])){if(t.value=s.shift(),t.type=t.value.toString().match(/Subscription(Object|Array|Map)/)?.[1].toLowerCase(),!t.type)throw new Error("Unknown value type")}else typeof s[0]=="string"&&(t.type=s.shift()),typeof s[0]=="string"&&(t.publicationName=s.shift()),Array.isArray(s[0])&&(t.params=s.shift());return typeof s[0]=="function"&&(t.handle=s.shift()),typeof s[0]=="function"&&(t.onBeforeInit=s.shift()),typeof s[0]=="number"&&(t.debounce=s.shift()),typeof s[0]=="boolean"&&(t.preventBind=s.shift()),t}return o.type??(o.type="object"),o}var E,pt,R,Z,C,It,Ct,vt,St=class St extends te{constructor(t,{target:e,debounce:r=St.debounceLimit,immediately:n}={}){super();f(this,C);i(this,"target");i(this,"emitUpdate");i(this,"items",[]);i(this,"values",[]);i(this,"isLoaded",!1);i(this,"isInited",!1);f(this,E);f(this,pt);f(this,R);f(this,Z,null);this.applyOptions({target:e,debounceLimit:r}),c(this,pt,new x),this.attach(t,!1,n)}applyOptions({target:t,debounceLimit:e}){if(this.target!==t){if(this.target)for(let r of this.items)r.isLoaded&&!r.preventBind&&delete this.target[r.name];if(this.target=t,this.target)for(let r of this.items)r.isLoaded&&!r.preventBind&&(this.target[r.name]=r.value)}if(a(this,Z)!==e)if(c(this,Z,e),typeof a(this,Z)=="number"){let r=lt(()=>this.emit("update",this.values),a(this,Z));this.emitUpdate=()=>r()}else this.emitUpdate=()=>this.emit("update",this.values)}attach(t,e=this.isLoaded,r=!0){return e&&(this.isLoaded=!1),(!a(this,E)||a(this,E).isFulfilled)&&c(this,E,new x),(!a(this,R)||a(this,R).isFulfilled)&&c(this,R,new x),Promise.all(t.map(n=>new mt(Et(n),this)).map(n=>(r&&n.subscribe(),n.loaded())))}detach(t,e=!0){for(let r of t)this.items[r]?.detach();e&&(g(this,C,vt).call(this),g(this,C,It).call(this))}redefine(t){let e=new Map(this.items.map(p=>[p.name,p])),r=[],n=[];for(let p of t)p=Et(p),e.delete(p.name)?r.push(p):n.push(p);return this.isLoaded=!1,this.detach([...e.keys()],!1),Promise.all([...r.map(p=>{let h=this.items[p.name];return h.define(p),h.subscribe(),h.loaded()}),...n.length?[this.attach(n,!1)]:[]])}subscribe(){return this.items.map(t=>t.subscribe())}unsubscribe(){return this.items.map(t=>t.unsubscribe())}loaded(){return a(this,E)}inited(){return a(this,pt)}unloaded(){return a(this,R)}valueOf(){return this.values}[Symbol.iterator](){return this.values[Symbol.iterator]()}};E=new WeakMap,pt=new WeakMap,R=new WeakMap,Z=new WeakMap,C=new WeakSet,It=function(){if(!this.isLoaded&&this.items.every(t=>t.isLoaded)){if(this.target)for(let t of this.items)t.preventBind||(this.target[t.name]=t.value);this.isLoaded=!0;for(let t of this.items)v.itemLoad(t);if(this.emit("load",this.values),this.isInited)if(this.items.length)for(let t of this.items)t.emitUpdate();else this.emitUpdate();a(this,E).resolve(this),a(this,R).isFulfilled&&c(this,R,new x)}},Ct=function(){if(!this.isInited&&this.items.every(t=>t.isInited)){if(this.isInited=!0,this.isLoaded)if(this.items.length)for(let t of this.items)t.emitUpdate();else this.emitUpdate();for(let t of this.items)v.itemInit(t);this.emit("init",this.values),a(this,pt).resolve(this)}},vt=function(){if(this.isLoaded&&this.items.every(t=>!t.isLoaded)){if(this.target)for(let t of this.items)t.preventBind||delete this.target[t.name];this.isLoaded=!1;for(let t of this.items)v.itemUnload(t);if(this.emit("unload",this.values),this.items.length)for(let t of this.items)t.emitUpdate();else this.emitUpdate();a(this,R).resolve(this),a(this,E).isFulfilled&&c(this,E,new x)}},i(St,"debounceLimit",8),v.groupLoad=t=>{var e;return g(e=t,C,It).call(e)},v.groupInit=t=>{var e;return g(e=t,C,Ct).call(e)},v.groupUnload=t=>{var e;return g(e=t,C,vt).call(e)};var Rt=St;export{q as Subscription,P as SubscriptionArray,B as SubscriptionArrayWithSubscription,Rt as SubscriptionGroup,T as SubscriptionMap,M as SubscriptionMapWithSubscription,_ as SubscriptionObject,F as SubscriptionObjectWithSubscription,L as clearSymbol,H as getAsInitialUpdatedSymbol,D as getAsUpdatesSymbol,zt as getHandleSubscriptionSymbol,O as getHandleUpdateSymbol,G as getSubscriptionSymbol,j as renewSymbol,Kt as setHandleSubscriptionSymbol,U as setHandleUpdateSymbol,y as subscribeSymbol,I as unsubscribeSymbol,A as updateSymbol};
1
+ var Jt=Object.defineProperty;var Tt=o=>{throw TypeError(o)};var qt=(o,s,t)=>s in o?Jt(o,s,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[s]=t;var i=(o,s,t)=>qt(o,typeof s!="symbol"?s+"":s,t),yt=(o,s,t)=>s.has(o)||Tt("Cannot "+t);var a=(o,s,t)=>(yt(o,s,"read from private field"),t?t.call(o):s.get(o)),f=(o,s,t)=>s.has(o)?Tt("Cannot add the same private member more than once"):s instanceof WeakSet?s.add(o):s.set(o,t),d=(o,s,t,e)=>(yt(o,s,"write to private field"),e?e.call(o,t):s.set(o,t),t),g=(o,s,t)=>(yt(o,s,"access private method"),t);var wt,et,L,q=class{constructor(s,t,e=[],r,n=!0){i(this,"i");i(this,"type");i(this,"publicationName");i(this,"args");i(this,"handler");i(this,"immediately");i(this,"isActive");i(this,"start",()=>{L.sendMessage("s-s",this.type,this.publicationName,this.i,this.args,this.immediately),this.immediately=!0});if(!L)throw new Error("Subscription is not binded to WS. Do Subscription.bindTo(ws) first");this.i=wt++,this.type=s,this.publicationName=t,this.args=e,this.handler=r,this.immediately=n&&!globalThis.__initials,et.set(this.i,this),L.on("open",this.start),L.isOpen&&this.start(),globalThis.__initials?(this.isActive=!0,this.handler(globalThis.__initials.splice(0,1)[0])):this.isActive=!1}cancel(){et.delete(this.i),L.off("open",this.start),L.sendMessage("s-u",this.i)}static bindTo(s){L=s,wt=0,et=new Map,L.on("message:s-c",(t,e)=>{let r=et.get(t);r&&(r.isActive=!0,r.handler(e))}),L.on("close",()=>{for(let t of et.values())t.isActive=!1}),L.on("server-change",()=>{for(let t of et.values())t.handler(null)})}};function it(){}function lt(o,s=0,t={}){let{leading:e=!1,trailing:r=!0}=t,{maxWait:n=1/0}=t,p,h,u,c,b,l,m,S,ft=async()=>{let tt=Date.now();tt>=u?(p=void 0,u=void 0,clearTimeout(h),typeof m=="function"&&await m(),V.resolve(S),V.promise.isResolved=!0,l=e):(clearTimeout(p),p=setTimeout(ft,u-tt))},ut=Number.isFinite(n)?()=>{m===X&&(m=!1),X(),h=setTimeout(ut,n)}:null;async function X(){return S=await o.apply(c,b)}e&&typeof e=="boolean"&&(e=X),l=e,r&&typeof r=="boolean"&&(r=X),m=r;let V=Object.assign(function(...tt){return b=tt,c=this,u=Date.now()+s,m=r,V.promise.isResolved&&(V.promise=Object.assign(new Promise(Vt=>{V.resolve=Vt,typeof l=="function"&&(l(),l=!1,m===X&&(m=!1)),p||(p=setTimeout(ft,s),ut&&(h=setTimeout(ut,n)))}),{isResolved:!1})),V.promise},{callback:o,promise:Object.assign(Promise.resolve(),{isResolved:!0}),resolve:it,run:X,clear:async tt=>{clearTimeout(p),p=void 0,clearTimeout(h),V.promise.isResolved=!0,V.resolve(tt?await X():S)}});return V}lt.noop=Object.assign(()=>{},{callback:it,promise:Object.assign(Promise.resolve(),{isResolved:!0}),resolve:it,run:it,clear:it});function At(o,s,t,e,r=!0){return Array.isArray(o)&&(o=o.length),typeof e!="string"&&(r=e??!0,e=t),(r?`${o}\xA0`:"")+At.raw(o,s,t,e)}At.raw=(o,s,t,e=t)=>{o=Math.abs(o);let r=o%10,n=o%100;return r===1&&n!==11?s:r>=2&&r<=4&&!(n>=12&&n<=15)?t:e};function Q(o,s){let t=o.indexOf(s);return t>-1?(o.splice(t,1),!0):!1}var ct=new WeakMap,x=class extends Promise{constructor(t){let e,r;super((n,p)=>{e=n,r=p});i(this,"isPending",!0);i(this,"isFulfilled",!1);i(this,"isRejected",!1);i(this,"state","pending");i(this,"result");i(this,"resolve",t=>{if(this.isPending){this.isPending=!1,this.isFulfilled=!0,this.state="fulfilled",this.result=t;let e=ct.get(this);e&&(e.resolve.call(this,t),ct.delete(this))}});i(this,"reject",t=>{if(this.isPending){this.isPending=!1,this.isRejected=!0,this.state="rejected",this.result=t;let e=ct.get(this);e&&(e.reject.call(this,t),ct.delete(this))}});ct.set(this,{resolve:e,reject:r}),t?.(this.resolve,this.reject)}};function Ot(o){return[...new Set(o)]}var A=Symbol("update"),O=Symbol("getHandleUpdate"),U=Symbol("setHandleUpdate"),D=Symbol("getAsUpdates"),H=Symbol("getAsInitialUpdated"),k=Symbol("clear"),y=Symbol("subscribe"),I=Symbol("unsubscribe"),j=Symbol("renew"),G=Symbol("getSubscription"),zt=Symbol("getHandleSubscription"),Kt=Symbol("setHandleSubscription");var Xt=(o,s)=>o>s?1:o<s?-1:0,Qt=(o,s)=>o>s?-1:o<s?1:0,Dt,Ht,jt,Pt,P=class extends Array{constructor(t,e){super();i(this,"sorted",[]);i(this,"handleUpdate");i(this,"sortDirection",0);i(this,"sortCompareFunction",null);i(this,Pt,this.update);i(this,jt,this.getAsUpdates);i(this,Ht,this.getAsInitialUpdated);i(this,Dt,this.clear);e&&(this.handleUpdate=e),Array.isArray(t)&&this.update(t)}[O](){return this.handleUpdate}[U](t){return this.handleUpdate=t}update(t){let e=Object.assign([],{deleted:[],added:[]});if(t){let r=!1;for(let n of t)switch(n?.[0]){case"i":{let[,p,h]=n;h?(this.sortDirection=h,this.sortCompareFunction=h>0?Xt:Qt,r=!0):(this.sortDirection=0,this.sortCompareFunction=null),this.length=0;for(let u of p)this.push(u),e.push(u),e.added.push(u);break}case"a":{let[,p]=n;this.includes(p)||(this.push(p),this.sortDirection&&(r=!0),e.push(p),e.added.push(p));break}case"d":{let[,p]=n;Q(this,p)&&e.deleted.push(p);break}default:this.clear()}r&&this.sortCompareFunction&&this.sort(this.sortCompareFunction)}else this.clear();return this.handleUpdate?.(this,e,t?.[0]?t:null),this}getAsUpdates(){return this.length||this.sortDirection?[["i",[...this],this.sortDirection]]:null}getAsInitialUpdated(){return Object.assign([...this.sorted],{added:[...this.sorted],deleted:[]})}clear(){this.length=0,this.sortDirection=0,this.sortCompareFunction=null}valueOf(){return this}get[(Pt=A,jt=D,Ht=H,Dt=k,Symbol.toStringTag)](){return"SubscriptionArray"}};i(P,"updateSymbol",A),i(P,"getHandleUpdateSymbol",O),i(P,"setHandleUpdateSymbol",U),i(P,"getAsUpdatesSymbol",D),i(P,"getAsInitialUpdatedSymbol",H),i(P,"clearSymbol",k),i(P,"WithSubscription");var $=class extends P{constructor(t,e,r,n=!0){super(null,r);i(this,"publicationName");i(this,"params");i(this,"subscription",null);this.publicationName=t,this.params=e,n&&this.subscribe()}subscribe(){this.subscription||(this.subscription=new q("array",this.publicationName,this.params,t=>this.update(t)))}get[y](){return this.subscribe}unsubscribe(){this.subscription&&(this.subscription.cancel(),this.subscription=null)}get[I](){return this.unsubscribe}renew(t,e){this.subscription&&(this.unsubscribe(),t&&(Array.isArray(t)?e=t:this.publicationName=t),e&&(this.params=e),this.subscribe())}get[j](){return this.renew}[G](){return this.subscription}get[Symbol.toStringTag](){return"SubscriptionArrayWithSubscription"}};i($,"subscribeSymbol",y),i($,"unsubscribeSymbol",I),i($,"renewSymbol",j),i($,"getSubscriptionSymbol",G);P.WithSubscription=$;import te from"eventemitter3";var v={};import Zt from"eventemitter3";function Yt(o){let s=document.createElement("script");s.textContent=`window.__insite_subscription_map_sort_function = function sort(a, b) { return ${Object.entries(o).map(([e,r])=>r>0?`a.${e} > b.${e} ? 1 : a.${e} < b.${e} ? -1 :`:`a.${e} < b.${e} ? 1 : a.${e} > b.${e} ? -1 :`).concat("0").join(" ")};}`,document.head.append(s);let t=window.__insite_subscription_map_sort_function;return s.remove(),delete window.__insite_subscription_map_sort_function,t}var _t,kt,Lt,Gt,st,Y,rt,T=class extends Map{constructor(t,e){super();i(this,"handleUpdate");f(this,st,new Map);i(this,"sortList",null);i(this,"sortCompareFunction",null);f(this,Y,[]);i(this,"sorted",[]);f(this,rt);i(this,Gt,this.update);i(this,Lt,this.getAsUpdates);i(this,kt,this.getAsInitialUpdated);i(this,_t,this.clear);e&&(this.handleUpdate=e),t&&this.update(t)}sort(){this.sortCompareFunction&&this.sorted.sort(this.sortCompareFunction)}[O](){return this.handleUpdate}[U](t){return this.handleUpdate=t}set Item(t){if(d(this,rt,t),this.size)for(let[e,r]of this)this.set(e,new t(this,r))}get Item(){return a(this,rt)}update(t){let e=Object.assign([],{added:[],deleted:[]});if(t){let r=!1,n=a(this,rt),p=a(this,st);for(let h of t)switch(h?.[0]){case"i":{let[,u,c]=h;if(c?(this.sortList=c,this.sortCompareFunction=Yt(c),d(this,Y,Ot(Object.keys(c).map(b=>b.replace(/\..+/,"")))),r=!0):(this.sortList=null,this.sortCompareFunction=null,d(this,Y,[])),this.size){let b=new Map;for(let l of u)b.set(l._id,l);for(let[l,m]of this){let S=b.get(l);if(S){let ft=p.get(l);if(n)m.update(S);else{for(let ut in ft)delete m[ut];Object.assign(m,S)}p.set(l,S),e.push(m),b.delete(l)}else n?m.delete():this.delete(l),p.delete(l),e.deleted.push(l)}for(let[l,m]of b){let S=n?new n(this,m):{...m};this.set(l,S),p.set(l,m),e.push(S),e.added.push(S)}this.sortList&&(this.sorted=u.map(l=>this.get(l._id)))}else for(let b of u){let l=n?new n(this,b):{...b};this.set(l._id,l),this.sorted.push(l),p.set(l._id,b),e.push(l),e.added.push(l)}break}case"c":case"u":{let[,u]=h,c=this.get(u._id);if(c){let b=h[2]===!0,l=b?Object.keys(u):h[2],m=p.get(c._id);if(n)c.update(u);else{if(!b)for(let S in m)delete c[S];Object.assign(c,u)}b&&(u=Object.assign(m,u)),this.sortList&&!r&&(!l||l.some(S=>a(this,Y).includes(S)))&&(r=!0)}else c=n?new n(this,u):{...u},this.set(c._id,c),this.sorted.push(c),this.sortList&&(r=!0),e.added.push(c);p.set(c._id,u),e.push(c);break}case"d":{let[,u]=h,c=this.get(u);c&&(n?c.delete():this.delete(u),p.delete(u),Q(this.sorted,c),e.deleted.push(u));break}default:this.clear()}r&&this.sort()}else this.clear();return this.handleUpdate?.(this,e,t?.[0]?t:null),this}getAsUpdates(){return this.size||this.sortList?[["i",[...a(this,st).values()],this.sortList]]:null}getAsInitialUpdated(){return Object.assign([...this.sorted],{added:[...this.sorted],deleted:[]})}clear(){super.clear(),a(this,st).clear(),this.sorted=[],this.sortList=null,this.sortCompareFunction=null,d(this,Y,[])}valueOf(){return this.sorted}get[(Gt=A,Lt=D,kt=H,_t=k,Symbol.toStringTag)](){return"SubscriptionMap"}};st=new WeakMap,Y=new WeakMap,rt=new WeakMap,i(T,"updateSymbol",A),i(T,"getHandleUpdateSymbol",O),i(T,"setHandleUpdateSymbol",U),i(T,"getAsUpdatesSymbol",D),i(T,"getAsInitialUpdatedSymbol",H),i(T,"clearSymbol",k),i(T,"WithSubscription"),i(T,"Item");var B=class extends T{constructor(t,e,r,n=!0){super(null,r);i(this,"publicationName");i(this,"params");i(this,"subscription",null);this.publicationName=t,this.params=e,n&&this.subscribe()}subscribe(){this.subscription||(this.subscription=new q("map",this.publicationName,this.params,t=>this.update(t)))}get[y](){return this.subscribe}unsubscribe(){this.subscription&&(this.subscription.cancel(),this.subscription=null)}get[I](){return this.unsubscribe}renew(t,e){this.subscription&&(this.unsubscribe(),t&&(Array.isArray(t)?e=t:this.publicationName=t),e&&(this.params=e),this.subscribe())}get[j](){return this.renew}[G](){return this.subscription}get[Symbol.toStringTag](){return"SubscriptionMapWithSubscription"}};i(B,"subscribeSymbol",y),i(B,"unsubscribeSymbol",I),i(B,"renewSymbol",j),i(B,"getSubscriptionSymbol",G);T.WithSubscription=B;var dt,gt=class{constructor(s,t){i(this,"_id");f(this,dt);d(this,dt,s),this.update(t)}update(s){Object.assign(this,s)}delete(){a(this,dt).delete(this._id)}};dt=new WeakMap;T.Item=gt;var ht=Symbol("handleUpdate"),z=Symbol("updates"),$t,Bt,_=class{constructor(s,t){i(this,Bt);i(this,$t,null);t&&(this[ht]=t),s&&this[A](s)}[(Bt=ht,$t=z,O)](){return this[ht]}[U](s){return this[ht]=s}[A](s){if(s){for(let t in this[z])delete this[t];for(let t in s)this[t]=s[t];this[z]=s}else this[k]();return this[ht]?.(this,this,this[z]),this}[D](){return this[z]&&{...this[z]}}[H](){return this.valueOf()}[k](){for(let s in this)delete this[s];this[z]=null}valueOf(){return this[z]&&this}get[Symbol.toStringTag](){return"SubscriptionObject"}};i(_,"updateSymbol",A),i(_,"getHandleUpdateSymbol",O),i(_,"setHandleUpdateSymbol",U),i(_,"getAsUpdatesSymbol",D),i(_,"getAsInitialUpdatedSymbol",H),i(_,"clearSymbol",k),i(_,"WithSubscription");var ot,nt,M,F=class extends _{constructor(t,e,r,n=!0){super(null,r);f(this,ot);f(this,nt);f(this,M,null);d(this,ot,t),d(this,nt,e),n&&this[y]()}[y](){a(this,M)||d(this,M,new q("object",a(this,ot),a(this,nt),t=>this[A](t)))}[I](){a(this,M)&&(a(this,M).cancel(),d(this,M,null))}[j](t,e){a(this,M)&&(this[I](),t&&(Array.isArray(t)?e=t:d(this,ot,t)),e&&d(this,nt,e),this[y]())}[G](){return a(this,M)}get[Symbol.toStringTag](){return"SubscriptionObjectWithSubscription"}};ot=new WeakMap,nt=new WeakMap,M=new WeakMap,i(F,"subscribeSymbol",y),i(F,"unsubscribeSymbol",I),i(F,"renewSymbol",j),i(F,"getSubscriptionSymbol",G);_.WithSubscription=F;function Mt(o){return Array.isArray(o)}var W,K,E,w,xt,Ft,J,at,Wt,Et,Ut,bt=class bt extends Zt{constructor(t,e){super();f(this,w);i(this,"name");i(this,"group");i(this,"value");i(this,"type");i(this,"publicationName");i(this,"params");i(this,"handle");i(this,"onBeforeInit");i(this,"preventBind");i(this,"emitUpdate");i(this,"isLoaded",!1);i(this,"isInited",!1);f(this,W);f(this,K);f(this,E);f(this,J,null);f(this,at,(t,e,r)=>{if(r)if(this.group.isLoaded){if(this.handle)switch(this.type){case"array":this.handle.call(this.group,e,this.group);break;case"map":this.handle.call(this.group,e,this.group);break;default:this.handle.call(this.group,e,this.group)}this.emitUpdate(e)}else d(this,J,e),this.isLoaded=!0,v.groupLoad(this.group);else this.isLoaded&&(this.isLoaded=!1,v.groupUnload(this.group));this.isInited||(this.isInited=!0,v.groupInit(this.group))});this.name=t.name,this.define(t,e)}define(t,e=this.group){if(!e)throw new Error("group is undefined");let{value:r,type:n="object",publicationName:p,params:h,handle:u,onBeforeInit:c,debounce:b=bt.debounceLimit,preventBind:l}=t;if(this.group){if((!r||this.value===r)&&this.type===n&&this.publicationName===p&&JSON.stringify(this.params)===JSON.stringify(h)&&this.handle===u&&this.onBeforeInit===c)return g(this,w,xt).call(this,{debounceLimit:b,preventBind:l});this.detach(),delete this.value,delete this.publicationName,delete this.params}return this.type=n,this.handle=u,this.onBeforeInit=c,r?this.value=r:(this.publicationName=p??this.name,this.params=h??[]),g(this,w,xt).call(this,{debounceLimit:b,preventBind:l}),g(this,w,Ft).call(this,e),null}detach(){this.group&&(this.value?.[I](),this.isLoaded=!1,g(this,w,Ut).call(this),delete this.group.values[this.name],Q(this.group.values,this.value),delete this.group.items[this.name],Q(this.group.items,this),delete this.group)}loaded(){return a(this,W)}inited(){return a(this,K)}unloaded(){return a(this,E)}subscribe(){if(this.value){if(this.value[y])return this.value[y]();{let t=this.value[O]();if(t){let e=this.value[H](),r=this.value[D]();switch(this.type){case"array":return t(this.value,e,r);case"map":return t(this.value,e,r);default:return t(this.value,e,r)}}}}return null}unsubscribe(){return this.value?.[I]?.()}renew(t,e){return this.value?.[j]?.(t,e)}valueOf(){return this.value}};W=new WeakMap,K=new WeakMap,E=new WeakMap,w=new WeakSet,xt=function({debounceLimit:t,preventBind:e}){let r=n=>{this.emit("update",this.value,n),this.group?.emit(`update.${this.name}`,this.value,n),this.group?.emitUpdate()};if(typeof t=="number"){let n,p,h=lt(()=>{let u=!this.group;u&&(this.group=p),r(n),u&&delete this.group,n=void 0},t);this.emitUpdate=u=>{if(u!==void 0)if(Mt(u))if(Mt(n))for(let c of u)n.push(c);else n=Object.assign([...u],{added:[...u.added],deleted:[...u.deleted]});else typeof u=="object"?n?Object.assign(n,u):n={...u}:n=u;return p=this.group,h()}}else this.emitUpdate=r;this.preventBind===void 0?delete this.preventBind:this.preventBind=e},Ft=function(t){if(!this.group){switch(this.group=t,this.group.items.push(this),this.group.items[this.name]=this,(!a(this,W)||a(this,W).isFulfilled)&&d(this,W,new x),(!a(this,K)||a(this,K).isFulfilled)&&d(this,K,new x),(!a(this,E)||a(this,E).isFulfilled)&&d(this,E,new x),this.type){case"array":{let e=a(this,at);this.value?this.value[U](e):this.value=new $(this.publicationName,this.params,e,!1);break}case"map":{let e=a(this,at);this.value?this.value[U](e):this.value=new B(this.publicationName,this.params,e,!1);break}default:{let e=a(this,at);this.value?this.value[U](e):this.value=new F(this.publicationName,this.params,e,!1)}}if(this.group.values.push(this.value),this.group.values[this.name]=this.value,this.onBeforeInit)switch(this.type){case"array":this.onBeforeInit.call(this.group,this.value);break;case"map":this.onBeforeInit.call(this.group,this.value);break;default:this.onBeforeInit.call(this.group,this.value)}}},J=new WeakMap,at=new WeakMap,Wt=function(){if(a(this,J)){if(this.handle)switch(this.type){case"array":this.handle.call(this.group,a(this,J),this.group);break;case"map":this.handle.call(this.group,a(this,J),this.group);break;default:this.handle.call(this.group,a(this,J),this.group)}d(this,J,null),this.emit("load",this.value),a(this,W).resolve(this),a(this,E).isFulfilled&&d(this,E,new x)}},Et=function(){this.emit("init",this.value),a(this,K).resolve(this)},Ut=function(){if(this.handle)switch(this.type){case"array":this.handle.call(this.group,null,this.group);break;case"map":this.handle.call(this.group,null,this.group);break;default:this.handle.call(this.group,null,this.group)}this.emit("unload",this.value),a(this,E).resolve(this),a(this,W).isFulfilled&&d(this,W,new x)},i(bt,"debounceLimit",4),v.itemLoad=t=>{var e;return g(e=t,w,Wt).call(e)},v.itemInit=t=>{var e;return g(e=t,w,Et).call(e)},v.itemUnload=t=>{var e;return g(e=t,w,Ut).call(e)};var mt=bt;function ee(o){return Array.isArray(o)}function Nt(o){if(ee(o)){let s=o.slice(),t={};if(t.name=s.shift(),typeof s[0]=="object"&&!Array.isArray(s[0])){if(t.value=s.shift(),t.type=t.value.toString().match(/Subscription(Object|Array|Map)/)?.[1].toLowerCase(),!t.type)throw new Error("Unknown value type")}else typeof s[0]=="string"&&(t.type=s.shift()),typeof s[0]=="string"&&(t.publicationName=s.shift()),Array.isArray(s[0])&&(t.params=s.shift());return typeof s[0]=="function"&&(t.handle=s.shift()),typeof s[0]=="function"&&(t.onBeforeInit=s.shift()),typeof s[0]=="number"&&(t.debounce=s.shift()),typeof s[0]=="boolean"&&(t.preventBind=s.shift()),t}return o.type??(o.type="object"),o}var N,pt,R,Z,C,It,Ct,vt,St=class St extends te{constructor(t,{target:e,debounce:r=St.debounceLimit,immediately:n}={}){super();f(this,C);i(this,"target");i(this,"emitUpdate");i(this,"items",[]);i(this,"values",[]);i(this,"isLoaded",!1);i(this,"isInited",!1);f(this,N);f(this,pt);f(this,R);f(this,Z,null);this.applyOptions({target:e,debounceLimit:r}),d(this,pt,new x),this.attach(t,!1,n)}applyOptions({target:t,debounceLimit:e}){if(this.target!==t){if(this.target)for(let r of this.items)r.isLoaded&&!r.preventBind&&delete this.target[r.name];if(this.target=t,this.target)for(let r of this.items)r.isLoaded&&!r.preventBind&&(this.target[r.name]=r.value)}if(a(this,Z)!==e)if(d(this,Z,e),typeof a(this,Z)=="number"){let r=lt(()=>this.emit("update",this.values),a(this,Z));this.emitUpdate=()=>r()}else this.emitUpdate=()=>this.emit("update",this.values)}attach(t,e=this.isLoaded,r=!0){return e&&(this.isLoaded=!1),(!a(this,N)||a(this,N).isFulfilled)&&d(this,N,new x),(!a(this,R)||a(this,R).isFulfilled)&&d(this,R,new x),Promise.all(t.map(n=>new mt(Nt(n),this)).map(n=>(r&&n.subscribe(),n.loaded())))}detach(t,e=!0){for(let r of t)this.items[r]?.detach();e&&(g(this,C,vt).call(this),g(this,C,It).call(this))}redefine(t){let e=new Map(this.items.map(p=>[p.name,p])),r=[],n=[];for(let p of t)p=Nt(p),e.delete(p.name)?r.push(p):n.push(p);return this.isLoaded=!1,this.detach([...e.keys()],!1),Promise.all([...r.map(p=>{let h=this.items[p.name];return h.define(p),h.subscribe(),h.loaded()}),...n.length?[this.attach(n,!1)]:[]])}subscribe(){return this.items.map(t=>t.subscribe())}unsubscribe(){return this.items.map(t=>t.unsubscribe())}loaded(){return a(this,N)}inited(){return a(this,pt)}unloaded(){return a(this,R)}valueOf(){return this.values}[Symbol.iterator](){return this.values[Symbol.iterator]()}};N=new WeakMap,pt=new WeakMap,R=new WeakMap,Z=new WeakMap,C=new WeakSet,It=function(){if(!this.isLoaded&&this.items.every(t=>t.isLoaded)){if(this.target)for(let t of this.items)t.preventBind||(this.target[t.name]=t.value);this.isLoaded=!0;for(let t of this.items)v.itemLoad(t);if(this.emit("load",this.values),this.isInited)if(this.items.length)for(let t of this.items)t.emitUpdate();else this.emitUpdate();a(this,N).resolve(this),a(this,R).isFulfilled&&d(this,R,new x)}},Ct=function(){if(!this.isInited&&this.items.every(t=>t.isInited)){if(this.isInited=!0,this.isLoaded)if(this.items.length)for(let t of this.items)t.emitUpdate();else this.emitUpdate();for(let t of this.items)v.itemInit(t);this.emit("init",this.values),a(this,pt).resolve(this)}},vt=function(){if(this.isLoaded&&this.items.every(t=>!t.isLoaded)){if(this.target)for(let t of this.items)t.preventBind||delete this.target[t.name];this.isLoaded=!1;for(let t of this.items)v.itemUnload(t);if(this.emit("unload",this.values),this.items.length)for(let t of this.items)t.emitUpdate();else this.emitUpdate();a(this,R).resolve(this),a(this,N).isFulfilled&&d(this,N,new x)}},i(St,"debounceLimit",8),v.groupLoad=t=>{var e;return g(e=t,C,It).call(e)},v.groupInit=t=>{var e;return g(e=t,C,Ct).call(e)},v.groupUnload=t=>{var e;return g(e=t,C,vt).call(e)};var Rt=St;export{q as Subscription,P as SubscriptionArray,$ as SubscriptionArrayWithSubscription,Rt as SubscriptionGroup,T as SubscriptionMap,B as SubscriptionMapWithSubscription,_ as SubscriptionObject,F as SubscriptionObjectWithSubscription,k as clearSymbol,H as getAsInitialUpdatedSymbol,D as getAsUpdatesSymbol,zt as getHandleSubscriptionSymbol,O as getHandleUpdateSymbol,G as getSubscriptionSymbol,j as renewSymbol,Kt as setHandleSubscriptionSymbol,U as setHandleUpdateSymbol,y as subscribeSymbol,I as unsubscribeSymbol,A as updateSymbol};
2
2
  //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "insite-subscriptions-client",
3
- "version": "2.1.3",
3
+ "version": "2.2.0",
4
4
  "description": "inSite subscriptions client side",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
7
7
  "module": "./dist/index.js",
8
- "packageManager": "bun@1.2.2",
8
+ "packageManager": "bun@1.2.3",
9
9
  "scripts": {
10
10
  "test": "echo \"Error: no test specified\" && exit 1",
11
11
  "build": "bun .conveyer.js",
@@ -24,13 +24,13 @@
24
24
  "author": "Eugene Nesvetaev <dev@nesvet.dev> (https://nesvet.dev/)",
25
25
  "license": "MIT",
26
26
  "dependencies": {
27
- "@nesvet/n": "^1.1.23",
27
+ "@nesvet/n": "^1.1.24",
28
28
  "eventemitter3": "^5.0.1",
29
- "insite-ws": "^2.1.3"
29
+ "insite-ws": "^2.2.0"
30
30
  },
31
31
  "devDependencies": {
32
32
  "@nesvet/conveyer": "^2.4.3",
33
- "@nesvet/eslint-config": "^2.0.47",
33
+ "@nesvet/eslint-config": "^2.0.48",
34
34
  "typescript": "^5.7.3"
35
35
  }
36
36
  }