insite-subscriptions-client
Version:
inSite subscriptions client side
3 lines (2 loc) • 21.9 kB
JavaScript
var zt=Object.defineProperty;var wt=n=>{throw TypeError(n)};var qt=(n,s,t)=>s in n?zt(n,s,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[s]=t;var i=(n,s,t)=>qt(n,typeof s!="symbol"?s+"":s,t),St=(n,s,t)=>s.has(n)||wt("Cannot "+t);var a=(n,s,t)=>(St(n,s,"read from private field"),t?t.call(n):s.get(n)),f=(n,s,t)=>s.has(n)?wt("Cannot add the same private member more than once"):s instanceof WeakSet?s.add(n):s.set(n,t),d=(n,s,t,e)=>(St(n,s,"write to private field"),e?e.call(n,t):s.set(n,t),t),g=(n,s,t)=>(St(n,s,"access private method"),t);var Tt,it,M,q=class{constructor(s,t,e=[],r,o=!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",()=>{M.sendMessage("s-s",this.type,this.publicationName,this.i,this.args,this.immediately),this.immediately=!0});if(!M)throw new Error("Subscription is not binded to WS. Do Subscription.bindTo(ws) first");this.i=Tt++,this.type=s,this.publicationName=t,this.args=e,this.handler=r,this.immediately=o&&!globalThis.__initials,it.set(this.i,this),M.on("open",this.start),M.isOpen&&this.start(),globalThis.__initials?(this.isActive=!0,this.handler(globalThis.__initials.splice(0,1)[0])):this.isActive=!1}cancel(){it.delete(this.i),M.off("open",this.start),M.sendMessage("s-u",this.i)}static bindTo(s){M=s,Tt=0,it=new Map,M.on("message:s-c",(t,e)=>{let r=it.get(t);r&&(r.isActive=!0,r.handler(e))}),M.on("close",()=>{for(let t of it.values())t.isActive=!1}),M.on("server-change",()=>{for(let t of it.values())t.handler(null)})}};function K(){}var pt=new WeakMap,y=class n extends Promise{constructor(t,e){let r,o;super((u,h)=>{r=u,o=h});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=pt.get(this);e&&(e.resolve.call(this,t),pt.delete(this))}});i(this,"reject",t=>{if(this.isPending){this.isPending=!1,this.isRejected=!0,this.state="rejected",this.result=t;let e=pt.get(this);e&&(e.reject.call(this,t),pt.delete(this))}});if(pt.set(this,{resolve:r,reject:o}),e?.signal)if(e.signal.aborted)this.reject(new DOMException("Aborted","AbortError"));else{let u=()=>this.reject(new DOMException("Aborted","AbortError"));e.signal.addEventListener("abort",u),this.finally(()=>e.signal?.removeEventListener("abort",u))}if(typeof e?.timeout=="number"&&Number.isFinite(e.timeout)){let u=setTimeout(()=>this.reject(new DOMException("Timeout exceeded","TimeoutError")),e.timeout);this.finally(()=>clearTimeout(u))}t?.(this.resolve,this.reject)}static resolved(t){return new n(e=>e(t))}static rejected(t){return new n((e,r)=>r(t))}};function ct(n,s=0,t={}){let{leading:e=!1,trailing:r=!0}=t,{maxWait:o=1/0}=t,u,h,l,c,b,p,m,S,ft=async()=>{let J=Date.now();J>=l?(u=void 0,l=void 0,clearTimeout(h),h=void 0,typeof m=="function"&&await m(),V.resolve(S),p=e):(clearTimeout(u),u=setTimeout(ft,l-J))},lt=Number.isFinite(o)?()=>{m===Y&&(m=!1),Y(),h=setTimeout(lt,o)}:null;async function Y(){try{return S=await n.apply(c,b),S}catch(J){throw V.reject(J),J}}e&&typeof e=="boolean"&&(e=Y),p=e,r&&typeof r=="boolean"&&(r=Y),m=r;let V=Object.assign(function(...J){return b=J,c=this,l=Date.now()+s,m=r,V.promise.isPending||(V.promise=new y((Vt,Jt)=>{V.resolve=Vt,V.reject=Jt,typeof p=="function"&&(p(),p=!1,m===Y&&(m=!1)),u||(u=setTimeout(ft,s),lt&&(h=setTimeout(lt,o)))})),V.promise},{callback:n,promise:y.resolved(void 0),resolve:K,reject:K,run:Y,clear:async J=>{clearTimeout(u),u=void 0,clearTimeout(h),h=void 0,V.resolve(J?await Y():S)}});return V}ct.noop=Object.assign(()=>{},{callback:K,promise:y.resolved(void 0),resolve:K,reject:K,run:K,clear:K});function Ot(n,s,t,e,r=!0){return Array.isArray(n)&&(n=n.length),typeof e!="string"&&(r=e??!0,e=t),(r?`${n}\xA0`:"")+Ot.raw(n,s,t,e)}Ot.raw=(n,s,t,e=t)=>{n=Math.abs(n);let r=n%10,o=n%100;return r===1&&o!==11?s:r>=2&&r<=4&&!(o>=12&&o<=15)?t:e};function Z(n,s){let t=n.indexOf(s);return t>-1?(n.splice(t,1),!0):!1}function At(n){return[...new Set(n)]}var O=Symbol("update"),A=Symbol("getHandleUpdate"),U=Symbol("setHandleUpdate"),j=Symbol("getAsUpdates"),D=Symbol("getAsInitialUpdated"),$=Symbol("clear"),x=Symbol("subscribe"),I=Symbol("unsubscribe"),k=Symbol("renew"),E=Symbol("getSubscription"),Kt=Symbol("getHandleSubscription"),Xt=Symbol("setHandleSubscription");var Qt=(n,s)=>n>s?1:n<s?-1:0,Yt=(n,s)=>n>s?-1:n<s?1:0,jt,Dt,kt,Ht,H=class extends Array{constructor(t,e){super();i(this,"sorted",[]);i(this,"handleUpdate");i(this,"sortDirection",0);i(this,"sortCompareFunction",null);i(this,Ht,this.update);i(this,kt,this.getAsUpdates);i(this,Dt,this.getAsInitialUpdated);i(this,jt,this.clear);e&&(this.handleUpdate=e),Array.isArray(t)&&this.update(t)}[A](){return this.handleUpdate}[U](t){return this.handleUpdate=t}update(t){let e=Object.assign([],{deleted:[],added:[]});if(t){let r=!1;for(let o of t)switch(o?.[0]){case"i":{let[,u,h]=o;h?(this.sortDirection=h,this.sortCompareFunction=h>0?Qt:Yt,r=!0):(this.sortDirection=0,this.sortCompareFunction=null),this.length=0;for(let l of u)this.push(l),e.push(l),e.added.push(l);break}case"a":{let[,u]=o;this.includes(u)||(this.push(u),this.sortDirection&&(r=!0),e.push(u),e.added.push(u));break}case"d":{let[,u]=o;Z(this,u)&&e.deleted.push(u);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[(Ht=O,kt=j,Dt=D,jt=$,Symbol.toStringTag)](){return"SubscriptionArray"}};i(H,"updateSymbol",O),i(H,"getHandleUpdateSymbol",A),i(H,"setHandleUpdateSymbol",U),i(H,"getAsUpdatesSymbol",j),i(H,"getAsInitialUpdatedSymbol",D),i(H,"clearSymbol",$),i(H,"WithSubscription");var _=class extends H{constructor(t,e,r,o=!0){super(null,r);i(this,"publicationName");i(this,"params");i(this,"subscription",null);this.publicationName=t,this.params=e,o&&this.subscribe()}subscribe(){this.subscription||(this.subscription=new q("array",this.publicationName,this.params,t=>this.update(t)))}get[x](){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[k](){return this.renew}[E](){return this.subscription}get[Symbol.toStringTag](){return"SubscriptionArrayWithSubscription"}};i(_,"subscribeSymbol",x),i(_,"unsubscribeSymbol",I),i(_,"renewSymbol",k),i(_,"getSubscriptionSymbol",E);H.WithSubscription=_;import ee from"eventemitter3";var v={};import te from"eventemitter3";function Zt(n){let s=document.createElement("script");s.textContent=`window.__insite_subscription_map_sort_function = function sort(a, b) { return ${Object.entries(n).map(([e,r])=>(e.includes(".")&&(e=e.replaceAll(".","?.")),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=globalThis.__insite_subscription_map_sort_function;return s.remove(),delete globalThis.__insite_subscription_map_sort_function,t}var Pt,$t,Mt,Et,st,tt,rt,w=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,tt,[]);i(this,"sorted",[]);f(this,rt);i(this,Et,this.update);i(this,Mt,this.getAsUpdates);i(this,$t,this.getAsInitialUpdated);i(this,Pt,this.clear);e&&(this.handleUpdate=e),t&&this.update(t)}sort(){this.sortCompareFunction&&this.sorted.sort(this.sortCompareFunction)}[A](){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,o=a(this,rt),u=a(this,st);for(let h of t)switch(h?.[0]){case"i":{let[,l,c]=h;if(c?(this.sortList=c,this.sortCompareFunction=Zt(c),d(this,tt,At(Object.keys(c).map(b=>b.replace(/\..+/,"")))),r=!0):(this.sortList=null,this.sortCompareFunction=null,d(this,tt,[])),this.size){let b=new Map;for(let p of l)b.set(p._id,p);for(let[p,m]of this){let S=b.get(p);if(S){let ft=u.get(p);if(o)m.update(S);else{for(let lt in ft)delete m[lt];Object.assign(m,S)}u.set(p,S),e.push(m),b.delete(p)}else o?m.delete():this.delete(p),u.delete(p),e.deleted.push(p)}for(let[p,m]of b){let S=o?new o(this,m):{...m};this.set(p,S),u.set(p,m),e.push(S),e.added.push(S)}this.sortList&&(this.sorted=l.map(p=>this.get(p._id)))}else for(let b of l){let p=o?new o(this,b):{...b};this.set(p._id,p),this.sorted.push(p),u.set(p._id,b),e.push(p),e.added.push(p)}break}case"c":case"u":{let[,l]=h,c=this.get(l._id);if(c){let b=h[2]===!0,p=b?Object.keys(l):h[2],m=u.get(c._id);if(o)c.update(l);else{if(!b)for(let S in m)delete c[S];Object.assign(c,l)}b&&(l=Object.assign(m,l)),this.sortList&&!r&&(!p||p.some(S=>a(this,tt).includes(S)))&&(r=!0)}else c=o?new o(this,l):{...l},this.set(c._id,c),this.sorted.push(c),this.sortList&&(r=!0),e.added.push(c);u.set(c._id,l),e.push(c);break}case"d":{let[,l]=h,c=this.get(l);c&&(o?c.delete():this.delete(l),u.delete(l),Z(this.sorted,c),e.deleted.push(l));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,tt,[])}valueOf(){return this.sorted}get[(Et=O,Mt=j,$t=D,Pt=$,Symbol.toStringTag)](){return"SubscriptionMap"}};st=new WeakMap,tt=new WeakMap,rt=new WeakMap,i(w,"updateSymbol",O),i(w,"getHandleUpdateSymbol",A),i(w,"setHandleUpdateSymbol",U),i(w,"getAsUpdatesSymbol",j),i(w,"getAsInitialUpdatedSymbol",D),i(w,"clearSymbol",$),i(w,"WithSubscription"),i(w,"Item");var L=class extends w{constructor(t,e,r,o=!0){super(null,r);i(this,"publicationName");i(this,"params");i(this,"subscription",null);this.publicationName=t,this.params=e,o&&this.subscribe()}subscribe(){this.subscription||(this.subscription=new q("map",this.publicationName,this.params,t=>this.update(t)))}get[x](){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[k](){return this.renew}[E](){return this.subscription}get[Symbol.toStringTag](){return"SubscriptionMapWithSubscription"}};i(L,"subscribeSymbol",x),i(L,"unsubscribeSymbol",I),i(L,"renewSymbol",k),i(L,"getSubscriptionSymbol",E);w.WithSubscription=L;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;w.Item=gt;var ht=Symbol("handleUpdate"),X=Symbol("updates"),_t,Lt,P=class{constructor(s,t){i(this,Lt);i(this,_t,null);t&&(this[ht]=t),s&&this[O](s)}[(Lt=ht,_t=X,A)](){return this[ht]}[U](s){return this[ht]=s}[O](s){if(s){for(let t in this[X])delete this[t];for(let t in s)this[t]=s[t];this[X]=s}else this[$]();return this[ht]?.(this,this,this[X]),this}[j](){return this[X]&&{...this[X]}}[D](){return this.valueOf()}[$](){for(let s in this)delete this[s];this[X]=null}valueOf(){return this[X]&&this}get[Symbol.toStringTag](){return"SubscriptionObject"}};i(P,"updateSymbol",O),i(P,"getHandleUpdateSymbol",A),i(P,"setHandleUpdateSymbol",U),i(P,"getAsUpdatesSymbol",j),i(P,"getAsInitialUpdatedSymbol",D),i(P,"clearSymbol",$),i(P,"WithSubscription");var nt,ot,G,B=class extends P{constructor(t,e,r,o=!0){super(null,r);f(this,nt);f(this,ot);f(this,G,null);d(this,nt,t),d(this,ot,e),o&&this[x]()}[x](){a(this,G)||d(this,G,new q("object",a(this,nt),a(this,ot),t=>this[O](t)))}[I](){a(this,G)&&(a(this,G).cancel(),d(this,G,null))}[k](t,e){a(this,G)&&(this[I](),t&&(Array.isArray(t)?e=t:d(this,nt,t)),e&&d(this,ot,e),this[x]())}[E](){return a(this,G)}get[Symbol.toStringTag](){return"SubscriptionObjectWithSubscription"}};nt=new WeakMap,ot=new WeakMap,G=new WeakMap,i(B,"subscribeSymbol",x),i(B,"unsubscribeSymbol",I),i(B,"renewSymbol",k),i(B,"getSubscriptionSymbol",E);P.WithSubscription=B;function Gt(n){return Array.isArray(n)}var F,Q,N,T,xt,Bt,z,at,Ft,Nt,Ut,bt=class bt extends te{constructor(t,e){super();f(this,T);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,F);f(this,Q);f(this,N);f(this,z,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,z,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:o="object",publicationName:u,params:h,handle:l,onBeforeInit:c,debounce:b=bt.debounceLimit,preventBind:p}=t;if(this.group){if((!r||this.value===r)&&this.type===o&&this.publicationName===u&&JSON.stringify(this.params)===JSON.stringify(h)&&this.handle===l&&this.onBeforeInit===c)return g(this,T,xt).call(this,{debounceLimit:b,preventBind:p});this.detach(),delete this.value,delete this.publicationName,delete this.params}this.type=o,this.handle=l,this.onBeforeInit=c,r?this.value=r:(this.publicationName=u??this.name,this.params=h??[]),g(this,T,xt).call(this,{debounceLimit:b,preventBind:p}),g(this,T,Bt).call(this,e)}detach(){this.group&&(this.value?.[I](),this.isLoaded=!1,g(this,T,Ut).call(this),delete this.group.values[this.name],Z(this.group.values,this.value),delete this.group.items[this.name],Z(this.group.items,this),delete this.group)}loaded(){return a(this,F)}inited(){return a(this,Q)}unloaded(){return a(this,N)}subscribe(){if(this.value){if(this.value[x])return this.value[x]();{let t=this.value[A]();if(t){let e=this.value[D](),r=this.value[j]();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)}}}}}unsubscribe(){return this.value?.[I]?.()}renew(t,e){return this.value?.[k]?.(t,e)}valueOf(){return this.value}};F=new WeakMap,Q=new WeakMap,N=new WeakMap,T=new WeakSet,xt=function({debounceLimit:t,preventBind:e}){let r=o=>{this.emit("update",this.value,o),this.group?.emit(`update.${this.name}`,this.value,o),this.group?.emitUpdate()};if(typeof t=="number"){let o,u,h=ct(()=>{let l=!this.group;l&&(this.group=u),r(o),l&&delete this.group,o=void 0},t);this.emitUpdate=l=>{if(l!==void 0)if(Gt(l))if(Gt(o))for(let c of l)o.push(c);else o=Object.assign([...l],{added:[...l.added],deleted:[...l.deleted]});else typeof l=="object"?o?Object.assign(o,l):o={...l}:o=l;return u=this.group,h()}}else this.emitUpdate=r;this.preventBind===void 0?delete this.preventBind:this.preventBind=e},Bt=function(t){if(!this.group){switch(this.group=t,this.group.items.push(this),this.group.items[this.name]=this,(!a(this,F)||a(this,F).isFulfilled)&&d(this,F,new y),(!a(this,Q)||a(this,Q).isFulfilled)&&d(this,Q,new y),(!a(this,N)||a(this,N).isFulfilled)&&d(this,N,new y),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 L(this.publicationName,this.params,e,!1);break}default:{let e=a(this,at);this.value?this.value[U](e):this.value=new B(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)}}},z=new WeakMap,at=new WeakMap,Ft=function(){if(a(this,z)){if(this.handle)switch(this.type){case"array":this.handle.call(this.group,a(this,z),this.group);break;case"map":this.handle.call(this.group,a(this,z),this.group);break;default:this.handle.call(this.group,a(this,z),this.group)}d(this,z,null),this.emit("load",this.value),a(this,F).resolve(this),a(this,N).isFulfilled&&d(this,N,new y)}},Nt=function(){this.emit("init",this.value),a(this,Q).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,F).isFulfilled&&d(this,F,new y)},i(bt,"debounceLimit",4),v.itemLoad=t=>{var e;return g(e=t,T,Ft).call(e)},v.itemInit=t=>{var e;return g(e=t,T,Nt).call(e)},v.itemUnload=t=>{var e;return g(e=t,T,Ut).call(e)};var mt=bt;function ie(n){return Array.isArray(n)}function Wt(n){if(ie(n)){let s=n.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 n.type??(n.type="object"),n}var W,ut,C,et,R,It,Rt,vt,yt=class yt extends ee{constructor(t,{target:e,debounce:r=yt.debounceLimit,immediately:o}={}){super();f(this,R);i(this,"target");i(this,"emitUpdate");i(this,"items",[]);i(this,"values",[]);i(this,"isLoaded",!1);i(this,"isInited",!1);f(this,W);f(this,ut);f(this,C);f(this,et,null);this.applyOptions({target:e,debounceLimit:r}),d(this,ut,new y),this.attach(t,!1,o)}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,et)!==e)if(d(this,et,e),typeof a(this,et)=="number"){let r=ct(()=>this.emit("update",this.values),a(this,et));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,W)||a(this,W).isFulfilled)&&d(this,W,new y),(!a(this,C)||a(this,C).isFulfilled)&&d(this,C,new y),Promise.all(t.map(o=>new mt(Wt(o),this)).map(o=>(r&&o.subscribe(),o.loaded())))}detach(t,e=!0){for(let r of t)this.items[r]?.detach();e&&(g(this,R,vt).call(this),g(this,R,It).call(this))}redefine(t){let e=new Map(this.items.map(u=>[u.name,u])),r=[],o=[];for(let u of t)u=Wt(u),e.delete(u.name)?r.push(u):o.push(u);return this.isLoaded=!1,this.detach([...e.keys()],!1),Promise.all([...r.map(u=>{let h=this.items[u.name];return h.define(u),h.subscribe(),h.loaded()}),...o.length?[this.attach(o,!1)]:[]])}subscribe(){return this.items.map(t=>t.subscribe())}unsubscribe(){return this.items.map(t=>t.unsubscribe())}loaded(){return a(this,W)}inited(){return a(this,ut)}unloaded(){return a(this,C)}valueOf(){return this.values}[Symbol.iterator](){return this.values[Symbol.iterator]()}};W=new WeakMap,ut=new WeakMap,C=new WeakMap,et=new WeakMap,R=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,W).resolve(this),a(this,C).isFulfilled&&d(this,C,new y)}},Rt=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,ut).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,C).resolve(this),a(this,W).isFulfilled&&d(this,W,new y)}},i(yt,"debounceLimit",8),v.groupLoad=t=>{var e;return g(e=t,R,It).call(e)},v.groupInit=t=>{var e;return g(e=t,R,Rt).call(e)},v.groupUnload=t=>{var e;return g(e=t,R,vt).call(e)};var Ct=yt;export{q as Subscription,H as SubscriptionArray,_ as SubscriptionArrayWithSubscription,Ct as SubscriptionGroup,w as SubscriptionMap,L as SubscriptionMapWithSubscription,P as SubscriptionObject,B as SubscriptionObjectWithSubscription,$ as clearSymbol,D as getAsInitialUpdatedSymbol,j as getAsUpdatesSymbol,Kt as getHandleSubscriptionSymbol,A as getHandleUpdateSymbol,E as getSubscriptionSymbol,k as renewSymbol,Xt as setHandleSubscriptionSymbol,U as setHandleUpdateSymbol,x as subscribeSymbol,I as unsubscribeSymbol,O as updateSymbol};
//# sourceMappingURL=index.js.map