UNPKG

insite-subscriptions-client

Version:

inSite subscriptions client side

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