tempest.games 0.2.17 → 0.2.19
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.
- package/CHANGELOG.md +15 -0
- package/app/assets/index-DikmBhXT.js +109 -0
- package/app/index.html +1 -1
- package/bin/backend.bun.js +234 -234
- package/bin/backend.worker.game.bun.js +14 -3
- package/bin/backend.worker.tribunal.bun.js +123 -110
- package/bin/frontend.bun.js +61 -50
- package/bin/setup-db.bun.js +55 -55
- package/package.json +20 -20
- package/app/assets/index-DV2ldfnq.js +0 -109
|
@@ -1,5 +1,16 @@
|
|
|
1
1
|
#!/usr/bin/env bun
|
|
2
2
|
// @bun
|
|
3
|
-
var De=Object.create;var{getPrototypeOf:ve,defineProperty:ee,getOwnPropertyNames:Be}=Object;var Ge=Object.prototype.hasOwnProperty;var Le=(e,o,n)=>{n=e!=null?De(ve(e)):{};let t=o||!e||!e.__esModule?ee(n,"default",{value:e,enumerable:!0}):n;for(let r of Be(e))if(!Ge.call(t,r))ee(t,r,{get:()=>e[r],enumerable:!0});return t};var Xe=(e,o)=>()=>(o||e((o={exports:{}}).exports,o),o.exports);var Ve=(e,o)=>{for(var n in o)ee(e,n,{get:o[n],enumerable:!0,configurable:!0,set:(t)=>o[n]=()=>t})};var Ne=(e,o)=>()=>(e&&(o=e(e=0)),o);var Ue=import.meta.require;class j extends Promise{fate;resolve;reject;done=!1;constructor(e){let o,n;super((t,r)=>{o=t,n=r});this.resolve=o,this.reject=n,this.use(e instanceof Promise?e:new Promise(e))}pass(e,o){if(e===this.fate)this.resolve(o),this.done=!0}fail(e,o){if(e===this.fate)this.reject(o),this.done=!0}use(e){if(e instanceof Promise){let o=e;this.fate=o,o.then((n)=>{this.pass(o,n)},(n)=>{this.fail(o,n)})}else this.resolve(e),this.fate=void 0}}var to=(e,o,n=Se.STORE)=>{return pe(n,{key:`${e.key}:JSON`,get:({get:t})=>o.toJson(t(e)),set:({set:t},r)=>{t(e,o.fromJson(r))}})};function lo(e,o,n){return oe(e,{key:`${o.key}:JSON`,get:(r)=>({get:i})=>{let l=i(o,r);return n.toJson(l)},set:(r)=>({set:i},l)=>{i(o,r,n.fromJson(l))}},["mutable","json"])}var $=(e)=>JSON.parse(e),u=(e)=>JSON.stringify(e),ao=[Array.prototype,Boolean.prototype,Number.prototype,Object.prototype,String.prototype];function a(e){while(e.child!==null)e=e.child;return e}class Z{_buffer;_index=0;constructor(e){let o;if(typeof e==="number")o=e;else o=e.length;this._buffer=Array.from({length:o})}get buffer(){return this._buffer}get index(){return this._index}add(e){this._buffer[this._index]=e,this._index=(this._index+1)%this._buffer.length}copy(){let e=new Z([...this._buffer]);return e._index=this._index,e}}var qe={atom_family:"atom",molecule_family:"molecule",mutable_atom_family:"mutable_atom",readonly_held_selector_family:"readonly_held_selector",readonly_pure_selector_family:"readonly_pure_selector",writable_held_selector_family:"writable_held_selector",writable_pure_selector_family:"writable_pure_selector"};function ke(e,o){let n=u(o),t=`${e.key}(${n})`,r=qe[e.type],i={key:t,type:r};return Object.assign(i,{family:{key:e.key,subKey:n}}),Object.assign(i,{counterfeit:!0}),i}function E(e){let o={key:e.key,type:e.type};if("family"in e)o.family=e.family;return o}var ne=(e)=>(o,n,t,r,...i)=>{console[e](`${o} ${n} "${t}" ${r}`,...i)},Qe={error:ne("error"),info:ne("info"),warn:ne("warn")};class te{logLevel;filter;logger;constructor(e,o,n=Qe){this.logLevel=e,this.filter=o,this.logger=n}error=(...e)=>{let o=this.filter?.(...e)??!0;if(this.logLevel!==null){if(o===!0)this.logger.error(...e);else if(o!==!1)this.logger.error(...o)}};info=(...e)=>{let o=this.filter?.(...e)??!0;if(this.logLevel==="info"){if(o===!0)this.logger.info(...e);else if(o!==!1)this.logger.info(...o)}};warn=(...e)=>{let o=this.filter?.(...e)??!0;if(this.logLevel!=="error"&&this.logLevel!==null){if(o===!0)this.logger.warn(...e);else if(o!==!1)this.logger.warn(...o)}}}class _{a;b;cardinality;relations=new Map;contents=new Map;isAType;isBType;isContent;makeContentKey=(e,o)=>`${e}:${o}`;warn;getRelatedKeys(e){return this.relations.get(e)}addRelation(e,o){let n=this.relations.get(e),t=this.relations.get(o);if(n)n.add(o);else n=new Set([o]),this.relations.set(e,n);if(t)t.add(e);else t=new Set([e]),this.relations.set(o,t)}deleteRelation(e,o){let n=this.relations.get(e);if(n){if(n.delete(o),n.size===0)this.relations.delete(e);let t=this.relations.get(o);if(t){if(t.delete(e),t.size===0)this.relations.delete(o)}}}replaceRelationsUnsafely(e,o){this.relations.set(e,new Set(o));for(let n of o){let t=new Set().add(e);this.relations.set(n,t)}}replaceRelationsSafely(e,o){let n=this.relations.get(e),t=this.isAType?.(e)?e:void 0,r=t===void 0?e:void 0;if(n)for(let i of n){t??=i,r??=i;let l=this.relations.get(i);if(l){if(l.size===1)this.relations.delete(i);else l.delete(e);this.contents.delete(this.makeContentKey(t,r))}}this.relations.set(e,new Set(o));for(let i of o){let l=this.relations.get(i);if(l)l.add(e);else l=new Set().add(e),this.relations.set(i,l)}}getContentInternal(e){return this.contents.get(e)}setContent(e,o){this.contents.set(e,o)}deleteContent(e){this.contents.delete(e)}constructor(e,o){if(this.a=e.between[0],this.b=e.between[1],this.cardinality=e.cardinality,!o?.externalStore)this.relations=new Map(e.relations?.map(([n,t])=>[n,new Set(t)])),this.contents=new Map(e.contents);if(this.isAType=o?.isAType??null,this.isBType=o?.isBType??null,this.isContent=o?.isContent??null,o?.makeContentKey)this.makeContentKey=o.makeContentKey;if(o?.externalStore){let n=o.externalStore;if(this.has=(t,r)=>n.has(t,r),this.addRelation=(t,r)=>{n.addRelation(t,r)},this.deleteRelation=(t,r)=>{n.deleteRelation(t,r)},this.replaceRelationsSafely=(t,r)=>{n.replaceRelationsSafely(t,r)},this.replaceRelationsUnsafely=(t,r)=>{n.replaceRelationsUnsafely(t,r)},this.getRelatedKeys=(t)=>n.getRelatedKeys(t),n.getContent)this.getContentInternal=(t)=>{return n.getContent(t)},this.setContent=(t,r)=>{n.setContent(t,r)},this.deleteContent=(t)=>{n.deleteContent(t)};for(let[t,r]of e.relations??[]){let i=this.isAType?.(t)?t:void 0,l=i===void 0?t:void 0;for(let c of r)i??=c,l??=c,this.addRelation(i,l)}for(let[t,r]of e.contents??[])this.setContent(t,r)}if(o?.warn)this.warn=o.warn}toJSON(){return{between:[this.a,this.b],cardinality:this.cardinality,relations:[...this.relations.entries()].map(([e,o])=>[e,[...o]]),contents:[...this.contents.entries()]}}set(...e){let o,n,t;switch(e.length){case 1:{let r=e[0];o=r[this.a],n=r[this.b],t=void 0;break}case 2:{let r=e[0];if(typeof r==="string")[o,n]=e;else o=r[this.a],n=r[this.b],t=e[1];break}default:{o=e[0],n=e[1],t=e[2];break}}switch(this.cardinality){case"1:1":{let r=this.getRelatedKey(o);if(r&&r!==n)this.delete(o,r)}case"1:n":{let r=this.getRelatedKey(n);if(r&&r!==o)this.delete(r,n)}break;case"n:n":}if(t){let r=this.makeContentKey(o,n);this.setContent(r,t)}return this.addRelation(o,n),this}delete(e,o){o=typeof o==="string"?o:e[this.b];let n=typeof e==="string"?e:e[this.a];if(n===void 0&&typeof o==="string"){let t=this.getRelatedKeys(o);if(t)for(let r of t)this.delete(r,o)}if(typeof n==="string"&&o===void 0){let t=this.getRelatedKeys(n);if(t)for(let r of t)this.delete(n,r)}if(typeof n==="string"&&typeof o==="string"){this.deleteRelation(n,o);let t=this.makeContentKey(n,o);this.deleteContent(t)}return this}getRelatedKey(e){let o=this.getRelatedKeys(e);if(o){if(o.size>1)this.warn?.(`${o.size} related keys were found for key "${e}": (${[...o].map((t)=>`"${t}"`).join(", ")}). Only one related key was expected.`);let n;for(let t of o){n=t;break}return n}}replaceRelations(e,o,n){let t=!Array.isArray(o),r=t?Object.keys(o):o;if(n?.reckless)this.replaceRelationsUnsafely(e,r);else this.replaceRelationsSafely(e,r);if(t)for(let i of r){let l=this.makeContentKey(e,i),c=o[i];this.setContent(l,c)}return this}getContent(e,o){let n=this.makeContentKey(e,o);return this.getContentInternal(n)}getRelationEntries(e){let o=e[this.a],n=e[this.b];if(o!==void 0&&n===void 0){let t=this.getRelatedKeys(o);if(t)return[...t].map((r)=>{return[r,this.getContent(o,r)]})}if(o===void 0&&n!==void 0){let t=this.getRelatedKeys(n);if(t)return[...t].map((r)=>{return[r,this.getContent(r,n)]})}return[]}has(e,o){if(o)return this.getRelatedKeys(e)?.has(o)??!1;return this.relations.has(e)}}class f{Subscriber;subscribers=new Map;subscribe(e,o){return this.subscribers.set(e,o),()=>{this.unsubscribe(e)}}unsubscribe(e){this.subscribers.delete(e)}next(e){let o=this.subscribers.values();for(let n of o)n(e)}}class re extends f{state;constructor(e){super();this.state=e}next(e){this.state=e,super.next(e)}}function g(e){return"epoch"in e.transactionMeta}function F(e){return"phase"in e.transactionMeta}function ue(e){return e[0].toUpperCase()+e.slice(1)}function L(e){return e.type.split("_").map(ue).join(" ")}class z extends Error{constructor(e,o){super(`${L(e)} ${u(e.key)} not found in store "${o.config.name}".`)}}var I=(e)=>(o)=>e instanceof Function?e(o):e;var W=(e,o)=>{if(e.valueMap.has(o.key))return e.logger.info("\uD83D\uDCD6",o.type,o.key,"reading cached value"),de(o,e);switch(o.type){case"readonly_held_selector":case"readonly_pure_selector":case"writable_held_selector":case"writable_pure_selector":return e.logger.info("\uD83E\uDDEE",o.type,o.key,"computing value"),o.get();case"atom":case"mutable_atom":{let n=o.default,t;if(n instanceof Function)t=n();else t=n;return e.logger.info("\uD83D\uDC81","atom",o.key,"could not find cached value; using default",t),t}}};var be=(e,o)=>{if(e.operation.open){let n=performance.now();return e.logger.info("\u2757",o.type,o.key,`deferring setState at T-${n} until setState for "${e.operation.token.key}" is done`),n}e.operation={open:!0,done:new Set,prev:new Map,time:Date.now(),token:o},e.logger.info("\u2B55",o.type,o.key,`operation start in store "${e.config.name}"${!F(e)?"":` ${e.transactionMeta.phase} "${e.transactionMeta.update.key}"`}`)},Ke=(e)=>{if(e.operation.open)e.logger.info("\uD83D\uDD34",e.operation.token.type,e.operation.token.key,`operation done in store "${e.config.name}"`);e.operation={open:!1},e.on.operationClose.next(e.operation)},xe=(e,o)=>{if(!e.operation.open)return e.logger.error("\uD83D\uDC1E","unknown",o,"isDone called outside of an operation. This is probably a bug in AtomIO."),!0;return e.operation.done.has(o)},P=(e,o)=>{if(!e.operation.open){e.logger.error("\uD83D\uDC1E","unknown",o,"markDone called outside of an operation. This is probably a bug in AtomIO.");return}e.operation.done.add(o)};var Fe=(e,o,n)=>{switch(o.type){case"mutable_atom":e.logger.info("\uD83D\uDCE2",o.type,o.key,"is now (",n.newValue,") subscribers:",o.subject.subscribers);break;case"atom":case"writable_pure_selector":case"readonly_pure_selector":case"writable_held_selector":case"readonly_held_selector":e.logger.info("\uD83D\uDCE2",o.type,o.key,"went (",n.oldValue,"->",n.newValue,") subscribers:",o.subject.subscribers)}o.subject.next(n)};var ie=(e,o)=>{let n=a(e),t=n.selectorAtoms.getRelatedKeys(o.key);if(n.logger.info("\uD83E\uDDF9",o.type,o.key,t?`evicting ${t.size} states downstream:`:"no downstream states",t??"to evict"),t){if(n.operation.open)n.logger.info("\uD83E\uDDF9",o.type,o.key,`[ ${[...n.operation.done].join(", ")} ] already done`);for(let r of t){if(xe(n,r))continue;we(r,n),P(n,r)}}};var Ae=(e,o,n)=>{let t=W(e,o),r=t;if(o.type==="mutable_atom"&&F(e)){let{parent:l}=e;r=D(e,o,l)}if(r=I(n)(r),e.logger.info("\uD83D\uDCDD","atom",o.key,"set to",r),r=d(e,o.key,r,o.subject),Re(e,o.key))Me(e,o.key);P(e,o.key),ie(e,o);let i={oldValue:t,newValue:r};if(!F(e)){Fe(e,o,i);return}if(e.on.transactionApplying.state===null){let{key:l}=o;if(_e(i.newValue))return;let c={type:"atom_update",key:l,...i};if(o.family)c.family=o.family;e.transactionMeta.update.updates.push(c),e.logger.info("\uD83D\uDCC1","atom",l,"stowed (",i.oldValue,"->",i.newValue,")")}else if(o.key.startsWith("*")){let l=o.key.slice(1),c=e.atoms.get(l),T=e.valueMap.get(l);if(c.type==="mutable_atom"&&F(e)){let{parent:y}=e;T=D(e,c,y)}if(T.do(i.newValue)===null)ie(e,c)}};var X=(e,o,n)=>{switch(o.type){case"atom":case"mutable_atom":Ae(e,o,n);break;case"writable_pure_selector":case"writable_held_selector":o.set(n);break}};var V=(e,o)=>a(e).atoms.has(o),Ye=(e,o)=>a(e).writableSelectors.has(o),Ze=(e,o)=>a(e).readonlySelectors.has(o),ge=(e,o)=>V(e,o)||Ye(e,o)||Ze(e,o);var N=(e,o)=>{return a(e).selectorGraph.getRelationEntries({downstreamSelectorKey:o}).filter(([t,{source:r}])=>r!==o).map(([t,{source:r}])=>r).filter((t)=>ge(e,t))};var le=(e,o,n)=>{let t=[],r=N(e,o);while(r.length>0){let i=r.shift();if(n.has(i))continue;if(n.add(i),!V(e,i))r.push(...N(e,i));else if(!t.includes(i))t.push(i)}return t},We=(e,o)=>{let n=e.key,t=N(o,n),r=new Set;return t.flatMap((i)=>V(o,i)?i:le(o,i,r))};var Pe=(e,o,n,t,r)=>{let i=a(e),{type:l,key:c}=t;if(l==="atom"||l==="mutable_atom")i.selectorAtoms.set({selectorKey:n,atomKey:c}),e.logger.info("\uD83D\uDD0D",o,n,`discovers root atom "${c}"`);else{let T=le(e,c,r);e.logger.info("\uD83D\uDD0D",o,n,`discovers root atoms: [ ${T.map((m)=>`"${m}"`).join(", ")} ]`);for(let m of T)i.selectorAtoms=i.selectorAtoms.set({selectorKey:n,atomKey:m})}r.add(c)};var C=(e,o,n,t)=>({get:(...r)=>{let i=a(e),l;if(r.length===2){let[y,s]=r;l=w(e,y,s)}else[l]=r;let c=K(e,l),T=W(e,c),m=l.key;return e.logger.info("\uD83D\uDD0C",o,n,`registers dependency ( "${m}" =`,T,")"),i.selectorGraph.set({upstreamSelectorKey:m,downstreamSelectorKey:n},{source:l.key}),Pe(e,o,n,l,t),T},set:(...r)=>{let i,l;if(r.length===2)i=r[0],l=r[1];else{let m=r[0],y=r[1];l=r[2],i=w(e,m,y)}let c=a(e),T=K(c,i);X(c,T,l)},find:(...r)=>w(e,...r),json:(r)=>H(e,r)});var ce=(e,o,n)=>{let t=a(e),r=new f,i=new Set,{key:l,const:c}=o,T="readonly_held_selector",{get:m,find:y,json:s}=C(t,"readonly_held_selector",l,i),b={...o,type:"readonly_held_selector",subject:r,install:(S)=>ce(S,o,n),get:()=>{return o.get({get:m,find:y,json:s},c),d(a(e),l,c,r),i.clear(),c},...n&&{family:n}};t.readonlySelectors.set(l,b),e.logger.info("\u2728","readonly_held_selector",l,"=",c);let k={key:l,type:"readonly_held_selector"};if(n)k.family=n;return k};var ae=(e,o,n)=>{let t=a(e),r=new f,i=new Set,l=o.key,c="readonly_pure_selector",{get:T,find:m,json:y}=C(t,"readonly_pure_selector",l,i),s=()=>{let S=o.get({get:T,find:m,json:y});return d(a(e),l,S,r),i.clear(),S},p={...o,type:"readonly_pure_selector",subject:r,install:(S)=>ae(S,o,n),get:s,...n&&{family:n}};t.readonlySelectors.set(l,p);let b=s();e.logger.info("\u2728","readonly_pure_selector",l,"=",b);let k={key:l,type:"readonly_pure_selector"};if(n)k.family=n;return k};var Te=(e,o,n)=>{let t=a(e),r=new f,i=new Set,{key:l,const:c}=o,T="writable_held_selector",m=C(t,"writable_held_selector",l,i),{find:y,get:s,json:p}=m,b={find:y,get:s,json:p},k=(A=o.get,x=a(e))=>{return A(b,c),d(x,l,c,r),i.clear(),c},J={...o,type:"writable_held_selector",subject:r,install:(A)=>Te(A,o,n),get:k,set:(A)=>{let x=a(e),M=k(o.get,x),Y=I(A)(M);if(e.logger.info("\uD83D\uDCDD","writable_held_selector",l,"set (",M,"->",Y,")"),d(x,l,Y,r),P(x,l),g(x))r.next({newValue:Y,oldValue:M});o.set(m,Y)},...n&&{family:n}};t.writableSelectors.set(l,J);let Q=k();e.logger.info("\u2728","writable_held_selector",l,"=",Q);let R={key:l,type:"writable_held_selector"};if(n)R.family=n;return R};var v=(e,o,n)=>{let t=a(e),r=new f,i=new Set,l=o.key,c="writable_pure_selector",T=C(t,"writable_pure_selector",l,i),{find:m,get:y,json:s}=T,p={find:m,get:y,json:s},b=(R=o.get,A=a(e))=>{let x=R(p);return d(A,l,x,r),i.clear(),x},S={...o,type:"writable_pure_selector",subject:r,install:(R)=>v(R,o,n),get:b,set:(R)=>{let A=a(e),x=b(o.get,A),M=I(R)(x);if(e.logger.info("\uD83D\uDCDD","writable_pure_selector",l,"set (",x,"->",M,")"),d(A,o.key,M,r),P(A,o.key),g(A))r.next({newValue:M,oldValue:x});o.set(T,M)},...n&&{family:n}};t.writableSelectors.set(l,S);let J=b();e.logger.info("\u2728",S.type,S.key,"=",J);let Q={key:l,type:"writable_pure_selector"};if(n)Q.family=n;return Q};function pe(e,o){let n="set"in o,t="const"in o;if(t&&n){let i=Te(e,o,void 0);return e.on.selectorCreation.next(i),i}if(t){let i=ce(e,o,void 0);return e.on.selectorCreation.next(i),i}if(n){let i=v(e,o,void 0);return e.on.selectorCreation.next(i),i}let r=ae(e,o,void 0);return e.on.selectorCreation.next(r),r}function oe(e,o,n){let t=o.key,r="writable_pure_selector_family",i={key:t,type:"writable_pure_selector_family"},l=e.families.get(t);if(l)e.logger.error("\u2757","writable_pure_selector_family",t,`Overwriting an existing ${L(l)} "${l.key}" in store "${e.config.name}". You can safely ignore this warning if it is due to hot module replacement.`);let c=new f,m=Object.assign((y)=>{let s=u(y),p={key:t,subKey:s},b=`${t}(${s})`,k=a(e),S=v(k,{key:b,get:o.get(y),set:o.set(y)},p);return c.next({type:"state_creation",token:S}),S},i,{internalRoles:n,subject:c,install:(y)=>oe(y,o),default:(y)=>{return o.get(y)({get:(...p)=>B(e,...p),find:(...p)=>w(e,...p),json:(p)=>H(e,p)})}});return e.families.set(t,m),i}function Ce(e,o,n){let t=e.families.get(o.key);if(t===void 0)throw new z(o,e);let r=t(n),i=a(e);if(r.family){if(g(i))switch(r.type){case"atom":case"mutable_atom":e.on.atomCreation.next(r);break;case"writable_pure_selector":case"readonly_pure_selector":case"writable_held_selector":case"readonly_held_selector":e.on.selectorCreation.next(r);break}else if(F(i)&&i.on.transactionApplying.state===null)i.transactionMeta.update.updates.push({type:"state_creation",token:r})}return r}function he(e,o,n){let t=u(n),r=`${o.key}(${t})`,i=a(e),l;switch(o.type){case"atom_family":case"mutable_atom_family":l=i.atoms.get(r);break;case"writable_held_selector_family":case"writable_pure_selector_family":l=i.writableSelectors.get(r);break;case"readonly_held_selector_family":case"readonly_pure_selector_family":l=i.readonlySelectors.get(r);break}if(l)return E(l);return l}function w(e,o,n){let t=he(e,o,n);if(t)return t;let r=u(n),i=e.molecules.get(r);if(!i&&e.config.lifespan==="immortal"){let l=ke(o,n);return e.logger.error("\u274C",l.type,l.key,`was not found in store "${e.config.name}"; returned a counterfeit token.`),l}if(t=Ce(e,o,n),i)a(e).moleculeData.set(r,o.key);return t}function Je(e,o){if(o.family){let n=e.families.get(o.family.key);if(n)return n}}function h(e,...o){let n,t,r,i;if(o.length===2){if(n=o[0],i=o[1],t=Je(e,n)??null,t)r=n.family?$(n.family.subKey):null,n=w(e,t,r)}else t=o[0],r=o[1],i=o[2],n=w(e,t,r);if("counterfeit"in n&&"family"in n){let T=n.family.subKey,m=e.disposalTraces.buffer.find((y)=>y?.key===T);e.logger.error("\u274C",n.type,n.key,`could not be set because it was not found in the store "${e.config.name}".`,m?`This state was previously disposed:
|
|
4
|
-
${
|
|
5
|
-
${l.trace}`:"No previous disposal trace was found."),t.type){case"atom_family":case"mutable_atom_family":return e.defaults.get(t.key);case"readonly_pure_selector_family":case"writable_pure_selector_family":case"readonly_held_selector_family":case"writable_held_selector_family":{if(e.defaults.has(t.key))return e.defaults.get(n.family.key);let c=K(e,t).default(i);return e.defaults.set(t.key,c),c}}}return W(e,K(e,n))}class Ie{parent=null;child=null;valueMap=new Map;defaults=new Map;atoms=new Map;writableSelectors=new Map;readonlySelectors=new Map;atomsThatAreDefault=new Set;selectorAtoms=new _({between:["selectorKey","atomKey"],cardinality:"n:n"});selectorGraph=new _({between:["upstreamSelectorKey","downstreamSelectorKey"],cardinality:"n:n"},{makeContentKey:(...e)=>e.sort().join(":")});trackers=new Map;families=new Map;joins=new Map;transactions=new Map;transactionMeta={epoch:new Map,actionContinuities:new _({between:["continuity","action"],cardinality:"1:n"})};timelines=new Map;timelineTopics=new _({between:["timelineKey","topicKey"],cardinality:"1:n"});disposalTraces=new Z(100);molecules=new Map;moleculeJoins=new _({between:["moleculeKey","joinKey"],cardinality:"n:n"},{makeContentKey:(...e)=>e.sort().join(":")});moleculeGraph=new _({between:["upstreamMoleculeKey","downstreamMoleculeKey"],cardinality:"n:n"},{makeContentKey:(...e)=>e.sort().join(":")});moleculeData=new _({between:["moleculeKey","stateFamilyKey"],cardinality:"n:n"},{makeContentKey:(...e)=>e.sort().join(":")});miscResources=new Map;on={atomCreation:new f,atomDisposal:new f,selectorCreation:new f,selectorDisposal:new f,timelineCreation:new f,transactionCreation:new f,transactionApplying:new re(null),operationClose:new f,moleculeCreation:new f,moleculeDisposal:new f};operation={open:!1};config={name:"IMPLICIT_STORE",lifespan:"ephemeral"};loggers=[new te("warn",(e,o,n)=>!Oe(n))];logger={error:(...e)=>{for(let o of this.loggers)o.error(...e)},info:(...e)=>{for(let o of this.loggers)o.info(...e)},warn:(...e)=>{for(let o of this.loggers)o.warn(...e)}};constructor(e,o=null){if(this.config={...o?.config,...e},o!==null){if(this.valueMap=new Map(o?.valueMap),this.operation={...o?.operation},g(o))this.transactionMeta={epoch:new Map(o?.transactionMeta.epoch),actionContinuities:new _(o?.transactionMeta.actionContinuities.toJSON())};for(let[,t]of o.families){if(t.internalRoles?.includes("mutable")||t.internalRoles?.includes("join"))continue;t.install(this)}let n=new Set;for(let[,t]of o.atoms){if(n.has(t.key))continue;if(t.install(this),t.type==="mutable_atom"){let r=H(o,t),i=He(t);n.add(r.key),n.add(i.key)}}for(let[,t]of o.readonlySelectors)t.install(this);for(let[,t]of o.writableSelectors){if(n.has(t.key))continue;t.install(this)}for(let[,t]of o.transactions)t.install(this);for(let[,t]of o.timelines)t.install(this)}}}var Se={get STORE(){return globalThis.ATOM_IO_IMPLICIT_STORE??=new Ie({name:"IMPLICIT_STORE",lifespan:"ephemeral"}),globalThis.ATOM_IO_IMPLICIT_STORE}};function K(e,o){let n,t=e;while(t!==null){switch(o.type){case"atom":case"mutable_atom":n=t.atoms.get(o.key);break;case"writable_pure_selector":case"writable_held_selector":n=t.writableSelectors.get(o.key);break;case"readonly_pure_selector":case"readonly_held_selector":n=t.readonlySelectors.get(o.key);break;case"atom_family":case"mutable_atom_family":case"writable_pure_selector_family":case"readonly_pure_selector_family":case"writable_held_selector_family":case"readonly_held_selector_family":n=t.families.get(o.key);break;case"timeline":n=t.timelines.get(o.key);break;case"transaction":n=t.transactions.get(o.key);break}if(n)return n;t=t.child}throw new z(o,e)}var je=(e,o)=>{let n=a(e);if(n.operation.open)return n.operation.prev.get(o.key);return n.valueMap.get(o.key)};var me=(e,o)=>{let n=a(e);return We(o,e).map((r)=>{let i=n.atoms.get(r);if(i===void 0)throw new Error(`Atom "${r}", a dependency of selector "${o.key}", not found in store "${e.config.name}".`);return i.subject.subscribe(`${o.type}:${o.key}`,(l)=>{e.logger.info("\uD83D\uDCE2",o.type,o.key,"root",r,"went",l.oldValue,"->",l.newValue);let c=je(n,o),T=W(n,o);e.logger.info("\u2728",o.type,o.key,"went",c,"->",T),o.subject.next({newValue:T,oldValue:c})})})};function G(e,o,n,t){function r(s){if(e.operation.open){let p=e.on.operationClose.subscribe(`state subscription ${n}`,()=>{p(),t(s)})}else t(s)}let i=K(e,o);e.logger.info("\uD83D\uDC40",i.type,i.key,`Adding subscription "${n}"`);let l=i.type==="writable_pure_selector"||i.type==="readonly_pure_selector",c=null,T=r;if(l)c=me(e,i),T=(s)=>{if(c)c.length=0,c.push(...me(e,i));r(s)};let m=i.subject.subscribe(n,T);return()=>{if(e.logger.info("\uD83D\uDE48",i.type,i.key,`Removing subscription "${n}"`),m(),c)for(let s of c)s()}}var $e=(e,o,n,t)=>{let r=K(e,o);e.logger.info("\uD83D\uDC40","timeline",o.key,`Adding subscription "${n}"`);let i=r.subject.subscribe(n,t);return()=>{e.logger.info("\uD83D\uDE48","timeline",o.key,`Removing subscription "${n}" from timeline`),i()}};class se{initializeState(e,o){let n=`*${e.key}`;o.atoms.delete(n),o.valueMap.delete(n);let t=e.family?{key:`*${e.family.key}`,subKey:e.family.subKey}:void 0,r=ye(o,{key:n,default:null},t);if(o.parent?.valueMap.has(n)){let i=o.parent.valueMap.get(n);o.valueMap.set(n,i)}return r}unsubscribeFromInnerValue;unsubscribeFromState;observeCore(e,o,n){let t=`tracker:${n.config.name}:${F(n)?n.transactionMeta.update.key:"main"}:${e.key}`,r=B(n,e);this.unsubscribeFromInnerValue=r.subscribe(t,(i)=>{h(n,o,i)}),this.unsubscribeFromState=G(n,e,t,(i)=>{if(i.newValue!==i.oldValue)this.unsubscribeFromInnerValue(),this.unsubscribeFromInnerValue=i.newValue.subscribe(t,(l)=>{h(n,o,l)})})}updateCore(e,o,n){let t=`tracker:${n.config.name}:${F(n)?n.transactionMeta.update.key:"main"}:${e.key}`;G(n,o,t,({newValue:r,oldValue:i})=>{let l=n.timelineTopics.getRelatedKey(o.key);if(l){if(n.timelines.get(l)?.timeTraveling){let m=$e(n,{key:l,type:"timeline"},t,(y)=>{m(),h(n,e,(s)=>{if(y==="redo"&&r)s.do(r);else if(y==="undo"&&i)s.undo(i);return s})});return}}let c=n.on.operationClose.subscribe(t,()=>{c();let T=B(n,e),m=r===null?-1:T.getUpdateNumber(r),y=m-T.cacheUpdateNumber;if(r&&y===1)h(n,e,(s)=>(s.do(r),s));else n.logger.info("\u274C","mutable_atom",e.key,`could not be updated. Expected update number ${T.cacheUpdateNumber+1}, but got ${m}`)})})}mutableState;latestUpdateState;[Symbol.dispose];constructor(e,o){this.mutableState=e;let n=a(o);this.latestUpdateState=this.initializeState(e,n),this.observeCore(e,this.latestUpdateState,n),this.updateCore(e,this.latestUpdateState,n),n.trackers.set(e.key,this),this[Symbol.dispose]=()=>{this.unsubscribeFromInnerValue(),this.unsubscribeFromState(),n.trackers.delete(e.key)}}}var H=(e,o)=>{if(o.family){let t=a(e),i={key:`${o.family.key}:JSON`,type:"writable_pure_selector_family"},l=K(t,i),c=JSON.parse(o.family.subKey);return w(e,l,c)}return{type:"writable_pure_selector",key:`${o.key}:JSON`}};var He=(e)=>{let n={type:"atom",key:`*${e.key}`};if(e.family)n.family={key:`*${e.family.key}`,subKey:e.family.subKey};return n};function _e(e){return typeof e==="object"&&e!==null&&"do"in e&&"undo"in e&&"subscribe"in e}function D(e,o,n){let t=n.valueMap.get(o.key),r=e.valueMap.get(o.key);if(t!==r)return r;if(t===void 0)return o.default();n.logger.info("\uD83D\uDCC3","atom",o.key,"copying");let i=o.toJson(t),l=o.fromJson(i);return e.valueMap.set(o.key,l),new se(o,n),l}function d(e,o,n,t){let r=e.valueMap.get(o);if(r instanceof j)r.use(n);if(n instanceof Promise){let i=new j(n);return e.valueMap.set(o,i),i.then((l)=>{d(e,o,l,t),t.next({newValue:l,oldValue:i})}).catch((l)=>{e.logger.error("\uD83D\uDCA5","state",o,"rejected:",l)}),i}return e.valueMap.set(o,n),n}var de=(e,o)=>{let n=o.valueMap.get(e.key);if(e.type==="mutable_atom"&&F(o)){let{parent:t}=o;n=D(o,e,t)}return n},we=(e,o)=>{let n=o.valueMap.get(e);if(n instanceof j){let t=n,r=o.writableSelectors.get(e)??o.readonlySelectors.get(e);if(r)t.use(r.get());return}if(o.operation.open)o.operation.prev.set(e,n);o.valueMap.delete(e),o.logger.info("\uD83D\uDDD1","state",e,"evicted")};var Re=(e,o)=>{return a(e).atomsThatAreDefault.has(o)},Ee=(e,o)=>{let n=a(e);n.atomsThatAreDefault=new Set(n.atomsThatAreDefault).add(o)},Me=(e,o)=>{let n=a(e);n.atomsThatAreDefault=new Set(a(e).atomsThatAreDefault),n.atomsThatAreDefault.delete(o)};function ye(e,o,n){let{key:r,default:i}=o;e.logger.info("\uD83D\uDD28","atom",r,`creating in store "${e.config.name}"`);let l=a(e),c=l.atoms.get(r);if(c&&c.type==="atom")return e.logger.error("\u274C","atom",r,"Tried to create atom, but it already exists in the store."),E(c);let T=new f,m={...o,type:"atom",install:(p)=>{return p.logger.info("\uD83D\uDEE0\uFE0F","atom",r,`installing in store "${p.config.name}"`),ye(p,o,n)},subject:T};if(n)m.family=n;let y=i;if(i instanceof Function)y=i();l.atoms.set(r,m),Ee(e,r),d(l,r,y,T);let s=E(m);if(o.effects){let p=0,b=[];for(let k of o.effects){let S=k({setSelf:(J)=>{h(e,s,J)},onSet:(J)=>G(e,s,`effect[${p}]`,J)});if(S)b.push(S);++p}m.cleanup=()=>{for(let k of b)k()}}return s}class O extends Set{mode="record";subject=new f;cacheLimit=0;cache=[];cacheIdx=-1;cacheUpdateNumber=-1;constructor(e,o=0){super(e);if(e instanceof O)this.parent=e,this.cacheUpdateNumber=e.cacheUpdateNumber;if(o)this.cacheLimit=o,this.cache=new Array(o),this.subscribe("auto cache",(n)=>{this.cacheIdx++,this.cacheIdx%=this.cacheLimit,this.cache[this.cacheIdx]=n})}toJSON(){return{members:[...this],cache:this.cache,cacheLimit:this.cacheLimit,cacheIdx:this.cacheIdx,cacheUpdateNumber:this.cacheUpdateNumber}}static fromJSON(e){let o=new O(e.members,e.cacheLimit);return o.cache=e.cache,o.cacheIdx=e.cacheIdx,o.cacheUpdateNumber=e.cacheUpdateNumber,o}add(e){let o=super.add(e);if(this.mode==="record")this.cacheUpdateNumber++,this.emit(`add:${u(e)}`);return o}clear(){let e=this.mode==="record"?[...this]:null;if(super.clear(),e)this.cacheUpdateNumber++,this.emit(`clear:${JSON.stringify(e)}`)}delete(e){let o=super.delete(e);if(this.mode==="record")this.cacheUpdateNumber++,this.emit(`del:${u(e)}`);return o}parent=null;child=null;transactionUpdates=null;transaction(e){this.mode="transaction",this.transactionUpdates=[],this.child=new O(this);let o=this.child._subscribe("transaction",(n)=>{this.transactionUpdates?.push(n)});try{if(e(this.child)){for(let t of this.transactionUpdates)this.doStep(t);this.cacheUpdateNumber++,this.emit(`tx:${this.transactionUpdates.join(";")}`)}}catch(n){throw console.warn("Did not apply transaction to SetRTX; this error was thrown:",n),n}finally{o(),this.child=null,this.transactionUpdates=null,this.mode="record"}}_subscribe(e,o){return this.subject.subscribe(e,o)}subscribe(e,o){return this.subject.subscribe(e,(n)=>{o(`${this.cacheUpdateNumber}=${n}`)})}emit(e){this.subject.next(e)}doStep(e){let o=e.indexOf(":"),n=e.substring(0,o),t=e.substring(o+1);switch(n){case"add":this.add(JSON.parse(t));break;case"clear":this.clear();break;case"del":this.delete(JSON.parse(t));break;case"tx":for(let r of t.split(";"))this.doStep(r)}}getUpdateNumber(e){let o=e.indexOf("=");return Number(e.substring(0,o))}do(e){let o=e.indexOf("="),n=Number(e.substring(0,o)),t=n-this.cacheUpdateNumber;if(t>0){if(t===1){this.mode="playback";let i=e.substring(o+1);return this.doStep(i),this.mode="record",this.cacheUpdateNumber=n,null}return this.cacheUpdateNumber+1}if(Math.abs(t)<this.cacheLimit){let i=this.cacheIdx+t;if(this.cache[i]===e)return null;this.mode="playback";let c=!1;while(!c){this.cacheIdx%=this.cacheLimit;let m=this.cache[this.cacheIdx];if(this.cacheIdx--,!m)return"OUT_OF_RANGE";this.undo(m),c=this.cacheIdx===i-1}let T=e.substring(o+1);return this.doStep(T),this.mode="record",this.cacheUpdateNumber=n,null}return"OUT_OF_RANGE"}undoStep(e){let o=e.indexOf(":"),n=e.substring(0,o),t=e.substring(o+1);switch(n){case"add":this.delete(JSON.parse(t));break;case"del":this.add(JSON.parse(t));break;case"clear":{let r=JSON.parse(t);for(let i of r)this.add(i);break}case"tx":{let r=t.split(";");for(let i=r.length-1;i>=0;i--)this.undoStep(r[i])}}}undo(e){let o=e.indexOf("=");if(Number(e.substring(0,o))===this.cacheUpdateNumber){this.mode="playback";let t=e.substring(o+1);return this.undoStep(t),this.mode="record",this.cacheUpdateNumber--,null}return this.cacheUpdateNumber}}function Oe(e){return e.startsWith("\uD83D\uDD0D ")}class U{listeners;globalListeners;handleEvent(e,...o){for(let t of this.globalListeners)t(e,...o);let n=this.listeners.get(e);if(n)for(let t of n)t(...o)}id="no_id_retrieved";emit;constructor(e){this.emit=e,this.listeners=new Map,this.globalListeners=new Set}on(e,o){let n=this.listeners.get(e);if(n)n.add(o);else this.listeners.set(e,new Set([o]));return this}onAny(e){return this.globalListeners.add(e),this}off(e,o){let n=this.listeners.get(e);if(n)if(o)n.delete(o);else this.listeners.delete(e);return this}offAny(e){return this.globalListeners.delete(e),this}}class ze extends U{in;out;id="no_id_retrieved";disposalFunctions=[];constructor(e){super((...o)=>{return this.out.next(o),this});this.id=e,this.in=new f,this.out=new f,this.in.subscribe("socket",(o)=>{this.handleEvent(...o)})}dispose(){for(let e of this.disposalFunctions)e()}}class fe extends U{incompleteData="";unprocessedEvents=[];relays;relayServices;process;id="#####";log(...e){this.process.stderr.write(u(e.map((o)=>o instanceof O?`{ ${o.toJSON().members.join(" | ")} }`:o))+"\x03")}logger={info:(...e)=>{this.log("i",...e)},warn:(...e)=>{this.log("w",...e)},error:(...e)=>{this.log("e",...e)}};constructor(){super((e,...o)=>{let n=JSON.stringify([e,...o]);return this.process.stdout.write(n+"\x03"),this});if(this.process=process,this.process.stdin.resume(),this.relays=new Map,this.relayServices=[],this.process.stdin.on("data",(e)=>{let o=e.toString();this.unprocessedEvents.push(...o.split("\x03"));let n=this.unprocessedEvents.shift();this.incompleteData+=n??"";try{let t=$(this.incompleteData);this.logger.info("\uD83C\uDFB0","received",t),this.handleEvent(...t);while(this.unprocessedEvents.length>0){let r=this.unprocessedEvents.shift();if(r){if(this.unprocessedEvents.length===0)this.incompleteData=r;let i=$(r);this.handleEvent(...i)}}this.incompleteData=""}catch(t){if(t instanceof Error)this.logger.error("\u2757",t.message,t.cause,t.stack)}}),this.on("exit",()=>{this.logger.info("\uD83D\uDD25",this.id,'received "exit"'),process.exit(0)}),process.on("exit",(e)=>{this.logger.info("\uD83D\uDD25",this.id,`exited with code ${e}`)}),process.on("end",()=>{this.logger.info("\uD83D\uDD25",this.id,"ended"),process.exit(0)}),process.on("SIGTERM",()=>{this.logger.error("\uD83D\uDD25",this.id,"terminated"),process.exit(0)}),process.on("SIGINT",()=>{this.logger.error("\uD83D\uDD25",this.id,"interrupted"),process.exit(0)}),process.pid)this.id=process.pid?.toString();this.on("user-joins",(e)=>{this.logger.info("\uD83D\uDC64","user",e,"joined");let o=new ze(`user:${e}`);this.relays.set(e,o),this.logger.info("\uD83D\uDD17","attaching services:",`[${[...this.relayServices.keys()].join(", ")}]`);for(let n of this.relayServices){let t=n(o);if(t)o.disposalFunctions.push(t)}this.on(`user:${e}`,(...n)=>{o.in.next(n)}),o.out.subscribe("socket",(n)=>{this.emit(...n)})}),this.on("user-leaves",(e)=>{let o=this.relays.get(e);if(this.off(`relay:${e}`),o)o.dispose(),this.relays.delete(e)}),process.stdout.write("ALIVE")}relay(e){this.logger.info("\uD83D\uDD17","running relay method"),this.relayServices.push(e)}}var q=new fe;Object.assign(console,q.logger,{log:q.logger.info});q.on("timeToStop",function e(){q.logger.info("\uD83D\uDEEC game worker exiting"),process.exit(0)});q.logger.info("\uD83D\uDEEB game worker ready");
|
|
3
|
+
var FQ=Object.create;var{getPrototypeOf:hQ,defineProperty:j0,getOwnPropertyNames:IQ}=Object;var UQ=Object.prototype.hasOwnProperty;var VZ=(Q,Z,Y)=>{Y=Q!=null?FQ(hQ(Q)):{};let G=Z||!Q||!Q.__esModule?j0(Y,"default",{value:Q,enumerable:!0}):Y;for(let _ of IQ(Q))if(!UQ.call(G,_))j0(G,_,{get:()=>Q[_],enumerable:!0});return G};var $Z=(Q,Z)=>()=>(Z||Q((Z={exports:{}}).exports,Z),Z.exports);var FZ=(Q,Z)=>{for(var Y in Z)j0(Q,Y,{get:Z[Y],enumerable:!0,configurable:!0,set:(G)=>Z[Y]=()=>G})};var hZ=(Q,Z)=>()=>(Q&&(Z=Q(Q=0)),Z);var IZ=import.meta.require;var n0=(Q,Z,Y=w.STORE)=>{return B0(Y,{key:`${Q.key}:JSON`,get:({get:G})=>Z.toJson(G(Q)),set:({set:G},_)=>{G(Q,Z.fromJson(_))}})};function r0(Q,Z,Y){return L0(Q,{key:`${Z.key}:JSON`,get:(_)=>({get:X})=>{let W=X(Z,_);return Y.toJson(W)},set:(_)=>({set:X},W)=>{X(Z,_,Y.fromJson(W))}},["mutable","json"])}var K=(Q)=>JSON.parse(Q),h=(Q)=>JSON.stringify(Q),wZ=[Array.prototype,Boolean.prototype,Number.prototype,Object.prototype,String.prototype];function r(Q){return _Q(w.STORE,Q)}function _0(Q){return e0(w.STORE,Q)}function TZ(...Q){if(Q.length===2)return k(w.STORE,...Q);return k(w.STORE,...Q)}function M0(Q,Z,Y=w.STORE){return Y.joins.set(Q.key,new h0(Q,Z,Y)),{key:Q.key,type:"join",a:Q.between[0],b:Q.between[1],cardinality:Q.cardinality}}function a0(Q){return WQ(Q,w.STORE)}var V0=(Q)=>(Z,Y,G,_,...X)=>{console[Q](`${Z} ${Y} "${G}" ${_}`,...X)},AQ={error:V0("error"),info:V0("info"),warn:V0("warn")},s0=class{logLevel;filter;logger;constructor(Q,Z,Y=AQ){this.logLevel=Q,this.filter=Z,this.logger=Y}error=(...Q)=>{let Z=this.filter?.(...Q)??!0;if(this.logLevel!==null){if(Z===!0)this.logger.error(...Q);else if(Z!==!1)this.logger.error(...Z)}};info=(...Q)=>{let Z=this.filter?.(...Q)??!0;if(this.logLevel==="info"){if(Z===!0)this.logger.info(...Q);else if(Z!==!1)this.logger.info(...Z)}};warn=(...Q)=>{let Z=this.filter?.(...Q)??!0;if(this.logLevel!=="error"&&this.logLevel!==null){if(Z===!0)this.logger.warn(...Q);else if(Z!==!1)this.logger.warn(...Z)}}},vZ=Symbol.for("claim"),wQ=class{store;constructor(Q=w.STORE){this.store=Q,ZQ("root",Q)}allocate(Q,Z,Y){return s(this.store,Q,Z,Y)}fuse(Q,Z,Y){return YQ(this.store,Q,Z,Y)}deallocate(Q){o(this.store,Q)}claim(Q,Z,Y){return W0(this.store,Q,Z,Y)}},o0=class{store;realm;constructor(Q=w.STORE){this.store=Q,this.realm=new wQ(Q)}allocate(Q,Z,Y){s(this.store,Q,Z,Y)}deallocate(Q){o(this.store,Q)}claim(Q,Z,Y){W0(this.store,Q,Z,Y)}};function J0(Q){return QQ(w.STORE,Q)}function X0(Q){return F0(w.STORE,Q)}var f=class Q extends Set{mode="record";subject=new I;cacheLimit=0;cache=[];cacheIdx=-1;cacheUpdateNumber=-1;constructor(Z,Y=0){super(Z);if(Z instanceof Q)this.parent=Z,this.cacheUpdateNumber=Z.cacheUpdateNumber;if(Y)this.cacheLimit=Y,this.cache=new Array(Y),this.subscribe("auto cache",(G)=>{this.cacheIdx++,this.cacheIdx%=this.cacheLimit,this.cache[this.cacheIdx]=G})}toJSON(){return{members:[...this],cache:this.cache,cacheLimit:this.cacheLimit,cacheIdx:this.cacheIdx,cacheUpdateNumber:this.cacheUpdateNumber}}static fromJSON(Z){let Y=new Q(Z.members,Z.cacheLimit);return Y.cache=Z.cache,Y.cacheIdx=Z.cacheIdx,Y.cacheUpdateNumber=Z.cacheUpdateNumber,Y}add(Z){let Y=super.add(Z);if(this.mode==="record")this.cacheUpdateNumber++,this.emit(`add:${h(Z)}`);return Y}clear(){let Z=this.mode==="record"?[...this]:null;if(super.clear(),Z)this.cacheUpdateNumber++,this.emit(`clear:${JSON.stringify(Z)}`)}delete(Z){let Y=super.delete(Z);if(this.mode==="record")this.cacheUpdateNumber++,this.emit(`del:${h(Z)}`);return Y}parent=null;child=null;transactionUpdates=null;transaction(Z){this.mode="transaction",this.transactionUpdates=[],this.child=new Q(this);let Y=this.child._subscribe("transaction",(G)=>{this.transactionUpdates?.push(G)});try{if(Z(this.child)){for(let _ of this.transactionUpdates)this.doStep(_);this.cacheUpdateNumber++,this.emit(`tx:${this.transactionUpdates.join(";")}`)}}catch(G){throw console.warn("Did not apply transaction to SetRTX; this error was thrown:",G),G}finally{Y(),this.child=null,this.transactionUpdates=null,this.mode="record"}}_subscribe(Z,Y){return this.subject.subscribe(Z,Y)}subscribe(Z,Y){return this.subject.subscribe(Z,(G)=>{Y(`${this.cacheUpdateNumber}=${G}`)})}emit(Z){this.subject.next(Z)}doStep(Z){let Y=Z.indexOf(":"),G=Z.substring(0,Y),_=Z.substring(Y+1);switch(G){case"add":this.add(JSON.parse(_));break;case"clear":this.clear();break;case"del":this.delete(JSON.parse(_));break;case"tx":for(let X of _.split(";"))this.doStep(X)}}getUpdateNumber(Z){let Y=Z.indexOf("=");return Number(Z.substring(0,Y))}do(Z){let Y=Z.indexOf("="),G=Number(Z.substring(0,Y)),_=G-this.cacheUpdateNumber;if(_>0){if(_===1){this.mode="playback";let W=Z.substring(Y+1);return this.doStep(W),this.mode="record",this.cacheUpdateNumber=G,null}return this.cacheUpdateNumber+1}if(Math.abs(_)<this.cacheLimit){let W=this.cacheIdx+_;if(this.cache[W]===Z)return null;this.mode="playback";let H=!1;while(!H){this.cacheIdx%=this.cacheLimit;let z=this.cache[this.cacheIdx];if(this.cacheIdx--,!z)return"OUT_OF_RANGE";this.undo(z),H=this.cacheIdx===W-1}let N=Z.substring(Y+1);return this.doStep(N),this.mode="record",this.cacheUpdateNumber=G,null}return"OUT_OF_RANGE"}undoStep(Z){let Y=Z.indexOf(":"),G=Z.substring(0,Y),_=Z.substring(Y+1);switch(G){case"add":this.delete(JSON.parse(_));break;case"del":this.add(JSON.parse(_));break;case"clear":{let X=JSON.parse(_);for(let W of X)this.add(W);break}case"tx":{let X=_.split(";");for(let W=X.length-1;W>=0;W--)this.undoStep(X[W])}}}undo(Z){let Y=Z.indexOf("=");if(Number(Z.substring(0,Y))===this.cacheUpdateNumber){this.mode="playback";let _=Z.substring(Y+1);return this.undoStep(_),this.mode="record",this.cacheUpdateNumber--,null}return this.cacheUpdateNumber}};function t0(Q=Math.random){return Q().toString(36).slice(2)}var I0=class extends Promise{fate;resolve;reject;done=!1;constructor(Q){let Z,Y;super((G,_)=>{Z=G,Y=_});this.resolve=Z,this.reject=Y,this.use(Q instanceof Promise?Q:new Promise(Q))}pass(Q,Z){if(Q===this.fate)this.resolve(Z),this.done=!0}fail(Q,Z){if(Q===this.fate)this.reject(Z),this.done=!0}use(Q){if(Q instanceof Promise){let Z=Q;this.fate=Z,Z.then((Y)=>{this.pass(Z,Y)},(Y)=>{this.fail(Z,Y)})}else this.resolve(Q),this.fate=void 0}};function C(Q){while(Q.child!==null)Q=Q.child;return Q}var TQ=class Q{_buffer;_index=0;constructor(Z){let Y;if(typeof Z==="number")Y=Z;else Y=Z.length;this._buffer=Array.from({length:Y})}get buffer(){return this._buffer}get index(){return this._index}add(Z){this._buffer[this._index]=Z,this._index=(this._index+1)%this._buffer.length}copy(){let Z=new Q([...this._buffer]);return Z._index=this._index,Z}},vQ={atom_family:"atom",molecule_family:"molecule",mutable_atom_family:"mutable_atom",readonly_held_selector_family:"readonly_held_selector",readonly_pure_selector_family:"readonly_pure_selector",writable_held_selector_family:"writable_held_selector",writable_pure_selector_family:"writable_pure_selector"};function SQ(Q,Z){let Y=h(Z),G=`${Q.key}(${Y})`,_=vQ[Q.type],X={key:G,type:_};return Object.assign(X,{family:{key:Q.key,subKey:Y}}),Object.assign(X,{counterfeit:!0}),X}function e(Q){let Z={key:Q.key,type:Q.type};if("family"in Q)Z.family=Q.family;return Z}var v=class{a;b;cardinality;relations=new Map;contents=new Map;isAType;isBType;isContent;makeContentKey=(Q,Z)=>`${Q}:${Z}`;warn;getRelatedKeys(Q){return this.relations.get(Q)}addRelation(Q,Z){let Y=this.relations.get(Q),G=this.relations.get(Z);if(Y)Y.add(Z);else Y=new Set([Z]),this.relations.set(Q,Y);if(G)G.add(Q);else G=new Set([Q]),this.relations.set(Z,G)}deleteRelation(Q,Z){let Y=this.relations.get(Q);if(Y){if(Y.delete(Z),Y.size===0)this.relations.delete(Q);let G=this.relations.get(Z);if(G){if(G.delete(Q),G.size===0)this.relations.delete(Z)}}}replaceRelationsUnsafely(Q,Z){this.relations.set(Q,new Set(Z));for(let Y of Z){let G=new Set().add(Q);this.relations.set(Y,G)}}replaceRelationsSafely(Q,Z){let Y=this.relations.get(Q),G=this.isAType?.(Q)?Q:void 0,_=G===void 0?Q:void 0;if(Y)for(let X of Y){G??=X,_??=X;let W=this.relations.get(X);if(W){if(W.size===1)this.relations.delete(X);else W.delete(Q);this.contents.delete(this.makeContentKey(G,_))}}this.relations.set(Q,new Set(Z));for(let X of Z){let W=this.relations.get(X);if(W)W.add(Q);else W=new Set().add(Q),this.relations.set(X,W)}}getContentInternal(Q){return this.contents.get(Q)}setContent(Q,Z){this.contents.set(Q,Z)}deleteContent(Q){this.contents.delete(Q)}constructor(Q,Z){if(this.a=Q.between[0],this.b=Q.between[1],this.cardinality=Q.cardinality,!Z?.externalStore)this.relations=new Map(Q.relations?.map(([Y,G])=>[Y,new Set(G)])),this.contents=new Map(Q.contents);if(this.isAType=Z?.isAType??null,this.isBType=Z?.isBType??null,this.isContent=Z?.isContent??null,Z?.makeContentKey)this.makeContentKey=Z.makeContentKey;if(Z?.externalStore){let Y=Z.externalStore;if(this.has=(G,_)=>Y.has(G,_),this.addRelation=(G,_)=>{Y.addRelation(G,_)},this.deleteRelation=(G,_)=>{Y.deleteRelation(G,_)},this.replaceRelationsSafely=(G,_)=>{Y.replaceRelationsSafely(G,_)},this.replaceRelationsUnsafely=(G,_)=>{Y.replaceRelationsUnsafely(G,_)},this.getRelatedKeys=(G)=>Y.getRelatedKeys(G),Y.getContent)this.getContentInternal=(G)=>{return Y.getContent(G)},this.setContent=(G,_)=>{Y.setContent(G,_)},this.deleteContent=(G)=>{Y.deleteContent(G)};for(let[G,_]of Q.relations??[]){let X=this.isAType?.(G)?G:void 0,W=X===void 0?G:void 0;for(let q of _)X??=q,W??=q,this.addRelation(X,W)}for(let[G,_]of Q.contents??[])this.setContent(G,_)}if(Z?.warn)this.warn=Z.warn}toJSON(){return{between:[this.a,this.b],cardinality:this.cardinality,relations:[...this.relations.entries()].map(([Q,Z])=>[Q,[...Z]]),contents:[...this.contents.entries()]}}set(...Q){let Z,Y,G;switch(Q.length){case 1:{let _=Q[0];Z=_[this.a],Y=_[this.b],G=void 0;break}case 2:{let _=Q[0];if(typeof _==="string")[Z,Y]=Q;else Z=_[this.a],Y=_[this.b],G=Q[1];break}default:{Z=Q[0],Y=Q[1],G=Q[2];break}}switch(this.cardinality){case"1:1":{let _=this.getRelatedKey(Z);if(_&&_!==Y)this.delete(Z,_)}case"1:n":{let _=this.getRelatedKey(Y);if(_&&_!==Z)this.delete(_,Y)}break;case"n:n":}if(G){let _=this.makeContentKey(Z,Y);this.setContent(_,G)}return this.addRelation(Z,Y),this}delete(Q,Z){Z=typeof Z==="string"?Z:Q[this.b];let Y=typeof Q==="string"?Q:Q[this.a];if(Y===void 0&&typeof Z==="string"){let G=this.getRelatedKeys(Z);if(G)for(let _ of G)this.delete(_,Z)}if(typeof Y==="string"&&Z===void 0){let G=this.getRelatedKeys(Y);if(G)for(let _ of G)this.delete(Y,_)}if(typeof Y==="string"&&typeof Z==="string"){this.deleteRelation(Y,Z);let G=this.makeContentKey(Y,Z);this.deleteContent(G)}return this}getRelatedKey(Q){let Z=this.getRelatedKeys(Q);if(Z){if(Z.size>1)this.warn?.(`${Z.size} related keys were found for key "${Q}": (${[...Z].map((G)=>`"${G}"`).join(", ")}). Only one related key was expected.`);let Y;for(let G of Z){Y=G;break}return Y}}replaceRelations(Q,Z,Y){let G=!Array.isArray(Z),_=G?Object.keys(Z):Z;if(Y?.reckless)this.replaceRelationsUnsafely(Q,_);else this.replaceRelationsSafely(Q,_);if(G)for(let X of _){let W=this.makeContentKey(Q,X),q=Z[X];this.setContent(W,q)}return this}getContent(Q,Z){let Y=this.makeContentKey(Q,Z);return this.getContentInternal(Y)}getRelationEntries(Q){let Z=Q[this.a],Y=Q[this.b];if(Z!==void 0&&Y===void 0){let G=this.getRelatedKeys(Z);if(G)return[...G].map((_)=>{return[_,this.getContent(Z,_)]})}if(Z===void 0&&Y!==void 0){let G=this.getRelatedKeys(Y);if(G)return[...G].map((_)=>{return[_,this.getContent(_,Y)]})}return[]}has(Q,Z){if(Z)return this.getRelatedKeys(Q)?.has(Z)??!1;return this.relations.has(Q)}},I=class{Subscriber;subscribers=new Map;subscribe(Q,Z){return this.subscribers.set(Q,Z),()=>{this.unsubscribe(Q)}}unsubscribe(Q){this.subscribers.delete(Q)}next(Q){let Z=this.subscribers.values();for(let Y of Z)Y(Q)}},KQ=class extends I{state;constructor(Q){super();this.state=Q}next(Q){this.state=Q,super.next(Q)}};function y(Q){return"epoch"in Q.transactionMeta}function R(Q){return"phase"in Q.transactionMeta}var fQ=(Q)=>{let Z=C(Q);if(!R(Z)){Q.logger.warn("\uD83D\uDC1E","transaction","???","abortTransaction called outside of a transaction. This is probably a bug in AtomIO.");return}Q.logger.info("\uD83E\uDE82","transaction",Z.transactionMeta.update.key,"Aborting transaction"),Z.parent.child=null};function U(Q){return Q[0].toUpperCase()+Q.slice(1)}function i(Q){return Q.type.split("_").map(U).join(" ")}var T0=class extends Error{constructor(Q,Z){super(`${i(Q)} ${h(Q.key)} not found in store "${Z.config.name}".`)}};function $0(Q,Z,Y){return(...G)=>{let _=A(Q,Z);if(_)return _.run(G,Y);throw new T0(Z,Q)}}var v0=(Q)=>(Z)=>Q instanceof Function?Q(Z):Q,E0=(Q,Z)=>{if(Q.valueMap.has(Z.key))return Q.logger.info("\uD83D\uDCD6",Z.type,Z.key,"reading cached value"),DZ(Z,Q);switch(Z.type){case"readonly_held_selector":case"readonly_pure_selector":case"writable_held_selector":case"writable_pure_selector":return Q.logger.info("\uD83E\uDDEE",Z.type,Z.key,"computing value"),Z.get();case"atom":case"mutable_atom":{let Y=Z.default,G;if(Y instanceof Function)G=Y();else G=Y;return Q.logger.info("\uD83D\uDC81","atom",Z.key,"could not find cached value; using default",G),G}}},U0=(Q,Z)=>{if(Q.operation.open){let Y=performance.now();return Q.logger.info("\u2757",Z.type,Z.key,`deferring setState at T-${Y} until setState for "${Q.operation.token.key}" is done`),Y}Q.operation={open:!0,done:new Set,prev:new Map,time:Date.now(),token:Z},Q.logger.info("\u2B55",Z.type,Z.key,`operation start in store "${Q.config.name}"${!R(Q)?"":` ${Q.transactionMeta.phase} "${Q.transactionMeta.update.key}"`}`)},A0=(Q)=>{if(Q.operation.open)Q.logger.info("\uD83D\uDD34",Q.operation.token.type,Q.operation.token.key,`operation done in store "${Q.config.name}"`);Q.operation={open:!1},Q.on.operationClose.next(Q.operation)},NQ=(Q,Z)=>{if(!Q.operation.open)return Q.logger.error("\uD83D\uDC1E","unknown",Z,"isDone called outside of an operation. This is probably a bug in AtomIO."),!0;return Q.operation.done.has(Z)},N0=(Q,Z)=>{if(!Q.operation.open){Q.logger.error("\uD83D\uDC1E","unknown",Z,"markDone called outside of an operation. This is probably a bug in AtomIO.");return}Q.operation.done.add(Z)},bQ=(Q,Z,Y)=>{switch(Z.type){case"mutable_atom":Q.logger.info("\uD83D\uDCE2",Z.type,Z.key,"is now (",Y.newValue,") subscribers:",Z.subject.subscribers);break;case"atom":case"writable_pure_selector":case"readonly_pure_selector":case"writable_held_selector":case"readonly_held_selector":Q.logger.info("\uD83D\uDCE2",Z.type,Z.key,"went (",Y.oldValue,"->",Y.newValue,") subscribers:",Z.subject.subscribers)}Z.subject.next(Y)};function w0(Q,Z){let Y=C(Q),G=Y.selectorAtoms.getRelatedKeys(Z.key);if(Y.logger.info("\uD83E\uDDF9",Z.type,Z.key,G?`evicting ${G.size} states downstream:`:"no downstream states",G??"to evict"),G){if(Y.operation.open)Y.logger.info("\uD83E\uDDF9",Z.type,Z.key,`[ ${[...Y.operation.done].join(", ")} ] already done`);for(let _ of G){if(NQ(Y,_))continue;PQ(_,Y),N0(Y,_)}}}function gQ(Q,Z){let Y=C(Q),G=Y.selectorGraph.getRelationEntries({upstreamSelectorKey:Z.key}).filter(([_,{source:X}])=>X===Z.key);for(let[_]of G){if(NQ(Y,_))continue;PQ(_,Y),N0(Y,_)}}var dQ=(Q,Z,Y)=>{let G=E0(Q,Z),_=G;if(Z.type==="mutable_atom"&&R(Q)){let{parent:W}=Q;_=R0(Q,Z,W)}if(_=v0(Y)(_),Q.logger.info("\uD83D\uDCDD","atom",Z.key,"set to",_),_=u(Q,Z.key,_,Z.subject),LZ(Q,Z.key))MZ(Q,Z.key);N0(Q,Z.key),w0(Q,Z);let X={oldValue:G,newValue:_};if(!R(Q)){bQ(Q,Z,X);return}if(Q.on.transactionApplying.state===null){let{key:W}=Z;if(zZ(X.newValue))return;let q={type:"atom_update",key:W,...X};if(Z.family)q.family=Z.family;Q.transactionMeta.update.updates.push(q),Q.logger.info("\uD83D\uDCC1","atom",W,"stowed (",X.oldValue,"->",X.newValue,")")}else if(Z.key.startsWith("*")){let W=Z.key.slice(1),q=Q.atoms.get(W),H=Q.valueMap.get(W);if(q.type==="mutable_atom"&&R(Q)){let{parent:z}=Q;H=R0(Q,q,z)}if(H.do(X.newValue)===null)w0(Q,q)}},zQ=(Q,Z,Y)=>{switch(Z.type){case"atom":case"mutable_atom":dQ(Q,Z,Y);break;case"writable_pure_selector":case"writable_held_selector":Z.set(Y);break}};function C0(Q,Z,Y){let G={key:Z.key,type:"atom_family"},_=Q.families.get(Z.key);if(_)Q.logger.error("\u2757","atom_family",Z.key,`Overwriting an existing ${i(_)} "${_.key}" in store "${Q.config.name}". You can safely ignore this warning if it is due to hot module replacement.`);let X=new I,q=Object.assign((H)=>{let N=h(H),z={key:Z.key,subKey:N},L=`${Z.key}(${N})`,M=C(Q),P=Z.default,E={key:L,default:P instanceof Function?P(H):P};if(Z.effects)E.effects=Z.effects(H);let j=O0(M,E,z);return X.next({type:"state_creation",token:j}),j},G,{subject:X,install:(H)=>C0(H,Z),internalRoles:Y});return Q.families.set(Z.key,q),Q.defaults.set(Z.key,Z.default),G}function e0(Q,Z){if("mutable"in Z)return k0(Q,Z);return C0(Q,Z)}var S0=(Q,Z)=>C(Q).atoms.has(Z),mQ=(Q,Z)=>C(Q).writableSelectors.has(Z),kQ=(Q,Z)=>C(Q).readonlySelectors.has(Z),cQ=(Q,Z)=>S0(Q,Z)||mQ(Q,Z)||kQ(Q,Z),x0=(Q,Z)=>{return C(Q).selectorGraph.getRelationEntries({downstreamSelectorKey:Z}).filter(([G,{source:_}])=>_!==Z).map(([G,{source:_}])=>_).filter((G)=>cQ(Q,G))},DQ=(Q,Z,Y)=>{let G=[],_=x0(Q,Z);while(_.length>0){let X=_.shift();if(Y.has(X))continue;if(Y.add(X),!S0(Q,X))_.push(...x0(Q,X));else if(!G.includes(X))G.push(X)}return G},uQ=(Q,Z)=>{let Y=Q.key,G=x0(Z,Y),_=new Set;return G.flatMap((X)=>S0(Z,X)?X:DQ(Z,X,_))},pQ=(Q,Z,Y,G,_)=>{let X=C(Q),{type:W,key:q}=G;if(W==="atom"||W==="mutable_atom")X.selectorAtoms.set({selectorKey:Y,atomKey:q}),Q.logger.info("\uD83D\uDD0D",Z,Y,`discovers root atom "${q}"`);else{let H=DQ(Q,q,_);Q.logger.info("\uD83D\uDD0D",Z,Y,`discovers root atoms: [ ${H.map((N)=>`"${N}"`).join(", ")} ]`);for(let N of H)X.selectorAtoms=X.selectorAtoms.set({selectorKey:Y,atomKey:N})}_.add(q)},P0=(Q,Z,Y,G)=>({get:(..._)=>{let X=C(Q),W;if(_.length===2){let[z,L]=_;W=$(Q,z,L)}else[W]=_;let q=A(Q,W),H=E0(Q,q),N=W.key;return Q.logger.info("\uD83D\uDD0C",Z,Y,`registers dependency ( "${N}" =`,H,")"),X.selectorGraph.set({upstreamSelectorKey:N,downstreamSelectorKey:Y},{source:W.key}),pQ(Q,Z,Y,W,G),H},set:(..._)=>{let X,W;if(_.length===2)X=_[0],W=_[1];else{let N=_[0],z=_[1];W=_[2],X=$(Q,N,z)}let q=C(Q),H=A(q,X);zQ(q,H,W)},find:(..._)=>$(Q,..._),json:(_)=>n(Q,_)}),K0=(Q,Z,Y)=>{let G=C(Q),_=new I,X=new Set,{key:W,const:q}=Z,H="readonly_held_selector",{get:N,find:z,json:L}=P0(G,"readonly_held_selector",W,X),P={...Z,type:"readonly_held_selector",subject:_,install:(j)=>K0(j,Z,Y),get:()=>{return Z.get({get:N,find:z,json:L},q),u(C(Q),W,q,_),X.clear(),q},...Y&&{family:Y}};G.readonlySelectors.set(W,P),Q.logger.info("\u2728","readonly_held_selector",W,"=",q);let E={key:W,type:"readonly_held_selector"};if(Y)E.family=Y;return E},f0=(Q,Z,Y)=>{let G=C(Q),_=new I,X=new Set,W=Z.key,q="readonly_pure_selector",{get:H,find:N,json:z}=P0(G,"readonly_pure_selector",W,X),L=()=>{let j=Z.get({get:H,find:N,json:z});return u(C(Q),W,j,_),X.clear(),j},M={...Z,type:"readonly_pure_selector",subject:_,install:(j)=>f0(j,Z,Y),get:L,...Y&&{family:Y}};G.readonlySelectors.set(W,M);let P=L();Q.logger.info("\u2728","readonly_pure_selector",W,"=",P);let E={key:W,type:"readonly_pure_selector"};if(Y)E.family=Y;return E},b0=(Q,Z,Y)=>{let G=C(Q),_=new I,X=new Set,{key:W,const:q}=Z,H="writable_held_selector",N=P0(G,"writable_held_selector",W,X),{find:z,get:L,json:M}=N,P={find:z,get:L,json:M},E=(T=Z.get,D=C(Q))=>{return T(P,q),u(D,W,q,_),X.clear(),q},S={...Z,type:"writable_held_selector",subject:_,install:(T)=>b0(T,Z,Y),get:E,set:(T)=>{let D=C(Q),J=E(Z.get,D),B=v0(T)(J);if(Q.logger.info("\uD83D\uDCDD","writable_held_selector",W,"set (",J,"->",B,")"),u(D,W,B,_),N0(D,W),y(D))_.next({newValue:B,oldValue:J});Z.set(N,B)},...Y&&{family:Y}};G.writableSelectors.set(W,S);let l=E();Q.logger.info("\u2728","writable_held_selector",W,"=",l);let d={key:W,type:"writable_held_selector"};if(Y)d.family=Y;return d},g0=(Q,Z,Y)=>{let G=C(Q),_=new I,X=new Set,W=Z.key,q="writable_pure_selector",H=P0(G,"writable_pure_selector",W,X),{find:N,get:z,json:L}=H,M={find:N,get:z,json:L},P=(d=Z.get,T=C(Q))=>{let D=d(M);return u(T,W,D,_),X.clear(),D},j={...Z,type:"writable_pure_selector",subject:_,install:(d)=>g0(d,Z,Y),get:P,set:(d)=>{let T=C(Q),D=P(Z.get,T),J=v0(d)(D);if(Q.logger.info("\uD83D\uDCDD","writable_pure_selector",W,"set (",D,"->",J,")"),u(T,Z.key,J,_),N0(T,Z.key),y(T))_.next({newValue:J,oldValue:D});Z.set(H,J)},...Y&&{family:Y}};G.writableSelectors.set(W,j);let S=P();Q.logger.info("\u2728",j.type,j.key,"=",S);let l={key:W,type:"writable_pure_selector"};if(Y)l.family=Y;return l};function B0(Q,Z){let Y="set"in Z,G="const"in Z;if(G&&Y){let X=b0(Q,Z,void 0);return Q.on.selectorCreation.next(X),X}if(G){let X=K0(Q,Z,void 0);return Q.on.selectorCreation.next(X),X}if(Y){let X=g0(Q,Z,void 0);return Q.on.selectorCreation.next(X),X}let _=f0(Q,Z,void 0);return Q.on.selectorCreation.next(_),_}function yQ(Q,Z){let Y=C(Q),{key:G,type:_}=Z,X=A(Y,Z);if(!X.family)Q.logger.error("\u274C",_,G,"Standalone selectors cannot be disposed.");else{if(Y.molecules.get(X.family.subKey))Y.moleculeData.delete(X.family.subKey,X.family.key);let q;switch(Z.type){case"writable_held_selector":Y.writableSelectors.delete(G),q={key:X.family.key,type:"writable_held_selector_family"},A(Q,q).subject.next({type:"state_disposal",subType:"selector",token:Z});break;case"writable_pure_selector":Y.writableSelectors.delete(G),q={key:X.family.key,type:"writable_pure_selector_family"},A(Q,q).subject.next({type:"state_disposal",subType:"selector",token:Z});break;case"readonly_held_selector":Y.readonlySelectors.delete(G),q={key:X.family.key,type:"readonly_held_selector_family"},A(Q,q).subject.next({type:"state_disposal",subType:"selector",token:Z});break;case"readonly_pure_selector":Y.readonlySelectors.delete(G),q={key:X.family.key,type:"readonly_pure_selector_family"},A(Q,q).subject.next({type:"state_disposal",subType:"selector",token:Z});break}if(Y.valueMap.delete(G),Y.selectorAtoms.delete(G),Y.selectorGraph.delete(G),Q.logger.info("\uD83D\uDD25",Z.type,G,"deleted"),R(Y)&&Y.transactionMeta.phase==="building")Y.transactionMeta.update.updates.push({type:"state_disposal",subType:"selector",token:Z});else Q.on.selectorDisposal.next(Z)}}function t(Q,Z,Y){let G=Z.key,_="readonly_pure_selector_family",X={key:G,type:"readonly_pure_selector_family"},W=Q.families.get(G);if(W)Q.logger.error("\u2757","readonly_pure_selector_family",G,`Overwriting an existing ${i(W)} "${W.key}" in store "${Q.config.name}". You can safely ignore this warning if it is due to hot module replacement.`);let q=new I,N=Object.assign((z)=>{let L=h(z),M={key:G,subKey:L},P=`${G}(${L})`,E=C(Q),j=f0(E,{key:P,get:Z.get(z)},M);return q.next({type:"state_creation",token:j}),j},X,{internalRoles:Y,subject:q,install:(z)=>t(z,Z),default:(z)=>{return Z.get(z)({get:(...M)=>k(Q,...M),find:(...M)=>$(Q,...M),json:(M)=>n(Q,M)})}});return Q.families.set(G,N),X}function LQ(Q,Z,Y){let G=Z.key,_="readonly_held_selector_family",X={key:G,type:"readonly_held_selector_family"},W=Q.families.get(G);if(W)Q.logger.error("\u2757","readonly_held_selector_family",G,`Overwriting an existing ${i(W)} "${W.key}" in store "${Q.config.name}". You can safely ignore this warning if it is due to hot module replacement.`);let q=new I,N=Object.assign((z)=>{let L=h(z),M={key:G,subKey:L},P=`${G}(${L})`,E=C(Q),j=K0(E,{key:P,const:Z.const(z),get:Z.get(z)},M);return q.next({type:"state_creation",token:j}),j},X,{internalRoles:Y,subject:q,install:(z)=>LQ(z,Z),default:Z.const});return Q.families.set(G,N),X}function MQ(Q,Z,Y){let G=Z.key,_="writable_held_selector_family",X={key:G,type:"writable_held_selector_family"},W=Q.families.get(G);if(W)Q.logger.error("\u2757","writable_held_selector_family",G,`Overwriting an existing ${i(W)} "${W.key}" in store "${Q.config.name}". You can safely ignore this warning if it is due to hot module replacement.`);let q=new I,N=Object.assign((z)=>{let L=h(z),M={key:G,subKey:L},P=`${G}(${L})`,E=C(Q),j=b0(E,{key:P,const:Z.const(z),get:Z.get(z),set:Z.set(z)},M);return q.next({type:"state_creation",token:j}),j},X,{internalRoles:Y,subject:q,install:(z)=>MQ(z,Z),default:Z.const});return Q.families.set(G,N),X}function L0(Q,Z,Y){let G=Z.key,_="writable_pure_selector_family",X={key:G,type:"writable_pure_selector_family"},W=Q.families.get(G);if(W)Q.logger.error("\u2757","writable_pure_selector_family",G,`Overwriting an existing ${i(W)} "${W.key}" in store "${Q.config.name}". You can safely ignore this warning if it is due to hot module replacement.`);let q=new I,N=Object.assign((z)=>{let L=h(z),M={key:G,subKey:L},P=`${G}(${L})`,E=C(Q),j=g0(E,{key:P,get:Z.get(z),set:Z.set(z)},M);return q.next({type:"state_creation",token:j}),j},X,{internalRoles:Y,subject:q,install:(z)=>L0(z,Z),default:(z)=>{return Z.get(z)({get:(...M)=>k(Q,...M),find:(...M)=>$(Q,...M),json:(M)=>n(Q,M)})}});return Q.families.set(G,N),X}function QQ(Q,Z){let Y="set"in Z,G="const"in Z;if(G&&Y)return MQ(Q,Z,void 0);if(G)return LQ(Q,Z,void 0);if(Y)return L0(Q,Z);return t(Q,Z)}function lQ(Q,Z,Y){let G=Q.families.get(Z.key);if(G===void 0)throw new T0(Z,Q);let _=G(Y),X=C(Q);if(_.family){if(y(X))switch(_.type){case"atom":case"mutable_atom":Q.on.atomCreation.next(_);break;case"writable_pure_selector":case"readonly_pure_selector":case"writable_held_selector":case"readonly_held_selector":Q.on.selectorCreation.next(_);break}else if(R(X)&&X.on.transactionApplying.state===null)X.transactionMeta.update.updates.push({type:"state_creation",token:_})}return _}function iQ(Q,Z,Y){let G=h(Y),_=`${Z.key}(${G})`,X=C(Q),W;switch(Z.type){case"atom_family":case"mutable_atom_family":W=X.atoms.get(_);break;case"writable_held_selector_family":case"writable_pure_selector_family":W=X.writableSelectors.get(_);break;case"readonly_held_selector_family":case"readonly_pure_selector_family":W=X.readonlySelectors.get(_);break}if(W)return e(W);return W}function $(Q,Z,Y){let G=iQ(Q,Z,Y);if(G)return G;let _=h(Y),X=Q.molecules.get(_);if(!X&&Q.config.lifespan==="immortal"){let W=SQ(Z,Y);return Q.logger.error("\u274C",W.type,W.key,`was not found in store "${Q.config.name}"; returned a counterfeit token.`),W}if(G=lQ(Q,Z,Y),X)C(Q).moleculeData.set(_,Z.key);return G}function a(Q,...Z){let Y;if(Z.length===1)Y=Z[0];else{let G=Z[0],_=Z[1];Y=$(Q,G,_)}try{A(Q,Y)}catch(G){Q.logger.error("\u274C",Y.type,Y.key,`could not be disposed because it was not found in the store "${Q.config.name}".`);return}switch(Y.type){case"atom":case"mutable_atom":jQ(Q,Y);break;case"writable_pure_selector":case"readonly_pure_selector":case"writable_held_selector":case"readonly_held_selector":yQ(Q,Y);break}}function nQ(Q,Z){if(Z.family){let Y=Q.families.get(Z.family.key);if(Y)return Y}}function g(Q,...Z){let Y,G,_,X;if(Z.length===2){if(Y=Z[0],X=Z[1],G=nQ(Q,Y)??null,G)_=Y.family?K(Y.family.subKey):null,Y=$(Q,G,_)}else G=Z[0],_=Z[1],X=Z[2],Y=$(Q,G,_);if("counterfeit"in Y&&"family"in Y){let H=Y.family.subKey,N=Q.disposalTraces.buffer.find((z)=>z?.key===H);Q.logger.error("\u274C",Y.type,Y.key,`could not be set because it was not found in the store "${Q.config.name}".`,N?`This state was previously disposed:
|
|
4
|
+
${N.trace}`:"No previous disposal trace was found.");return}let W=U0(Q,Y);if(W){let H=Q.on.operationClose.subscribe(`waiting to set "${Y.key}" at T-${W}`,()=>{H(),Q.logger.info("\uD83D\uDFE2",Y.type,Y.key,`resuming deferred setState from T-${W}`),g(Q,Y,X)});return}let q=A(Q,Y);zQ(Q,q,X),A0(Q)}function rQ(Q,Z,Y){let{key:G,newValue:_,oldValue:X}=Z,W=Q==="newValue"?_:X,q={key:G,type:"atom"};if(Z.family)Object.assign(q,{family:Z.family});g(Y,q,W)}function aQ(Q){let{stack:Z}=Q;if(Z)return`
|
|
5
|
+
`+Z.split(`
|
|
6
|
+
`)?.slice(1)?.join(`
|
|
7
|
+
`);return""}function ZQ(Q,Z=w.STORE){let Y={key:Q,stringKey:h(Q),dependsOn:"any"};return Z.molecules.set(h(Q),Y),Q}function s(Q,Z,Y,G="any"){let _=Z,X=h(Y),W=[],q=C(Q);if(Array.isArray(_))for(let z of _){let L=h(z);if(q.molecules.get(L))Q.moleculeGraph.set(L,X,{source:L});else W.push(L)}else{let z=h(_);if(q.molecules.get(z))Q.moleculeGraph.set(z,X,{source:z});else W.push(z)}if(W.length===0)q.molecules.set(X,{key:Y,stringKey:X,dependsOn:G});let H={type:"molecule_creation",key:Y,provenance:_};if(R(q)&&q.transactionMeta.phase==="building")q.transactionMeta.update.updates.push(H);else q.on.moleculeCreation.next(H);for(let z of W){let L=Q.disposalTraces.buffer.find((M)=>M?.key===z);Q.logger.error("\u274C","molecule",Y,"allocation failed:",`Could not allocate to ${z} in store "${Q.config.name}".`,L?`
|
|
8
|
+
${z} was most recently disposed
|
|
9
|
+
${L.trace}`:`No previous disposal trace for ${z} was found.`)}return Y}function YQ(Q,Z,Y,G){let _=`T$--${Z}==${Y}++${G}`;return s(Q,[Y,G],_,"all"),_}function o(Q,Z){let Y=h(Z),G=Q.molecules.get(Y);if(!G){let P=Q.disposalTraces.buffer.find((E)=>E?.key===Y);Q.logger.error("\u274C","molecule",Z,"deallocation failed:",`Could not find allocation for ${Y} in store "${Q.config.name}".`,P?`
|
|
10
|
+
This state was most recently deallocated
|
|
11
|
+
${P.trace}`:`No previous disposal trace for ${Y} was found.`);return}let _=Q.moleculeJoins.getRelatedKeys(G.key);if(_)for(let P of _){let E=Q.joins.get(P);if(E)E.relations.delete(G.key),E.molecules.delete(G.stringKey)}Q.moleculeJoins.delete(G.stringKey);let X=[],W=[],q={type:"molecule_disposal",key:G.key,values:W,provenance:X},H=C(Q);H.molecules.delete(Y);let N=R(H)&&H.transactionMeta.phase==="building";if(N)H.transactionMeta.update.updates.push(q);let z=Q.moleculeGraph.getRelationEntries({downstreamMoleculeKey:G.stringKey});if(z)for(let[P,{source:E}]of z)if(E===G.stringKey){let j=K(P);o(Q,j)}else X.push(E);let L=H.moleculeData.getRelatedKeys(G.stringKey);if(L)for(let P of L){let E=H.families.get(P),j=$(Q,E,G.key);W.push([E.key,j]),a(Q,j)}if(H.moleculeGraph.delete(G.stringKey),H.moleculeJoins.delete(G.stringKey),H.moleculeData.delete(G.stringKey),!N)H.on.moleculeDisposal.next(q);H.molecules.delete(G.stringKey);let M=aQ(new Error);Q.disposalTraces.add({key:Y,trace:M})}function W0(Q,Z,Y,G){let _=h(Y),X=C(Q),W=X.molecules.get(_);if(!W){let M=Q.disposalTraces.buffer.find((P)=>P?.key===_);return Q.logger.error("\u274C","molecule",Y,"claim failed:",`Could not allocate to ${_} in store "${Q.config.name}".`,M?`
|
|
12
|
+
${_} was most recently disposed
|
|
13
|
+
${M.trace}`:`No previous disposal trace for ${_} was found.`),Y}let q=h(Z),H=X.molecules.get(q);if(!H){let M=Q.disposalTraces.buffer.find((P)=>P?.key===q);return Q.logger.error("\u274C","molecule",Y,"claim failed:",`Could not allocate to ${q} in store "${Q.config.name}".`,M?`
|
|
14
|
+
${q} was most recently disposed
|
|
15
|
+
${M.trace}`:`No previous disposal trace for ${q} was found.`),Y}let N=Q.moleculeGraph.getRelationEntries({downstreamMoleculeKey:W.stringKey}).filter(([,{source:M}])=>M!==_).map(([M])=>K(M));if(G)X.moleculeGraph.delete(_);X.moleculeGraph.set({upstreamMoleculeKey:H.stringKey,downstreamMoleculeKey:W.stringKey},{source:H.stringKey});let z={type:"molecule_transfer",key:W.key,from:N,to:[H.key]};if(R(X)&&X.transactionMeta.phase==="building")X.transactionMeta.update.updates.push(z);return Y}function sQ(Q,Z,Y){switch(Z){case"newValue":{JQ(Q,Y);break}case"oldValue":{a(Y,Q.token);break}}}function oQ(Q,Z,Y){switch(Z){case"newValue":{a(Y,Q.token);break}case"oldValue":{if(JQ(Q,Y),Q.subType==="atom")Y.valueMap.set(Q.token.key,Q.value);break}}}function JQ(Q,Z){let{family:Y}=Q.token;if(Y){let G=Z.families.get(Y.key);if(G)$(Z,G,K(Y.subKey))}}function tQ(Q,Z,Y){switch(Z){case"newValue":s(Y,Q.provenance,Q.key);break;case"oldValue":o(Y,Q.key);break}}function eQ(Q,Z,Y){switch(Z){case"newValue":o(Y,Q.key);break;case"oldValue":{let G=Q.provenance.map(K);s(Y,G,Q.key);for(let[_,X]of Q.values){let W=Y.families.get(_);if(W){$(Y,W,Q.key);let q=`${_}(${h(Q.key)})`;Y.valueMap.set(q,X)}}}break}}function QZ(Q,Z,Y){switch(Z){case"newValue":{let G=Q.to.length===1?Q.to[0]:Q.to;W0(Y,G,Q.key,"exclusive")}break;case"oldValue":{let G=Q.from.length===1?Q.from[0]:Q.from;W0(Y,G,Q.key,"exclusive")}break}}function EQ(Q,Z,Y){let G=Q==="newValue"?Z.updates:[...Z.updates].reverse();for(let _ of G)switch(_.type){case"atom_update":case"selector_update":rQ(Q,_,Y);break;case"state_creation":sQ(_,Q,Y);break;case"state_disposal":oQ(_,Q,Y);break;case"molecule_creation":tQ(_,Q,Y);break;case"molecule_disposal":eQ(_,Q,Y);break;case"molecule_transfer":QZ(_,Q,Y);break;case"transaction_update":EQ(Q,_,Y);break}}function CQ(Q,Z){return Q.transactionMeta.actionContinuities.getRelatedKey(Z)}function ZZ(Q,Z){return Q.transactionMeta.epoch.get(Z)}function YZ(Q,Z){if(!y(Q))return;let G=CQ(Q,Z);if(G===void 0)return;return ZZ(Q,G)}function GZ(Q,Z,Y){if(!y(Q))return;let _=CQ(Q,Z);if(_!==void 0)Q.transactionMeta.epoch.set(_,Y)}var _Z=(Q,Z)=>{let Y=C(Z),{parent:G}=Y;if(G===null||!R(Y)||Y.transactionMeta?.phase!=="building"){Z.logger.warn("\uD83D\uDC1E","transaction","???","applyTransaction called outside of a transaction. This is probably a bug in AtomIO.");return}Y.transactionMeta.phase="applying",Y.transactionMeta.update.output=Q,G.child=null,G.on.transactionApplying.next(Y.transactionMeta);let{updates:_}=Y.transactionMeta.update;if(Z.logger.info("\uD83D\uDEC4","transaction",Y.transactionMeta.update.key,`Applying transaction with ${_.length} updates:`,_),EQ("newValue",Y.transactionMeta.update,G),y(G))GZ(G,Y.transactionMeta.update.key,Y.transactionMeta.update.epoch),A(Z,{key:Y.transactionMeta.update.key,type:"transaction"})?.subject.next(Y.transactionMeta.update),Z.logger.info("\uD83D\uDEEC","transaction",Y.transactionMeta.update.key,"Finished applying transaction.");else if(R(G))G.transactionMeta.update.updates.push(Y.transactionMeta.update);G.on.transactionApplying.next(null)};function XZ(Q){return{store:Q}}function k(Q,...Z){let Y,G,_;if(Z.length===1)Y=Z[0];else G=Z[0],_=Z[1],Y=$(Q,G,_);if("counterfeit"in Y&&"family"in Y){G=Q.families.get(Y.family.key);let X=Y.family.subKey,W=Q.disposalTraces.buffer.find((q)=>q?.key===X);switch(Q.logger.error("\u274C",Y.type,Y.key,`could not be retrieved because it was not found in the store "${Q.config.name}".`,W?`This state was previously disposed:
|
|
16
|
+
${W.trace}`:"No previous disposal trace was found."),G.type){case"atom_family":case"mutable_atom_family":return Q.defaults.get(G.key);case"readonly_pure_selector_family":case"writable_pure_selector_family":case"readonly_held_selector_family":case"writable_held_selector_family":{if(Q.defaults.has(G.key))return Q.defaults.get(Y.family.key);let q=A(Q,G).default(X);return Q.defaults.set(G.key,q),q}}}return E0(Q,A(Q,Y))}var c=class extends Map{deleted=new Set;source;constructor(Q){super();this.source=Q}get(Q){if(super.has(Q))return super.get(Q);if(!this.deleted.has(Q)&&this.source.has(Q))return this.source.get(Q);return}set(Q,Z){return this.deleted.delete(Q),super.set(Q,Z)}hasOwn(Q){return super.has(Q)}has(Q){return!this.deleted.has(Q)&&(super.has(Q)||this.source.has(Q))}delete(Q){return this.deleted.add(Q),super.delete(Q)}},WZ=(Q,Z,Y,G)=>{let _=C(Q),X={parent:_,child:null,on:_.on,loggers:_.loggers,logger:_.logger,config:_.config,atoms:new c(_.atoms),atomsThatAreDefault:new Set(_.atomsThatAreDefault),families:new c(_.families),joins:new c(_.joins),operation:{open:!1},readonlySelectors:new c(_.readonlySelectors),timelines:new c(_.timelines),timelineTopics:new v(_.timelineTopics.toJSON()),trackers:new Map,transactions:new c(_.transactions),selectorAtoms:new v(_.selectorAtoms.toJSON()),selectorGraph:new v(_.selectorGraph.toJSON(),{makeContentKey:(...N)=>N.sort().join(":")}),writableSelectors:new c(_.writableSelectors),valueMap:new c(_.valueMap),defaults:_.defaults,disposalTraces:Q.disposalTraces.copy(),molecules:new c(_.molecules),moleculeGraph:new v(_.moleculeGraph.toJSON(),{makeContentKey:_.moleculeGraph.makeContentKey}),moleculeData:new v(_.moleculeData.toJSON(),{makeContentKey:_.moleculeData.makeContentKey}),moleculeJoins:new v(_.moleculeJoins.toJSON(),{makeContentKey:_.moleculeJoins.makeContentKey}),miscResources:new c(_.miscResources)},W=YZ(Q,Z),q={phase:"building",update:{type:"transaction_update",key:Z,id:G,epoch:W===void 0?NaN:W+1,updates:[],params:Y,output:void 0},toolkit:{get:(...N)=>k(H,...N),set:(...N)=>{g(H,...N)},run:(N,z=t0())=>$0(H,N,z),find:(...N)=>$(Q,...N),json:(N)=>n(H,N),dispose:(...N)=>{a(H,...N)},env:()=>XZ(H)}},H=Object.assign(X,{transactionMeta:q});return _.child=H,Q.logger.info("\uD83D\uDEEB","transaction",Z,"Building transaction with params:",Y),H};function F0(Q,Z){let Y={key:Z.key,type:"transaction",run:(X,W)=>{let q=WZ(Q,Z.key,X,W);try{let H=C(Q),{toolkit:N}=q.transactionMeta,z=Z.do(N,...X);return _Z(z,H),z}catch(H){throw fQ(G),Q.logger.warn("\uD83D\uDCA5","transaction",Z.key,"caught:",H),H}},install:(X)=>F0(X,Z),subject:new I},G=C(Q);G.transactions.set(Y.key,Y);let _=e(Y);return Q.on.transactionCreation.next(_),_}var GQ=class{parent=null;child=null;valueMap=new Map;defaults=new Map;atoms=new Map;writableSelectors=new Map;readonlySelectors=new Map;atomsThatAreDefault=new Set;selectorAtoms=new v({between:["selectorKey","atomKey"],cardinality:"n:n"});selectorGraph=new v({between:["upstreamSelectorKey","downstreamSelectorKey"],cardinality:"n:n"},{makeContentKey:(...Q)=>Q.sort().join(":")});trackers=new Map;families=new Map;joins=new Map;transactions=new Map;transactionMeta={epoch:new Map,actionContinuities:new v({between:["continuity","action"],cardinality:"1:n"})};timelines=new Map;timelineTopics=new v({between:["timelineKey","topicKey"],cardinality:"1:n"});disposalTraces=new TQ(100);molecules=new Map;moleculeJoins=new v({between:["moleculeKey","joinKey"],cardinality:"n:n"},{makeContentKey:(...Q)=>Q.sort().join(":")});moleculeGraph=new v({between:["upstreamMoleculeKey","downstreamMoleculeKey"],cardinality:"n:n"},{makeContentKey:(...Q)=>Q.sort().join(":")});moleculeData=new v({between:["moleculeKey","stateFamilyKey"],cardinality:"n:n"},{makeContentKey:(...Q)=>Q.sort().join(":")});miscResources=new Map;on={atomCreation:new I,atomDisposal:new I,selectorCreation:new I,selectorDisposal:new I,timelineCreation:new I,transactionCreation:new I,transactionApplying:new KQ(null),operationClose:new I,moleculeCreation:new I,moleculeDisposal:new I};operation={open:!1};config={name:"IMPLICIT_STORE",lifespan:"ephemeral"};loggers=[new s0("warn",(Q,Z,Y)=>!JZ(Y))];logger={error:(...Q)=>{for(let Z of this.loggers)Z.error(...Q)},info:(...Q)=>{for(let Z of this.loggers)Z.info(...Q)},warn:(...Q)=>{for(let Z of this.loggers)Z.warn(...Q)}};constructor(Q,Z=null){if(this.config={...Z?.config,...Q},Z!==null){if(this.valueMap=new Map(Z?.valueMap),this.operation={...Z?.operation},y(Z))this.transactionMeta={epoch:new Map(Z?.transactionMeta.epoch),actionContinuities:new v(Z?.transactionMeta.actionContinuities.toJSON())};for(let[,G]of Z.families){if(G.internalRoles?.includes("mutable")||G.internalRoles?.includes("join"))continue;G.install(this)}let Y=new Set;for(let[,G]of Z.atoms){if(Y.has(G.key))continue;if(G.install(this),G.type==="mutable_atom"){let _=n(Z,G),X=c0(G);Y.add(_.key),Y.add(X.key)}}for(let[,G]of Z.readonlySelectors)G.install(this);for(let[,G]of Z.writableSelectors){if(Y.has(G.key))continue;G.install(this)}for(let[,G]of Z.transactions)G.install(this);for(let[,G]of Z.timelines)G.install(this)}}},w={get STORE(){return globalThis.ATOM_IO_IMPLICIT_STORE??=new GQ({name:"IMPLICIT_STORE",lifespan:"ephemeral"}),globalThis.ATOM_IO_IMPLICIT_STORE}};function A(Q,Z){let Y,G=Q;while(G!==null){switch(Z.type){case"atom":case"mutable_atom":Y=G.atoms.get(Z.key);break;case"writable_pure_selector":case"writable_held_selector":Y=G.writableSelectors.get(Z.key);break;case"readonly_pure_selector":case"readonly_held_selector":Y=G.readonlySelectors.get(Z.key);break;case"atom_family":case"mutable_atom_family":case"writable_pure_selector_family":case"readonly_pure_selector_family":case"writable_held_selector_family":case"readonly_held_selector_family":Y=G.families.get(Z.key);break;case"timeline":Y=G.timelines.get(Z.key);break;case"transaction":Y=G.transactions.get(Z.key);break}if(Y)return Y;G=G.child}throw new T0(Z,Q)}var qZ=(Q,Z)=>{let Y=C(Q);if(Y.operation.open)return Y.operation.prev.get(Z.key);return Y.valueMap.get(Z.key)};var qQ=(Q,Z)=>{let Y=C(Q);return uQ(Z,Q).map((_)=>{let X=Y.atoms.get(_);if(X===void 0)throw new Error(`Atom "${_}", a dependency of selector "${Z.key}", not found in store "${Q.config.name}".`);return X.subject.subscribe(`${Z.type}:${Z.key}`,(W)=>{Q.logger.info("\uD83D\uDCE2",Z.type,Z.key,"root",_,"went",W.oldValue,"->",W.newValue);let q=qZ(Y,Z),H=E0(Y,Z);Q.logger.info("\u2728",Z.type,Z.key,"went",q,"->",H),Z.subject.next({newValue:H,oldValue:q})})})};function H0(Q,Z,Y,G){function _(L){if(Q.operation.open){let M=Q.on.operationClose.subscribe(`state subscription ${Y}`,()=>{M(),G(L)})}else G(L)}let X=A(Q,Z);Q.logger.info("\uD83D\uDC40",X.type,X.key,`Adding subscription "${Y}"`);let W=X.type==="writable_pure_selector"||X.type==="readonly_pure_selector",q=null,H=_;if(W)q=qQ(Q,X),H=(L)=>{if(q)q.length=0,q.push(...qQ(Q,X));_(L)};let N=X.subject.subscribe(Y,H);return()=>{if(Q.logger.info("\uD83D\uDE48",X.type,X.key,`Removing subscription "${Y}"`),N(),q)for(let L of q)L()}}var HZ=(Q,Z,Y,G)=>{let _=A(Q,Z);Q.logger.info("\uD83D\uDC40","timeline",Z.key,`Adding subscription "${Y}"`);let X=_.subject.subscribe(Y,G);return()=>{Q.logger.info("\uD83D\uDE48","timeline",Z.key,`Removing subscription "${Y}" from timeline`),X()}};var d0=class{initializeState(Q,Z){let Y=`*${Q.key}`;Z.atoms.delete(Y),Z.valueMap.delete(Y);let G=Q.family?{key:`*${Q.family.key}`,subKey:Q.family.subKey}:void 0,_=O0(Z,{key:Y,default:null},G);if(Z.parent?.valueMap.has(Y)){let X=Z.parent.valueMap.get(Y);Z.valueMap.set(Y,X)}return _}unsubscribeFromInnerValue;unsubscribeFromState;observeCore(Q,Z,Y){let G=`tracker:${Y.config.name}:${R(Y)?Y.transactionMeta.update.key:"main"}:${Q.key}`,_=k(Y,Q);this.unsubscribeFromInnerValue=_.subscribe(G,(X)=>{g(Y,Z,X)}),this.unsubscribeFromState=H0(Y,Q,G,(X)=>{if(X.newValue!==X.oldValue)this.unsubscribeFromInnerValue(),this.unsubscribeFromInnerValue=X.newValue.subscribe(G,(W)=>{g(Y,Z,W)})})}updateCore(Q,Z,Y){let G=`tracker:${Y.config.name}:${R(Y)?Y.transactionMeta.update.key:"main"}:${Q.key}`;H0(Y,Z,G,({newValue:_,oldValue:X})=>{let W=Y.timelineTopics.getRelatedKey(Z.key);if(W){if(Y.timelines.get(W)?.timeTraveling){let N=HZ(Y,{key:W,type:"timeline"},G,(z)=>{N(),g(Y,Q,(L)=>{if(z==="redo"&&_)L.do(_);else if(z==="undo"&&X)L.undo(X);return L})});return}}let q=Y.on.operationClose.subscribe(G,()=>{q();let H=k(Y,Q),N=_===null?-1:H.getUpdateNumber(_),z=N-H.cacheUpdateNumber;if(_&&z===1)g(Y,Q,(L)=>(L.do(_),L));else Y.logger.info("\u274C","mutable_atom",Q.key,`could not be updated. Expected update number ${H.cacheUpdateNumber+1}, but got ${N}`)})})}mutableState;latestUpdateState;[Symbol.dispose];constructor(Q,Z){this.mutableState=Q;let Y=C(Z);this.latestUpdateState=this.initializeState(Q,Y),this.observeCore(Q,this.latestUpdateState,Y),this.updateCore(Q,this.latestUpdateState,Y),Y.trackers.set(Q.key,this),this[Symbol.dispose]=()=>{this.unsubscribeFromInnerValue(),this.unsubscribeFromState(),Y.trackers.delete(Q.key)}}};function m0(Q,Z,Y){Q.logger.info("\uD83D\uDD28","atom",Z.key,`creating in store "${Q.config.name}"`);let G=C(Q),{key:_,default:X}=Z,W=G.atoms.get(_),q="mutable_atom";if(W&&W.type===q)return Q.logger.error("\u274C",q,_,"Tried to create atom, but it already exists in the store."),e(W);let H=new I,N={...Z,type:q,install:(M)=>{return M.logger.info("\uD83D\uDEE0\uFE0F","atom",_,`installing in store "${M.config.name}"`),m0(M,Z,Y)},subject:H};if(Y)N.family=Y;let z=X();G.atoms.set(N.key,N),OQ(Q,_),u(G,_,z,H);let L=e(N);if(Z.effects){let M=0,P=[];for(let E of Z.effects){let j=E({setSelf:(S)=>{g(Q,L,S)},onSet:(S)=>H0(Q,L,`effect[${M}]`,S)});if(j)P.push(j);++M}N.cleanup=()=>{for(let E of P)E()}}if(new d0(L,Q),!Y)n0(L,Z,Q);return L}var NZ=class{trackers=new Map;latestUpdateAtoms;mutableAtoms;constructor(Q,Z){let Y=C0(Z,{key:`*${Q.key}`,default:null},["mutable","updates"]);this.latestUpdateAtoms=A(Z,Y),this.mutableAtoms=Q,this.mutableAtoms.subject.subscribe(`store=${Z.config.name}::tracker-atom-family`,(G)=>{let{type:_,token:X}=G;if(X.family){let W=K(X.family.subKey);switch(_){case"state_creation":this.trackers.set(W,new d0(X,Z));break;case"state_disposal":{let q=this.trackers.get(W);if(q)q[Symbol.dispose](),this.trackers.delete(W)}break}}})}};function k0(Q,Z,Y){let G={key:Z.key,type:"mutable_atom_family"},_=Q.families.get(Z.key);if(_)Q.logger.error("\u2757","mutable_atom_family",Z.key,`Overwriting an existing ${i(_)} "${_.key}" in store "${Q.config.name}". You can safely ignore this warning if it is due to hot module replacement.`);let X=new I,q=Object.assign((H)=>{let N=h(H),z={key:Z.key,subKey:N},L=`${Z.key}(${N})`,M=C(Q),P={key:L,default:()=>Z.default(H),toJson:Z.toJson,fromJson:Z.fromJson,mutable:!0};if(Z.effects)P.effects=Z.effects(H);let E=m0(M,P,z);return X.next({type:"state_creation",token:E}),E},G,{subject:X,install:(H)=>k0(H,Z),toJson:Z.toJson,fromJson:Z.fromJson,internalRoles:Y});return Q.families.set(Z.key,q),r0(Q,q,Z),new NZ(q,Q),G}var HQ=(Q,Z)=>{let Y=C(Z),G=`${Q.key}:JSON`;return Y.families.get(G)},n=(Q,Z)=>{if(Z.family){let G=C(Q),X={key:`${Z.family.key}:JSON`,type:"writable_pure_selector_family"},W=A(G,X),q=JSON.parse(Z.family.subKey);return $(Q,W,q)}return{type:"writable_pure_selector",key:`${Z.key}:JSON`}};var c0=(Q)=>{let Y={type:"atom",key:`*${Q.key}`};if(Q.family)Y.family={key:`*${Q.family.key}`,subKey:Q.family.subKey};return Y};function zZ(Q){return typeof Q==="object"&&Q!==null&&"do"in Q&&"undo"in Q&&"subscribe"in Q}function R0(Q,Z,Y){let G=Y.valueMap.get(Z.key),_=Q.valueMap.get(Z.key);if(G!==_)return _;if(G===void 0)return Z.default();Y.logger.info("\uD83D\uDCC3","atom",Z.key,"copying");let X=Z.toJson(G),W=Z.fromJson(X);return Q.valueMap.set(Z.key,W),new d0(Z,Y),W}function u(Q,Z,Y,G){let _=Q.valueMap.get(Z);if(_ instanceof I0){let X=_;if(Y instanceof Promise)return X;return Q.valueMap.set(Z,Y),Y}if(Y instanceof Promise){let X=new I0(Y);return Q.valueMap.set(Z,X),X.then((W)=>{if(Q.valueMap.get(Z)===X){u(Q,Z,W,G);let H=Q.atoms.get(Z);if(H)U0(Q,H),w0(Q,H),A0(Q);else{let N=Q.writableSelectors.get(Z)??Q.readonlySelectors.get(Z);if(N)U0(Q,N),gQ(Q,N),A0(Q)}G.next({newValue:W,oldValue:X})}}).catch((W)=>{Q.logger.error("\uD83D\uDCA5","state",Z,"rejected:",W)}),X}return Q.valueMap.set(Z,Y),Y}var DZ=(Q,Z)=>{let Y=Z.valueMap.get(Q.key);if(Q.type==="mutable_atom"&&R(Z)){let{parent:G}=Z;Y=R0(Z,Q,G)}return Y},PQ=(Q,Z)=>{let Y=Z.valueMap.get(Q);if(Y instanceof I0){let G=Y,_=Z.writableSelectors.get(Q)??Z.readonlySelectors.get(Q);if(_)G.use(_.get());return}if(Z.operation.open)Z.operation.prev.set(Q,Y);Z.valueMap.delete(Q),Z.logger.info("\uD83D\uDDD1","state",Q,"evicted")},LZ=(Q,Z)=>{return C(Q).atomsThatAreDefault.has(Z)},OQ=(Q,Z)=>{let Y=C(Q);Y.atomsThatAreDefault=new Set(Y.atomsThatAreDefault).add(Z)},MZ=(Q,Z)=>{let Y=C(Q);Y.atomsThatAreDefault=new Set(C(Q).atomsThatAreDefault),Y.atomsThatAreDefault.delete(Z)};function O0(Q,Z,Y){let{key:_,default:X}=Z;Q.logger.info("\uD83D\uDD28","atom",_,`creating in store "${Q.config.name}"`);let W=C(Q),q=W.atoms.get(_);if(q&&q.type==="atom")return Q.logger.error("\u274C","atom",_,"Tried to create atom, but it already exists in the store."),e(q);let H=new I,N={...Z,type:"atom",install:(M)=>{return M.logger.info("\uD83D\uDEE0\uFE0F","atom",_,`installing in store "${M.config.name}"`),O0(M,Z,Y)},subject:H};if(Y)N.family=Y;let z=X;if(X instanceof Function)z=X();W.atoms.set(_,N),OQ(Q,_),u(W,_,z,H);let L=e(N);if(Z.effects){let M=0,P=[];for(let E of Z.effects){let j=E({setSelf:(S)=>{g(Q,L,S)},onSet:(S)=>H0(Q,L,`effect[${M}]`,S)});if(j)P.push(j);++M}N.cleanup=()=>{for(let E of P)E()}}return L}function _Q(Q,Z){if("mutable"in Z){let _=m0(Q,Z,void 0);return Q.on.atomCreation.next(_),_}let G=O0(Q,Z,void 0);return Q.on.atomCreation.next(G),G}function jQ(Q,Z){let Y=C(Q),{key:G,family:_}=Z,X=A(Y,Z);if(!_)Q.logger.error("\u274C","atom",G,"Standalone atoms cannot be disposed.");else{X.cleanup?.();let W=Q.valueMap.get(X.key),q=A(Q,{key:_.key,type:"atom_family"}),H={type:"state_disposal",subType:"atom",token:Z,value:W};q.subject.next(H);let N=R(Y);if(Y.atoms.delete(G),Y.valueMap.delete(G),Y.selectorAtoms.delete(G),Y.atomsThatAreDefault.delete(G),Q.timelineTopics.delete(G),Z.type==="mutable_atom"){let z=c0(Z);jQ(Q,z),Q.trackers.delete(G)}if(Q.logger.info("\uD83D\uDD25","atom",G,"deleted"),N&&Y.transactionMeta.phase==="building"){let z=Y.transactionMeta.update.updates.at(-1);if(!(z?.type==="molecule_disposal"&&z.values.some(([P])=>P===X.family?.key)))Y.transactionMeta.update.updates.push(H)}else Q.on.atomDisposal.next(Z)}}var h0=class{toolkit;options;defaultContent;molecules=new Map;relations;states;core;transact(Q,Z){let Y=this.toolkit;this.toolkit=Q,Z(this),this.toolkit=Y}store;realm;[Symbol.dispose](){}constructor(Q,Z,Y=w.STORE){this.store=Y,this.realm=new o0(Y),this.options=Q,this.defaultContent=Z,this.store.miscResources.set(`join:${Q.key}`,this),this.realm.allocate("root",Q.key),this.toolkit={get:(...D)=>k(Y,...D),set:(...D)=>{g(Y,...D)},find:(...D)=>$(Y,...D),json:(D)=>n(Y,D)};let G=Q.between[0],_=Q.between[1],X=k0(Y,{key:`${Q.key}/relatedKeys`,default:()=>new f,mutable:!0,fromJson:(D)=>f.fromJSON(D),toJson:(D)=>D.toJSON()},["join","relations"]);this.core={relatedKeysAtoms:X};let W=({get:D},J)=>D(X,J),q=({set:D},J,B)=>{if(!this.store.molecules.has(h(J)))this.realm.allocate(Q.key,J);D(X,J,(O)=>O.add(B)),D(X,B,(O)=>O.add(J))},H=({set:D},J,B)=>{D(X,J,(O)=>{return O.delete(B),O}),D(X,B,(O)=>{return O.delete(J),O})},N=(D,J,B)=>{let{find:O,get:V,set:F}=D,x=O(X,J),m=V(x);for(let b of m){if(B.includes(b))continue;F(X,b,(p)=>{return p.delete(J),p})}F(x,(b)=>{return b.transaction((Q0)=>{Q0.clear();for(let p of B){let Z0=W(D,p),l0=Z0.has(J);if(this.relations.cardinality==="1:n"){let i0=[];for(let Y0 of Z0){if(Y0===J)continue;let G0=W(D,Y0);if(G0.delete(p),G0.size===0)i0.push(Y0)}if(!l0&&Z0.size>0)Z0.clear();for(let Y0 of i0){let G0=[p,Y0].sort(),$Q=`"${G0[0]}:${G0[1]}"`;this.molecules.delete($Q)}}if(!l0)Z0.add(J);Q0.add(p)}return!0}),b})},z=(D,J,B)=>{let{set:O}=D;O(X,J,(V)=>{return V.transaction((F)=>{for(let x of B)F.add(x);return!0}),V});for(let V of B)O(X,V,(F)=>{return F.add(J),F});return!0},L=(D,J,B)=>{let O=W(D,J);return B?O.has(B):O.size>0},M={getRelatedKeys:(D)=>W(this.toolkit,D),addRelation:(D,J)=>{this.store.moleculeJoins.set(D,Q.key),this.store.moleculeJoins.set(J,Q.key),q(this.toolkit,D,J)},deleteRelation:(D,J)=>{H(this.toolkit,D,J)},replaceRelationsSafely:(D,J)=>{N(this.toolkit,D,J)},replaceRelationsUnsafely:(D,J)=>{z(this.toolkit,D,J)},has:(D,J)=>L(this.toolkit,D,J)},P,E;if(Z){E=C0(Y,{key:`${Q.key}/content`,default:Z},["join","content"]);let D=({get:O},V)=>O(E,V),J=({set:O},V,F)=>{O(E,V,F)};P=Object.assign(M,{getContent:(O)=>{return D(this.toolkit,O)},setContent:(O,V)=>{J(this.toolkit,O,V)},deleteContent:(O)=>{this.realm.deallocate(O)}})}else P=M;let j=new v(Q,{externalStore:P,isAType:Q.isAType,isBType:Q.isBType,makeContentKey:(...D)=>{let[J,B]=D,O=D.sort(),V=`${O[0]}:${O[1]}`,F=Y.molecules.get(h(J)),x=Y.molecules.get(h(B));if(!F)this.realm.allocate(Q.key,J);if(!x)this.realm.allocate(Q.key,B);return this.realm.allocate(J,V,"all"),this.realm.claim(B,V),this.store.moleculeJoins.set(V,Q.key),V}}),S=()=>t(Y,{key:`${Q.key}/singleRelatedKey`,get:(D)=>({get:J})=>{let B=J(X,D);for(let O of B)return O;return null}},["join","keys"]),l=()=>{return t(Y,{key:`${Q.key}/multipleRelatedKeys`,get:(D)=>({get:J})=>{let B=HQ(X,Y);return J(B,D).members}},["join","keys"])},d=()=>t(Y,{key:`${Q.key}/singleRelatedEntry`,get:(D)=>({get:J})=>{let B=J(X,D);for(let O of B){let V=j.isAType?.(D)?D:void 0,F=V===void 0?D:void 0;V??=O,F??=O;let x=j.makeContentKey(V,F),m=J(E,x);return[O,m]}return null}},["join","entries"]),T=()=>t(Y,{key:`${Q.key}/multipleRelatedEntries`,get:(D)=>({get:J})=>{let B=HQ(X,Y);return J(B,D).members.map((V)=>{let F=j.isAType?.(D)?D:void 0,x=F===void 0?D:void 0;F??=V,x??=V;let m=j.makeContentKey(F,x),b=J(E,m);return[V,b]})}},["join","entries"]);switch(Q.cardinality){case"1:1":{let D=S(),J=`${G}KeyOf${U(_)}`,B=`${_}KeyOf${U(G)}`,O={[J]:D,[B]:D},V;if(Z){let F=d(),x=`${G}EntryOf${U(_)}`,m=`${_}EntryOf${U(G)}`,b={[x]:F,[m]:F};V=Object.assign(O,b)}else V=O;this.relations=j,this.states=V;break}case"1:n":{let D=S(),J=l(),B=`${G}KeyOf${U(_)}`,O=`${_}KeysOf${U(G)}`,V={[B]:D,[O]:J},F;if(Z){let x=d(),m=T(),b=`${G}EntryOf${U(_)}`,Q0=`${_}EntriesOf${U(G)}`,p={[b]:x,[Q0]:m};F=Object.assign(V,p)}else F=V;this.relations=j,this.states=F;break}case"n:n":{let D=l(),J=`${G}KeysOf${U(_)}`,B=`${_}KeysOf${U(G)}`,O={[J]:D,[B]:D},V;if(Z){let F=T(),x=`${G}EntriesOf${U(_)}`,m=`${_}EntriesOf${U(G)}`,b={[x]:F,[m]:F};V=Object.assign(O,b)}else V=O;this.relations=j,this.states=V}}}};function u0(Q,Z){let Y=Z.joins.get(Q.key);if(Y===void 0){let _=w.STORE.joins.get(Q.key);if(_===void 0)throw new Error(`Join "${Q.key}" not found in store "${Z.config.name}"`);Y=new h0(_.options,_.defaultContent,Z),Z.joins.set(Q.key,Y)}return Y}function q0(Q,Z,Y){let G=u0(Q,Y),_=C(Y);if(R(_)){let{toolkit:X}=_.transactionMeta;G.transact(X,({relations:W})=>{Z(W)})}else Z(G.relations)}function XQ(Q,Z,Y){let G=u0(Q,Y),_;switch(Q.cardinality){case"1:1":{let X=`${Q.a}KeyOf${U(Q.b)}`,W=`${Q.b}KeyOf${U(Q.a)}`;_={get[X](){let H=G.states[X];return $(Y,H,Z)},get[W](){let H=G.states[W];return $(Y,H,Z)}};let q=`${Q.a}EntryOf${U(Q.b)}`;if(q in G.states){let H=`${Q.b}EntryOf${U(Q.a)}`;Object.assign(_,{get[q](){let N=G.states[q];return $(Y,N,Z)},get[H](){let N=G.states[H];return $(Y,N,Z)}})}break}case"1:n":{let X=`${Q.a}KeyOf${U(Q.b)}`,W=`${Q.b}KeysOf${U(Q.a)}`;_={get[X](){let H=G.states[X];return $(Y,H,Z)},get[W](){let H=G.states[W];return $(Y,H,Z)}};let q=`${Q.a}EntryOf${U(Q.b)}`;if(q in G.states){let H=`${Q.b}EntriesOf${U(Q.a)}`;Object.assign(_,{get[q](){let N=G.states[q];return $(Y,N,Z)},get[H](){let N=G.states[H];return $(Y,N,Z)}})}break}case"n:n":{let X=`${Q.a}KeysOf${U(Q.b)}`,W=`${Q.b}KeysOf${U(Q.a)}`;_={get[X](){let H=G.states[X];return $(Y,H,Z)},get[W](){let H=G.states[W];return $(Y,H,Z)}};let q=`${Q.a}EntriesOf${U(Q.b)}`;if(q in G.states){let H=`${Q.b}EntriesOf${U(Q.a)}`;Object.assign(_,{get[q](){let N=G.states[q];return $(Y,N,Z)},get[H](){let N=G.states[H];return $(Y,N,Z)}})}}}return _}function WQ(Q,Z){return u0(Q,Z).core.relatedKeysAtoms}function JZ(Q){return Q.startsWith("\uD83D\uDD0D ")}var EZ=class extends Map{set(Q,Z){if(this.has(Q))return console.warn("Tried to set a key that already exists in an InvariantMap",{key:Q,value:Z}),this;return super.set(Q,Z)}clear(){throw new Error("Cannot clear an InvariantMap")}},_1=class Q{type="continuity";globals=[];actions=[];perspectives=[];key;constructor(Z){this.key=Z}static existing=new EZ;static create(Z,Y){let G=new Q(Z),{type:_,globals:X,actions:W,perspectives:q}=Y(G),H={type:_,key:Z,globals:X,actions:W,perspectives:q};return Q.existing.set(Z,H),H}add(...Z){switch(Z[0].type){case"atom":case"mutable_atom":this.globals.push(...Z);break;case"transaction":this.actions.push(...Z);break;case"atom_family":case"mutable_atom_family":{let[G,_]=Z;this.perspectives.push({type:"realtime_perspective",resourceAtoms:G,viewAtoms:_})}break}return this}};var X1=r({key:"usersInRoomIndex",mutable:!0,default:()=>new f,toJson:(Q)=>Q.toJSON(),fromJson:(Q)=>f.fromJSON(Q)}),p0=r({key:"roomIndex",default:()=>new f,mutable:!0,toJson:(Q)=>Q.toJSON(),fromJson:(Q)=>f.fromJSON(Q)}),CZ={enteredAtEpoch:0},z0=M0({key:"usersInRooms",between:["room","user"],cardinality:"1:n",isAType:(Q)=>typeof Q==="string",isBType:(Q)=>typeof Q==="string"},CZ),W1=J0({key:"usersInMyRoomView",get:(Q)=>({find:Z})=>{let Y=a0(z0);return[Z(Y,Q)]}});import{spawn as PZ}from"child_process";var y0=class{listeners;globalListeners;handleEvent(Q,...Z){for(let G of this.globalListeners)G(Q,...Z);let Y=this.listeners.get(Q);if(Y)for(let G of Y)G(...Z)}id="no_id_retrieved";emit;constructor(Q){this.emit=Q,this.listeners=new Map,this.globalListeners=new Set}on(Q,Z){let Y=this.listeners.get(Q);if(Y)Y.add(Z);else this.listeners.set(Q,new Set([Z]));return this}onAny(Q){return this.globalListeners.add(Q),this}off(Q,Z){let Y=this.listeners.get(Q);if(Y)if(Z)Y.delete(Z);else this.listeners.delete(Q);return this}offAny(Q){return this.globalListeners.delete(Q),this}},OZ=class extends y0{incompleteData="";unprocessedEvents=[];incompleteLog="";unprocessedLogs=[];id="#####";process;key;logger;handleLog(Q){if(Array.isArray(Q)){let[Z,...Y]=Q;switch(Z){case"i":this.logger.info(...Y);break;case"w":this.logger.warn(...Y);break;case"e":this.logger.error(...Y);break;default:return}}}constructor(Q,Z,Y){super((G,..._)=>{let X=JSON.stringify([G,..._])+"\x03",W=(q)=>{if(q.code==="EPIPE")console.error("EPIPE error during write",this.process.stdin);this.process.stdin.removeListener("error",W)};return this.process.stdin.once("error",W),this.process.stdin.write(X),this});if(this.process=Q,this.key=Z,this.logger=Y??{info:(...G)=>{console.info(this.id,this.key,...G)},warn:(...G)=>{console.warn(this.id,this.key,...G)},error:(...G)=>{console.error(this.id,this.key,...G)}},this.process.stdout.on("data",(G)=>{let _=G.toString();if(_==="ALIVE")return;this.unprocessedEvents.push(..._.split("\x03"));let X=this.unprocessedEvents.shift();this.incompleteData+=X??"";try{if(this.incompleteData.startsWith("error"))console.log("\u2757",this.incompleteData);let W=K(this.incompleteData);this.handleEvent(...W);while(this.unprocessedEvents.length>0){let q=this.unprocessedEvents.shift();if(q){if(this.unprocessedEvents.length===0)this.incompleteData=q;W=K(q),this.handleEvent(...W)}}this.incompleteData=""}catch(W){console.warn("\u26A0\uFE0F----------------\u26A0\uFE0F"),console.warn(this.incompleteData),console.warn("\u26A0\uFE0F----------------\u26A0\uFE0F"),console.error(W)}}),this.process.stderr.on("data",(G)=>{let _=G.toString();this.unprocessedLogs.push(..._.split("\x03"));let X=this.unprocessedLogs.shift();this.incompleteLog+=X??"";try{let W=K(this.incompleteLog);this.handleLog(W);while(this.unprocessedLogs.length>0)if(this.incompleteLog=this.unprocessedLogs.shift()??"",this.incompleteLog)W=K(this.incompleteLog),this.handleLog(W)}catch(W){console.error("\u274C\u274C\u274C"),console.error(this.incompleteLog),console.error(W),console.error("\u274C\u274C\u274C\uFE0F")}}),Q.pid)this.id=Q.pid.toString()}},jZ=class extends y0{in;out;id="no_id_retrieved";disposalFunctions=[];constructor(Q){super((...Z)=>{return this.out.next(Z),this});this.id=Q,this.in=new I,this.out=new I,this.in.subscribe("socket",(Z)=>{this.handleEvent(...Z)})}dispose(){for(let Q of this.disposalFunctions)Q()}},BQ=class extends y0{incompleteData="";unprocessedEvents=[];relays;relayServices;process;id="#####";log(...Q){this.process.stderr.write(h(Q.map((Z)=>Z instanceof f?`{ ${Z.toJSON().members.join(" | ")} }`:Z))+"\x03")}logger={info:(...Q)=>{this.log("i",...Q)},warn:(...Q)=>{this.log("w",...Q)},error:(...Q)=>{this.log("e",...Q)}};constructor(){super((Q,...Z)=>{let Y=JSON.stringify([Q,...Z]);return this.process.stdout.write(Y+"\x03"),this});if(this.process=process,this.process.stdin.resume(),this.relays=new Map,this.relayServices=[],this.process.stdin.on("data",(Q)=>{let Z=Q.toString();this.unprocessedEvents.push(...Z.split("\x03"));let Y=this.unprocessedEvents.shift();this.incompleteData+=Y??"";try{let G=K(this.incompleteData);this.logger.info("\uD83C\uDFB0","received",G),this.handleEvent(...G);while(this.unprocessedEvents.length>0){let _=this.unprocessedEvents.shift();if(_){if(this.unprocessedEvents.length===0)this.incompleteData=_;let X=K(_);this.handleEvent(...X)}}this.incompleteData=""}catch(G){if(G instanceof Error)this.logger.error("\u2757",G.message,G.cause,G.stack)}}),this.on("exit",()=>{this.logger.info("\uD83D\uDD25",this.id,'received "exit"'),process.exit(0)}),process.on("exit",(Q)=>{this.logger.info("\uD83D\uDD25",this.id,`exited with code ${Q}`)}),process.on("end",()=>{this.logger.info("\uD83D\uDD25",this.id,"ended"),process.exit(0)}),process.on("SIGTERM",()=>{this.logger.error("\uD83D\uDD25",this.id,"terminated"),process.exit(0)}),process.on("SIGINT",()=>{this.logger.error("\uD83D\uDD25",this.id,"interrupted"),process.exit(0)}),process.pid)this.id=process.pid?.toString();this.on("user-joins",(Q)=>{this.logger.info("\uD83D\uDC64","user",Q,"joined");let Z=new jZ(`user:${Q}`);this.relays.set(Q,Z),this.logger.info("\uD83D\uDD17","attaching services:",`[${[...this.relayServices.keys()].join(", ")}]`);for(let Y of this.relayServices){let G=Y(Z);if(G)Z.disposalFunctions.push(G)}this.on(`user:${Q}`,(...Y)=>{Z.in.next(Y)}),Z.out.subscribe("socket",(Y)=>{this.emit(...Y)})}),this.on("user-leaves",(Q)=>{let Z=this.relays.get(Q);if(this.off(`relay:${Q}`),Z)Z.dispose(),this.relays.delete(Q)}),process.stdout.write("ALIVE")}relay(Q){this.logger.info("\uD83D\uDD17","running relay method"),this.relayServices.push(Q)}},VQ=_0({key:"roomArguments",default:["echo",["Hello World!"]]}),BZ=J0({key:"room",get:(Q)=>async({get:Z,find:Y})=>{let G=Y(VQ,Q),_=Z(G),[X,W]=_,q=await new Promise((H)=>{let N=PZ(X,W,{env:process.env}),z=(L)=>{if(L.toString()==="ALIVE")N.stdout.off("data",z),H(N)};N.stdout.on("data",z)});return new OZ(q,Q)}}),C1=X0({key:"createRoom",do:({get:Q,set:Z,find:Y},G,_,X)=>{let W=X?[_,X]:[_],q=Y(VQ,G);Z(q,W),Z(p0,(z)=>z.add(G));let H=Y(BZ,G);return Q(H)}}),P1=X0({key:"joinRoom",do:(Q,Z,Y,G)=>{let _={enteredAtEpoch:G};return q0(z0,(X)=>{X.set({room:Z,user:Y},_)},Q.env().store),_}}),O1=X0({key:"leaveRoom",do:(Q,Z,Y)=>{q0(z0,(G)=>{G.delete({room:Z,user:Y})},Q.env().store)}}),j1=X0({key:"destroyRoom",do:(Q,Z)=>{q0(z0,(Y)=>{Y.delete({room:Z})},Q.env().store),Q.set(p0,(Y)=>(Y.delete(Z),Y))}});var B1=_0({key:"redactor",default:{occlude:(Q)=>Q}}),V1=_0({key:"unacknowledgedUpdates",default:()=>[]}),$1=_0({key:"sockets",default:null}),F1=r({key:"socketsIndex",mutable:!0,default:()=>new f,toJson:(Q)=>Q.toJSON(),fromJson:(Q)=>f.fromJSON(Q)}),h1=r({key:"usersIndex",mutable:!0,default:()=>new f,toJson:(Q)=>Q.toJSON(),fromJson:(Q)=>f.fromJSON(Q)}),I1=M0({key:"usersOfSockets",between:["user","socket"],cardinality:"1:1",isAType:(Q)=>Q.startsWith("user::"),isBType:(Q)=>Q.startsWith("socket::")});var D0=new BQ;Object.assign(console,D0.logger,{log:D0.logger.info});D0.on("timeToStop",function Q(){D0.logger.info("\uD83D\uDEEC game worker exiting"),process.exit(0)});D0.logger.info("\uD83D\uDEEB game worker ready");
|