@traxjs/trax
Version:
Reactive state management
2 lines (1 loc) • 22.3 kB
JavaScript
(function(M,ee){typeof exports=="object"&&typeof module<"u"?ee(exports):typeof define=="function"&&define.amd?define(["exports"],ee):(M=typeof globalThis<"u"?globalThis:M||self,ee(M.trax={}))})(this,function(M){"use strict";let ee;function Se(r){r.value=null,r.next=ee,ee=r}function $e(r,s){if(ee){const c=ee;return ee=c.next,c.value=r,c.next=s,c}}class ae{constructor(){this._size=0}get head(){return this._head}get size(){return this._size}add(s){const c=this._head;let f=$e(s,c)||{value:s,next:c};return this._head=f,this._size++,f}insert(s){let c=this._head,f;if(!c)f=s()||void 0,f!==void 0&&this.add(f);else{let u;for(;u||c;){if(f=s(u==null?void 0:u.value,c==null?void 0:c.value)||void 0,f!==void 0){let i=$e(f,c)||{value:f,next:c};u?u.next=i:this._head=i,this._size++;return}u=c,c=c==null?void 0:c.next}}}peek(){return this._head?this._head.value:void 0}shift(){const s=this._head;if(s){const c=s.value;return this._head=s.next,this._size--,Se(s),c}}remove(s){let c=this._head,f=null;for(;c;){if(c.value===s)return f?(f.next=c.next,this._size--,Se(c)):this.shift(),!0;f=c,c=c.next}return!1}}M.TraxObjectType=void 0,function(r){r.NotATraxObject="",r.Object="O",r.Array="A",r.Store="S",r.Processor="P"}(M.TraxObjectType||(M.TraxObjectType={}));const p=Object.freeze({Info:"!LOG",Warning:"!WRN",Error:"!ERR",CycleStart:"!CS",CycleComplete:"!CC",New:"!NEW",Dispose:"!DEL",Set:"!SET",Get:"!GET",ProcessorDirty:"!DRT",ProcessorSkipped:"!SKP",ProcessingStart:"!PCS",ProcessingPause:"!PCP",ProcessingResume:"!PCR",ProcessingEnd:"!PCE"}),Ce=new Set;Object.getOwnPropertyNames(p).forEach(r=>{Ce.add(p[r])});var ke=globalThis&&globalThis.__awaiter||function(r,s,c,f){function u(i){return i instanceof c?i:new c(function(m){m(i)})}return new(c||(c=Promise))(function(i,m){function A(w){try{E(f.next(w))}catch(z){m(z)}}function R(w){try{E(f.throw(w))}catch(z){m(z)}}function E(w){w.done?i(w.value):u(w.value).then(A,R)}E((f=f.apply(r,s||[])).next())})};const je=new Set(["","Main","AllButGet","All"]),Ee="color: #65abff",we="color: ",Oe="color: #e08d00;font-weight:bold",Ne="color: #00ff00;font-weight:bold";function Me(r,s,c){let f=0,u=1e3,i,m;const A=new Map,R=[];let E="",w=-1,z=-1,F=null,_=0;function N(){return F===null&&(z=-1,w++,F=Promise.resolve().then(O),j(p.CycleStart)),z++,w+":"+z}function O(){H(),j(p.CycleComplete),F=null}function j(l){l===p.CycleComplete&&c&&c();const g=Date.now(),b=_!==0?g-_:0;_=g,D(l,{elapsedTime:b},r)}const L=1,G=2,K=3,J=new ae;function Z(l){J.add(l)}function B(l){let g=J.shift();for(;g&&g!==l;)C("[trax/processing context] Contexts must be ended or paused before parent:",g.id),g=J.shift()}function H(){if(J.size!==0){let l=J.shift();for(;l;)C("[trax/processing context] Contexts must be ended or paused before cycle ends:",l.id),l=J.shift()}}function te(l,g){let b=L;const y=Object.assign({processId:l},g),d={get id(){return l},pause(){b!==L?C("[trax/processing context] Only started or resumed contexts can be paused:",l):(B(d),D(p.ProcessingPause,y,r),b=G)},resume(){b!==G?C("[trax/processing context] Only paused contexts can be resumed:",l):(Z(d),D(p.ProcessingResume,y,r),b=L)},end(){b===K?C("[trax/processing context] Contexts cannot be ended twice:",l):(B(d),D(p.ProcessingEnd,y,r),b=K)}};return Z(d),d}function C(...l){D(p.Error,pe(l))}function D(l,g,b,y){let d;if(f>=u&&u>1?(d=i,i=i.next,f--,d.id="",d.type="",d.next=d.data=d.parentId=void 0):d={id:"",type:""},We(r,d,l,s,g,b),d.id=N(),d.parentId=y,d.type!==""){i===void 0?(i=m=d,f=1):(m.next=d,m=d,f++);for(const T of R)try{T({id:d.id,type:d.type,data:d.data})}catch{}V(d.type,d)}if(E){const T=d.type;let X=!1;if(E==="All"?X=!0:E==="AllButGet"?X=T!==p.Get:E==="Main"&&(Ce.has(T)?X=T===p.Error||T===p.Info||T===p.Warning||T===p.ProcessingResume||T===p.ProcessorDirty||T===p.ProcessorSkipped||T===p.Set||T===p.ProcessingStart:X=!0),X&&T!==p.CycleStart&&T!==p.CycleComplete){let q=Ge(d.type,d.data,!0),ne="";d.parentId&&(ne=" - parent:%c"+d.parentId);const e=`%cTRX %c${d.id} %c${d.type}${q?"%c - "+q:""}${ne}`,t=e.replaceAll("%c",""),o=Math.floor((e.length-t.length)/2),a=[e,Ee,we,Ne];for(let n=3;o>n;n++)a.push(n%2?we:Oe);console.log.apply(console,a)}}return d}function V(l,g){const b=A.get(l);if(b){const y=b.filter(d=>!d({id:g.id,type:g.type,data:g.data,parentId:g.parentId}));y.length===0?A.delete(l):A.set(l,y)}}return{get consoleOutput(){return E},set consoleOutput(l){if(je.has(l))E=l;else{const g=`"${[...je].join('" or "')}"`;console.log(`%cTRX %cInvalid consoleOutput value - should be either %c${g}`,Ee,"color: ",Oe)}},event(l,g,b){D(l,g,b)},info(...l){D(p.Info,pe(l))},warn(...l){D(p.Warning,pe(l))},error:C,set maxSize(l){const g=u;if(l<0?u=-1:l<2?u=2:u=l,u>0&&u<g&&u<f){let b=f-u;for(;i&&b;)i=i.next,b--,f--}},startProcessingContext(l,g){let b=l.name;b.charAt(0)==="!"&&g!==r&&(C(`Processing Context name cannot start with reserved prefix: ${b}`),l.name=b.replace(/\!+/,""));const y=J.peek(),d=y?y.id:void 0,T=D(p.ProcessingStart,l,r,d);return te(T.id,l)},get maxSize(){return u},get size(){return f},scan(l){let g=i,b=!0;for(;b&&g;)try{l(g)===!1&&(b=!1),g=g.next}catch{}},lastEvent(){return m},awaitEvent(l,g){return ke(this,void 0,void 0,function*(){if(l===""||l==="*")return D(p.Error,`[trax/eventStream.await] Invalid event type: '${l}'`),{id:m.id,type:m.type,data:m.data};let b=A.get(l);b===void 0&&(b=[],A.set(l,b));let y,d=new Promise(T=>{y=T});return b.push(T=>Ve(T,g)?(y(T),!0):!1),d})},subscribe(l,g){let b;return l==="*"?b=y=>g(y):b=y=>{y.type===l&&g(y)},R.push(b),b},unsubscribe(l){const g=R.indexOf(l);return g>-1?(R.splice(g,1),!0):!1}}}function Ve(r,s){if(s===void 0||r.data===void 0)return!0;const c=JSON.parse(r.data);if(typeof s!="object"&&s!==null)return c===s;if(s!==null&&c!==null&&typeof c=="object"){for(const f of Object.keys(s)){const u=c[f],i=s[f];if(i instanceof RegExp)if(typeof u=="string"){if(u.match(i)===null)return!1}else return!1;else if(u!==s[f])return!1}return!0}return!1}function pe(r){let s="";const c=[];for(let f of r){const u=typeof f;u==="string"||u==="number"||u==="boolean"||f===null?s?s+=" "+f:s=""+f:(s&&(c.push(s),s=""),c.push(f))}if(s&&c.push(s),c.length!==0)return c.length===1?c[0]:c}function We(r,s,c,f,u,i){let m=!1,A="";if(c==="")m=!0,A="Event type cannot be empty";else if(c.charAt(0)==="!"&&i!==r&&c!==p.Error&&c!==p.Warning&&c!==p.Info)m=!0,A="Event type cannot start with reserved prefix: "+c;else if(s.type=c,u!==void 0)try{f?s.data=f(u):s.data=JSON.stringify(u)}catch(R){m=!0,A="Event strinfication error: "+R}else u=void 0;m&&(s.type=p.Error,s.data=A)}function Ge(r,s,c=!1){if(!s||!r||r.charAt(0)!=="!")return s;const f=c?"%c":"";try{const u=JSON.parse(""+s);if(r===p.CycleStart||r===p.CycleComplete)return`${f}0`;if(r===p.Info||r===p.Warning||r===p.Error)return`${f}${s.replace(/"/g,"")}`;if(r===p.ProcessingPause||r===p.ProcessingResume||r===p.ProcessingEnd)return`${JSON.parse(s).processId}`;if(r===p.ProcessingStart){const i=u;if(i.name==="!StoreInit")return`${i.name} (${f}${i.storeId}${f})`;if(i.name==="!Compute"){const m=i.isRenderer?"R":"";return`${i.name} #${i.computeCount} (${f}${i.processorId}${f}) P${i.processorPriority}${m} ${i.trigger}`}else return i.name==="!Reconciliation"?`${i.name} #${i.index} - ${i.processorCount} processor${i.processorCount!==1?"s":""}`:i.name==="!ArrayUpdate"?`${i.name} (${f}${i.objectId}${f})`:`${f}${i.name}`}else if(r===p.New){const i=u;return i.objectId===void 0?s:`${i.objectType}: ${f}${i.objectId}`}else if(r===p.Dispose){const i=u;return i.objectId===void 0?s:`${f}${i.objectId}`}else if(r===p.Get){const i=u;return`${f}${i.objectId}.${i.propName}${f} -> ${f}${he(i.propValue)}`}else if(r===p.Set){const i=u;return`${f}${i.objectId}.${i.propName}${f} = ${f}${he(i.toValue)}${f} (prev: ${he(i.fromValue)})`}else if(r===p.ProcessorDirty){const i=u;return`${f}${i.processorId}${f} <- ${f}${i.objectId}.${i.propName}`}else if(r===p.ProcessorSkipped)return`${f}${u.processorId}`}catch{}return s}function he(r){if(r===void 0)return"undefined";if(r===null)return"null";if(typeof r=="object"){const s=k(r);return s?s.id:JSON.stringify(r)}else return typeof r=="string"?"'"+r.replace(/\'/g,"\\'")+"'":""+r}function Te(){}function ye(r,s,c,f=Te,u=Te){let i=null,m,A,R,E;return(..._)=>{f(),i=s();let N,O=!0;m=void 0;let j;try{j=r(..._),j&&j.next&&typeof j.next=="function"?(m=j,N=j.next(),O=!!N.done):j&&typeof j=="object"&&typeof j.then=="function"&&typeof j.catch=="function"&&j.catch(L=>{c(L)})}catch(L){c(L)}return u(O),O?(i.end(),i=null):(i.pause(),w(N,m)),m!==void 0?(A&&E&&E("Processing Cancelled"),A=R=E=void 0,O?Promise.resolve(j):(A=new Promise((L,G)=>{R=L,E=G}),A)):j};function w(_,N){if(_&&!_.done){const O=_.value,j=L=>{c(L)};O&&O.then?O.then(L=>{z(L,N)}).catch(j):Promise.resolve().then(()=>{z(O,N)}).catch(j)}}function z(_,N){if(N!==m||!i)return;if(f()===!1){F();return}i.resume();let O,j=!0;try{O=N.next(_),j=!!O.done}catch(L){c(L),j=!0,E&&E(L),F()}u(j),j?(i.end(),R&&R(O.value)):(i.pause(),w(O,N))}function F(){R=E=void 0}}const Xe="~";function Ue(r,s,c,f,u,i,m,A,R,E,w=!0,z=!1){let F=0,_=-1,N=!0,O=!1,j=!1,L=new Set,G=new Set,K,J=-1,Z="DirectCall",B,H=!1;s&&s[0]===Xe&&(R!=null?H=!0:te(`(${r}) Eager processors must not use the ~ prefix`));function te(y){m({type:p.Error,data:y})}let C,D;g(f);const V={get id(){return r},get target(){return R},get reconciliationId(){return J},get autoCompute(){return w},get priority(){return c},get computeCount(){return F},get dirty(){return N},get isRenderer(){return z},get isLazy(){return H},get disposed(){return O},get dependencies(){return O?[]:Array.from(L).sort()},onDirty:null,notifyChange(y,d){if(O||j)return!1;if(!N&&L.has(l(y,d))){if(N=!0,m({type:p.ProcessorDirty,processorId:r,objectId:y,propName:d}),this.onDirty)try{this.onDirty()}catch(T){te(`(${r}) onDirty callback execution error: ${T}`)}return!0}return!1},registerDependency(y,d,T){!j||O||T==="then"&&y.then===void 0||(L.add(l(d,T)),G.add(d),He(V,k(i(d))))},compute(y=!1,d="DirectCall",T=-1){var X;if(O||!y&&!w&&(d==="Init"||d==="Reconciliation"))return;let q=!0;const ne=d==="TargetRead"&&u.size>0;if(H&&!ne&&(q=((X=k(R))===null||X===void 0?void 0:X.hasExternalPropListener)||!1),q&&N||y){N=!1,L.clear(),K=G,G=new Set,F++,J=T,Z=d,D&&(g(D),D=void 0),B={processorId:r,processorName:s,computeCount:F,maxComputeCount:_};let e;R!==null?e=C(R,B):e=C(B),e&&typeof e=="object"&&typeof e.catch=="function"&&e.catch(()=>{}),w&&L.size===0&&te(`(${r}) No dependencies found: processor will never be re-executed`)}else q||m({type:p.ProcessorSkipped,processorId:r})},updateComputeFn(y){D=y},dispose(){if(O)return!1;O=!0,j=!1,E&&E(r);for(const y of G)Ae(V,k(i(y)));return m({type:p.Dispose,objectId:r}),!0}};return m({type:p.New,objectId:r,objectType:M.TraxObjectType.Processor}),V.compute(!1,"Init"),V;function l(y,d){return y+"."+d}function g(y){C=ye(y,()=>A({type:"!PCS",name:"!Compute",processorId:r,processorPriority:c,trigger:Z,isRenderer:z,computeCount:F}),d=>{te(`(${r}) Compute error: ${d}`)},()=>(j=!0,u.add(V),O?!1:void 0),d=>{j=!1,u.shift(),_=(B==null?void 0:B.maxComputeCount)||-1,d&&_>-1&&F>=_?V.dispose():b()})}function b(){if(K){for(const y of K)G.has(y)||Ae(V,k(i(y)));K=void 0}}}var Fe=globalThis&&globalThis.__awaiter||function(r,s,c,f){function u(i){return i instanceof c?i:new c(function(m){m(i)})}return new(c||(c=Promise))(function(i,m){function A(w){try{E(f.next(w))}catch(z){m(z)}}function R(w){try{E(f.throw(w))}catch(z){m(z)}}function E(w){w.done?i(w.value):u(w.value).then(A,R)}E((f=f.apply(r,s||[])).next())})};const ge=new WeakMap,oe=Symbol("trax.proxy.target"),Ie=Symbol("trax.dict.size"),Re=/(\/|\>|\.|\#)/g,se="data",Je=":",Be="*",Ye="-",Ke="$",_e="$",me="#",ve=">",Ze="/",ce="☆trax.dictionary.size☆";function He(r,s){s&&(s.propListeners||(s.propListeners=new Set),s.propListeners.add(r),s.contentProcessors!==void 0&&!s.hasExternalPropListener&&(!r.target||k(r.target)!==s)&&(s.hasExternalPropListener=!0))}function Ae(r,s){if(s&&s.propListeners)if(s.propListeners.delete(r),s.propListeners.size===0)s.propListeners=void 0,s.hasExternalPropListener=!1;else if(s.contentProcessors){let c=!1;for(const f of s.propListeners)!c&&(!f.target||k(f.target)!==s)&&(c=!0);s.hasExternalPropListener=c}else s.hasExternalPropListener=!1}function k(r){return r?ge.get(r[oe]||r):void 0}function fe(r,s,c,f){const u={id:s,type:c,storeId:f,propListeners:void 0,computedProps:void 0,computedContent:void 0,awLevel:void 0,dictSize:void 0,contentProcessors:void 0,hasExternalPropListener:!1,processingLazyCheck:!1};return ge.set(r[oe]||r,u),u}function qe(r){ge.delete(r[oe]||r)}function Qe(){const r={};let s=0;const c=new Map,f=new Map,u=new Map,i=new Map,m=Array.isArray;let A=0,R=0;const E=new ae;let w=new ae,z=0;const _=Me(r,e=>JSON.stringify(e,(t,o)=>{if(typeof o=="object"){const a=k(o);return a?`[TRAX ${a.id}]`:o}else if(typeof o=="function")return"[Function]";return o}),()=>{b(),O.processChanges()});let N=!1;const O={log:_,createStore(e,t){return ne(e,"",t)},get pendingChanges(){return w.size>0},processChanges(){if(w.size>0){z++;const e=V({type:"!PCS",name:"!Reconciliation",index:z,processorCount:R});let t=w.shift();for(;t;)t.reconciliationId===z?C(`(${t.id}) Circular reference: Processors cannot run twice during reconciliation`):t.compute(!1,"Reconciliation",z),t=w.shift();e.end()}},reconciliation(){return Fe(this,void 0,void 0,function*(){let e=_.lastEvent();(!e||e.type===p.CycleComplete)&&(yield Promise.resolve(1)),(w.size||e&&e.type!==p.CycleComplete)&&(yield _.awaitEvent(p.CycleComplete))})},isTraxObject(e){return k(e)!==void 0},getTraxId(e){var t;return((t=k(e))===null||t===void 0?void 0:t.id)||""},getTraxObjectType(e){var t;return((t=k(e))===null||t===void 0?void 0:t.type)||M.TraxObjectType.NotATraxObject},getProcessor(e){return u.get(e)},getStore(e){return c.get(e)},getData(e){return y(e)||void 0},getActiveProcessor(){return E.peek()},updateArray(e,t){if(!m(e)||!m(t)){C("updateArray: Invalid argument (array expected)");return}const o=j(e),a=V({type:p.ProcessingStart,name:"!ArrayUpdate",objectId:o}),n=e.length,x=t.length;for(let S=0;x>S;S++)e[S]=t[S];if(x<n){for(let S=x;n>S;S++)e[S]=void 0;e.splice(x,n-x)}a.end()},updateDictionary(e,t){if(e===null||typeof e!="object"){C("updateDictionary: Invalid argument (object expected)");return}const o=j(e),a=V({type:p.ProcessingStart,name:"!DictionaryUpdate",objectId:o}),n=O.getObjectKeys(e),x=O.getObjectKeys(t);for(const S of n)x.includes(S)||delete e[S];for(const S of x)e[S]=t[S];a.end()},getObjectKeys(e){return k(e)?e[Ie]?Object.keys(e):[]:Object.keys(e)}};function j(e){const t=k(e);if(t){te(t);const o=E.peek();return o&&(t.computedContent?o.id!==t.computedContent&&C(`Computed content conflict: ${t.id} can only be changed by ${t.computedContent}`):t.computedContent=o.id),t.id}return""}const L={get(e,t){const o=k(e);if(t===oe)return e;if(t==="toJSON")return;if(typeof t=="string"){let a,n=!1;if(o){const x=E.peek();x&&x.registerDependency(e,o.id,t),g(o),n=t!=="then"&&t!=="constructor"||a!==void 0,e[t]!==void 0&&(a=e[t]=G(e[t],t,o)),n&&D({type:"!GET",objectId:o.id,propName:t,propValue:a})}else a=e[t];return a}else if(t===Ie&&o){let a=o.dictSize;a===void 0&&(o.dictSize=a=Object.keys(e).length);const n=E.peek();return n&&n.registerDependency(e,o.id,ce),g(o),D({type:"!GET",objectId:o.id,propName:ce,propValue:a}),a}return e[t]},set(e,t,o){if(typeof t!="symbol"){const a=e[t],n=k(e);if(n){const x=E.peek();if(te(n),x){let S=n.computedContent||void 0;if(!n.computedContent){let U=n.computedProps;U||(U=n.computedProps={}),S=U[t],S||(U[t]=x.id)}S&&S!==x.id&&(n.computedContent?C(`Computed content conflict: ${n.id}.${t} can only be set by ${S}`):C(`Computed property conflict: ${n.id}.${t} can only be set by ${S}`),o=a)}else n.computedContent&&(C(`Computed content conflict: ${n.id}.${t} can only be set by ${n.computedContent}`),o=a);if(a!==o){let S=!1,U=!1;if(m(e)){const re=e.length;o=e[t]=G(o,""+t,n),S=e.length!==re}else{o=e[t]=G(o,""+t,n);const re=n.dictSize;if(a===void 0&&re!==void 0){const le=Object.keys(e).length;le!==re&&(n.dictSize=le,U=!0)}}D({type:"!SET",objectId:n.id,propName:t,fromValue:a,toValue:o}),typeof t=="string"&&l(n,t),S&&l(n,"length"),U&&l(n,ce)}}else e[t]=o}return!0},deleteProperty(e,t){if(typeof t=="string"&&t in e){const o=k(e);return o&&o.dictSize&&(o.dictSize--,l(o,ce)),delete e[t],!0}return!1}};function G(e,t,o){if(e!=null&&typeof e=="object"){if(e[oe])return e;let a=k(e);if(a)return e;let n=0;if(t[0]===_e){let x=0,S=t.length;for(;x<S&&t[x]===_e;){n++;x++}}else o.awLevel&&(n=o.awLevel);if(n!==1&&!a&&(e=K(o.id+Be+t,e,o.storeId,!0),n&&n>1)){let x=k(e);x&&!x.awLevel&&(x.awLevel=n-1)}}return e}function K(e,t,o,a=!1){const n=y(e);if(n)if(t&&a){const U=e;for(;s++,e=U+Ke+s,!!f.get(e););}else return n;let x;m(t)?x=fe(t,e,M.TraxObjectType.Array,o):x=fe(t,e,M.TraxObjectType.Object,o),D({type:"!NEW",objectId:e,objectType:x.type});const S=new Proxy(t,L);return d(e,S,o),S}return O;function J(e){const t=e.priority,o=e.isRenderer;w.insert((a,n)=>{if(n)if(o){if(n.isRenderer)return t<=n.priority?e:void 0}else return n.isRenderer||t<=n.priority?e:void 0;else return e})}function Z(e,t){let o="";if(m(e)?o=e.map(a=>{if(typeof a=="object"){const n=k(a);if(n){const x=n.id;if(t){const S=t.length+1;if(x.length>S&&x.slice(0,S)===t+"/")return x.slice(S)}return x.replace(/\//g,Ye)}else return C("Invalid id param: not a trax object"),B()}return""+a}).join(Je):o=e,o.match(Re)){const a=o.replace(Re,"");C(`Invalid trax id: ${o} (changed into ${a})`),o=a}return o}function B(){return""+Math.floor(Math.random()*1e5)}function H(e,t,o){let a=Z(e,t);return o?t+me+a:t+Ze+a}function te(e,t){if(e){o(e.computedContent)&&(e.computedContent=void 0);const a=e.computedProps;a&&t&&o(a[t])&&(a[t]=void 0)}function o(a){return!!(a&&!u.has(a))}}function C(e){_.error("[TRAX] "+e)}function D(e){e.type===p.Error?C(""+e.data):e.type===p.Info?_.info(""+e.data):_.event(e.type,e,r)}function V(e){return _.startProcessingContext(e,r)}function l(e,t){const o=e.propListeners;if(o)for(const a of o)a.notifyChange(e.id,t)&&J(a)}function g(e){if(!e.processingLazyCheck&&e.contentProcessors){e.processingLazyCheck=!0;for(const t of e.contentProcessors)t.isLazy&&t.compute(!1,"TargetRead");e.processingLazyCheck=!1}}function b(){!N&&globalThis.__TRAX_DEVTOOLS__&&(N=!0,console.log("[Trax] DevTools detected"),globalThis.__TRAX_DEVTOOLS__.connectTrax(O))}function y(e){const t=f.get(e);return t&&t.deref()||null}function d(e,t,o){f.set(e,new WeakRef(t)),X(e,o)}function T(e,t,o){if(t&&qe(t),o){if(o.contentProcessors){for(const a of o.contentProcessors)a.dispose();o.contentProcessors=void 0}q(e,o.storeId)}return D({type:"!DEL",objectId:e}),f.delete(e)}function X(e,t){let o=i.get(t);o||(o=new Set,i.set(t,o)),o.add(e)}function q(e,t){let o=i.get(t);o&&o.delete(e)}function ne(e,t,o,a){const n=Le(e,t,!0);let x,S=!0,U=!1;const re=V({type:"!PCS",name:"!StoreInit",storeId:n}),le=typeof o=="function"?o:h=>{h.init(o)},de={get id(){return n},get data(){return x},get disposed(){return U},createStore(h,P){const $=ne(h,n,P,tt);return X($.id,n),$},init(h,P){return S?x=be(se,h,!0,P):C(`(${n}) Store.init can only be called during the store init phase`),x},add(h,P,$){return be(h,P,!1,$)},get(h){const P=H(h,n,!1);return y(P)||void 0},remove(h){const P=k(h);let $="";if(P)if($=P.id,P.type===M.TraxObjectType.Processor)C(`(${$}) Processors cannot be disposed through store.remove()`);else if(P.type===M.TraxObjectType.Store)C(`(${$}) Stores cannot be disposed through store.remove()`);else if($.slice(n.length+1)===se)C(`(${$}) Root objects cannot be disposed through store.remove()`);else return T($,h,P);return!1},compute(h,P,$,I){const v=Z(h,n),W=H(v,n,!0);return De(W,v,P,null,$,I)},getProcessor(h){const P=H(h,n,!0);return u.get(P)},getStore(h){const P=Le(h,n,!1);return c.get(P)},dispose(){return Pe()},async(h,P){let $="[ASYNC]",I;typeof h=="string"?($=h,I=P):I=h;const v=ye(I,()=>_.startProcessingContext({name:n+"."+$+"()",storeId:n}),W=>{C(`(${n}.${$}) error: ${W}`)});return v.updateAsyncName=W=>{$=W},v}};fe(de,n,M.TraxObjectType.Store,""),D({type:"!NEW",objectId:n,objectType:M.TraxObjectType.Store}),c.set(n,de);function Pe(){if(U)return!1;c.delete(n),U=!0,a&&a(n);const h=i.get(n);if(h){i.delete(n);const P=n.length;let $,I;for(const v of h)I=void 0,$=v.charAt(P),$===ve?(I=c.get(v),I&&I.dispose()):$===me?(I=u.get(v),I&&I.dispose(),u.get(v)&&(console.error("Unexpected processor dispose error"),u.delete(v))):T(v);h.clear()}return D({type:"!DEL",objectId:n}),!0}function tt(h){q(h,n)}function nt(h){u.delete(h)&&(R--,q(h,n))}function rt(h){const P=h;for(const $ of Object.keys(P))if(typeof P[$]=="function"){const I=P[$];typeof I.updateAsyncName=="function"?I.updateAsyncName($):P[$]=ye(I,()=>_.startProcessingContext({name:n+"."+$+"()",storeId:n}),v=>{C(`(${n}.${$}) error: ${v}`)})}}let Q;try{Q=le(de),S=!1,Q!==void 0&&(Q!==null&&typeof Q=="object"?rt(Q):(C(`createStore init function must return a valid object (${n})`),Q={}))}catch(h){C(`createStore init error (${n}): ${h}`),Q={}}if(S=!1,it(),Q===void 0)return re.end(),de;const ie=Q;if(typeof ie.dispose=="function"){const h=ie.dispose;ie.dispose=()=>{h.call(Q),Pe()}}else ie.dispose=Pe;return ie.id&&C(`Store id will be overridden and must not be provided by init function (${n})`),ie.id=n,re.end(),ie;function it(){x==null&&(C(`(${n}) createStore init must define a root data object - see also: init()`),x=be(se,{},!0))}function ot(){return U?(C(`(${n}) Stores cannot be used after being disposed`),!1):!0}function Le(h,P,$=!0){let I=Z(h);P!==""&&(I=P+ve+I);let v="";if($){let W=c.get(I),Y=0;for(;W;)v=""+ ++Y,W=W=c.get(I+v)}return I+v}function be(h,P,$,I){let v=Z(h,n);if($||v===se&&(C("Store.add: Invalid id 'data' (reserved)"),v=B()),ot()){(P==null||typeof P!="object")&&(C(`(${n}) Store.add(${h}): Invalid init object parameter: [${typeof P}]`),P={});const W=K(H(v,n,!1),P,n);if(I!==void 0){const Y=k(W),ze=[];for(const xe of Object.getOwnPropertyNames(I)){const st=`${n}${me}${v}[${xe}]`;ze.push(De(st,xe,I[xe],W,!0,!1))}Y.contentProcessors=ze}return W}else return P}function De(h,P,$,I,v,W){let Y=u.get(h);return Y?(Y.updateComputeFn($),Y):(A++,R++,Y=Ue(h,P,A,$,E,y,D,V,I,nt,v,W),Y.disposed||(fe(Y,h,M.TraxObjectType.Processor,n),u.set(h,Y),X(h,n)),Y)}}}let ue;globalThis.trax?ue=globalThis.trax:ue=Qe(),globalThis.trax=ue;const et=ue;M.trax=et,M.traxEvents=p,Object.defineProperty(M,Symbol.toStringTag,{value:"Module"})});