tempest.games 0.0.10 → 0.0.12

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 CHANGED
@@ -1,5 +1,18 @@
1
1
  # tempest.games
2
2
 
3
+ ## 0.0.12
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [8731eb0]
8
+ - atom.io@0.29.4
9
+
10
+ ## 0.0.11
11
+
12
+ ### Patch Changes
13
+
14
+ - 4270cd7: ✨ Add explicit teardown functions.
15
+
3
16
  ## 0.0.10
4
17
 
5
18
  ### Patch Changes
@@ -14,7 +14,7 @@ var Id=Object.defineProperty;var jd=(e,t,n)=>t in e?Id(e,t,{enumerable:!0,config
14
14
  *
15
15
  * This source code is licensed under the MIT license found in the
16
16
  * LICENSE file in the root directory of this source tree.
17
- */var Zd=fe,Gd=Symbol.for("react.element"),eh=Symbol.for("react.fragment"),th=Object.prototype.hasOwnProperty,nh=Zd.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner,rh={key:!0,ref:!0,__self:!0,__source:!0};function ec(e,t,n){var r,i={},o=null,s=null;n!==void 0&&(o=""+n),t.key!==void 0&&(o=""+t.key),t.ref!==void 0&&(s=t.ref);for(r in t)th.call(t,r)&&!rh.hasOwnProperty(r)&&(i[r]=t[r]);if(e&&e.defaultProps)for(r in t=e.defaultProps,t)i[r]===void 0&&(i[r]=t[r]);return{$$typeof:Gd,type:e,key:o,ref:s,props:i,_owner:nh.current}}no.Fragment=eh;no.jsx=ec;no.jsxs=ec;ba.exports=no;var K=ba.exports;function al(e=Math.random){return e().toString(36).slice(2)}class es extends Promise{constructor(n){let r,i;super((o,s)=>{r=o,i=s});C(this,"fate");C(this,"resolve");C(this,"reject");C(this,"done",!1);this.resolve=r,this.reject=i,this.use(n instanceof Promise?n:new Promise(n))}pass(n,r){n===this.fate&&(this.resolve(r),this.done=!0)}fail(n,r){n===this.fate&&(this.reject(r),this.done=!0)}use(n){if(n instanceof Promise){const r=n;this.fate=r,r.then(i=>{this.pass(r,i)},i=>{this.fail(r,i)})}else this.resolve(n),this.fate=void 0}}const ih=(e,t,n=_e.STORE)=>cc(n,{key:`${e.key}:JSON`,get:({get:r})=>t.toJson(r(e)),set:({set:r},i)=>{r(e,t.fromJson(i))}});function $(e){for(;e.child!==null;)e=e.child;return e}function oh(e){for(;e.parent!==null;)e=e.parent;return e}class Y{constructor(){C(this,"Subscriber");C(this,"subscribers",new Map)}subscribe(t,n){return this.subscribers.set(t,n),()=>{this.unsubscribe(t)}}unsubscribe(t){this.subscribers.delete(t)}next(t){const n=this.subscribers.values();for(const r of n)r(t)}}class sh extends Y{constructor(n){super();C(this,"state");this.state=n}next(n){this.state=n,super.next(n)}}function cl(e,t,n){const r={key:t.key,type:"atom_family"},i=e.families.get(t.key);i&&e.logger.error("❗","atom_family",t.key,`Overwriting an existing ${Fn(i)} "${i.key}" in store "${e.config.name}". You can safely ignore this warning if it is due to hot module replacement.`);const o=new Y,l=Object.assign(u=>{const a=X(u),c={key:t.key,subKey:a},p=`${t.key}(${a})`,d=$(e),m=t.default,w={key:p,default:m instanceof Function?m(u):m};t.effects&&(w.effects=t.effects(u));const S=oo(d,w,c);return o.next({type:"state_creation",token:S}),S},r,{subject:o,install:u=>cl(u,t),internalRoles:n});return e.families.set(t.key,l),e.defaults.set(t.key,t.default),r}function vn(e,t,n){const r={key:t.key,type:"readonly_selector_family"},i=e.families.get(t.key);i&&e.logger.error("❗","readonly_selector_family",t.key,`Overwriting an existing ${Fn(i)} "${i.key}" in store "${e.config.name}". You can safely ignore this warning if it is due to hot module replacement.`);const o=new Y,l=Object.assign(u=>{const a=X(u),c={key:t.key,subKey:a},p=`${t.key}(${a})`,d=$(e),m=gl(d,{key:p,get:t.get(u)},c);return o.next({type:"state_creation",token:m}),m},r,{internalRoles:n,subject:o,install:u=>vn(u,t),default:u=>t.get(u)({get:(...c)=>je(e,...c),find:(c,p)=>Oe(e,c,p),seek:(c,p)=>ke(e,c,p),json:c=>Kt(e,c)})});return e.families.set(t.key,l),r}function lh(e,t){return"set"in t?wl(e,t):vn(e,t)}const uh={atom_family:"atom",mutable_atom_family:"mutable_atom",selector_family:"selector",readonly_selector_family:"readonly_selector",molecule_family:"molecule"};function fl(e,t){const n=X(t),r=`${e.key}(${n})`,i=uh[e.type],o={key:r,type:i};return i==="molecule"?Object.assign(o,{key:t,family:e}):Object.assign(o,{family:{key:e.key,subKey:n}}),Object.assign(o,{counterfeit:!0}),o}function tn(e){const t={key:e.key,type:e.type};return"family"in e&&(t.family=e.family),t}function fn(e){return rp(_e.STORE,e)}const xo=e=>(t,n,r,i,...o)=>{console[e](`${t} ${n} "${r}" ${i}`,...o)},ah={error:xo("error"),info:xo("info"),warn:xo("warn")};class ch{constructor(t,n,r=ah){C(this,"error",(...t)=>{var n;(((n=this.filter)==null?void 0:n.call(this,...t))??!0)&&this.logLevel!==null&&this.logger.error(...t)});C(this,"info",(...t)=>{var n;(((n=this.filter)==null?void 0:n.call(this,...t))??!0)&&this.logLevel==="info"&&this.logger.info(...t)});C(this,"warn",(...t)=>{var n;(((n=this.filter)==null?void 0:n.call(this,...t))??!0)&&this.logLevel!=="error"&&this.logLevel!==null&&this.logger.warn(...t)});this.logLevel=t,this.filter=n,this.logger=r}}function fh(e){return cc(_e.STORE,e)}function dh(e){return lh(_e.STORE,e)}function hh(e){return lc(e,_e.STORE)}function ph(e,t=al()){return ro(e,t,_e.STORE)}class pt{constructor(t,n){C(this,"a");C(this,"b");C(this,"cardinality");C(this,"relations",new Map);C(this,"contents",new Map);C(this,"isContent");C(this,"makeContentKey",(t,n)=>`${t}:${n}`);var r;if(this.a=t.between[0],this.b=t.between[1],this.cardinality=t.cardinality,n!=null&&n.externalStore||(this.relations=new Map((r=t.relations)==null?void 0:r.map(([i,o])=>[i,new Set(o)])),this.contents=new Map(t.contents)),this.isContent=(n==null?void 0:n.isContent)??null,n!=null&&n.makeContentKey&&(this.makeContentKey=n.makeContentKey),n!=null&&n.externalStore){const i=n.externalStore;this.has=(o,s)=>i.has(o,s),this.addRelation=(o,s)=>{i.addRelation(o,s)},this.deleteRelation=(o,s)=>{i.deleteRelation(o,s)},this.replaceRelationsSafely=(o,s)=>{i.replaceRelationsSafely(o,s)},this.replaceRelationsUnsafely=(o,s)=>{i.replaceRelationsUnsafely(o,s)},this.getRelatedKeys=o=>i.getRelatedKeys(o),i.getContent&&(this.getContentInternal=o=>i.getContent(o),this.setContent=(o,s)=>{i.setContent(o,s)},this.deleteContent=o=>{i.deleteContent(o)});for(const[o,s]of t.relations??[])for(const l of s)this.addRelation(o,l);for(const[o,s]of t.contents??[])this.setContent(o,s)}}getRelatedKeys(t){return this.relations.get(t)}addRelation(t,n){let r=this.relations.get(t),i=this.relations.get(n);r?r.add(n):(r=new Set([n]),this.relations.set(t,r)),i?i.add(t):(i=new Set([t]),this.relations.set(n,i))}deleteRelation(t,n){const r=this.relations.get(t);if(r){r.delete(n),r.size===0&&this.relations.delete(t);const i=this.relations.get(n);i&&(i.delete(t),i.size===0&&this.relations.delete(n))}}replaceRelationsUnsafely(t,n){this.relations.set(t,new Set(n));for(const r of n){const i=new Set([t]);this.relations.set(r,i)}}replaceRelationsSafely(t,n){const r=this.relations.get(t);if(r)for(const i of r){const o=this.relations.get(i);o&&(o.size===1?this.relations.delete(i):o.delete(t),this.contents.delete(this.makeContentKey(t,i)))}this.relations.set(t,new Set(n));for(const i of n){let o=this.relations.get(i);o?o.add(t):(o=new Set([t]),this.relations.set(i,o))}}getContentInternal(t){return this.contents.get(t)}setContent(t,n){this.contents.set(t,n)}deleteContent(t){this.contents.delete(t)}toJSON(){return{between:[this.a,this.b],cardinality:this.cardinality,relations:[...this.relations.entries()].map(([t,n])=>[t,[...n]]),contents:[...this.contents.entries()]}}set(t,...n){const r=typeof n[0]=="string"?n[0]:t[this.b],i=n[1]??typeof n[0]=="string"?void 0:n[0];switch(t=typeof t=="string"?t:t[this.a],this.cardinality){case"1:1":{const o=this.getRelatedKey(t);o&&o!==r&&this.delete(o,t)}case"1:n":{const o=this.getRelatedKey(r);o&&o!==t&&this.delete(o,r)}}if(i){const o=this.makeContentKey(t,r);this.setContent(o,i)}return this.addRelation(t,r),this}delete(t,n){n=typeof n=="string"?n:t[this.b];const r=typeof t=="string"?t:t[this.a];if(r===void 0&&typeof n=="string"){const i=this.getRelatedKeys(n);if(i)for(const o of i)this.delete(o,n)}if(typeof r=="string"&&n===void 0){const i=this.getRelatedKeys(r);if(i)for(const o of i)this.delete(r,o)}if(typeof r=="string"&&typeof n=="string"){this.deleteRelation(r,n);const i=this.makeContentKey(r,n);this.deleteContent(i)}return this}getRelatedKey(t){const n=this.getRelatedKeys(t);if(n){n.size>1&&console.warn(`${n.size} related keys were found for key "${t}": (${[...n].map(r=>`"${r}"`).join(", ")}). Only one related key was expected.`);for(const r of n)return r}}replaceRelations(t,n,r){const i=!Array.isArray(n),o=i?Object.keys(n):n;if(r!=null&&r.reckless?this.replaceRelationsUnsafely(t,o):this.replaceRelationsSafely(t,o),i)for(const s of o){const l=this.makeContentKey(t,s),u=n[s];this.setContent(l,u)}return this}getContent(t,n){const r=this.makeContentKey(t,n);return this.getContentInternal(r)}getRelationEntries(t){const n=t[this.a],r=t[this.b];if(n!==void 0&&r===void 0){const i=this.getRelatedKeys(n);if(i)return[...i].map(o=>[o,this.getContent(n,o)??null])}if(n===void 0&&r!==void 0){const i=this.getRelatedKeys(r);if(i)return[...i].map(o=>[o,this.getContent(o,r)??null])}return[]}has(t,n){if(n){const r=this.getRelatedKeys(t);return(r==null?void 0:r.has(n))??!1}return this.relations.has(t)}}function Je(e){return"epoch"in e.transactionMeta}function ge(e){return"phase"in e.transactionMeta}const yh=e=>{const t=$(e);if(!ge(t)){e.logger.warn("🐞","transaction","???","abortTransaction called outside of a transaction. This is probably a bug in AtomIO.");return}e.logger.info("🪂","transaction",t.transactionMeta.update.key,"Aborting transaction"),t.parent.child=null},mh=e=>e[0].toUpperCase()+e.slice(1);function Fn(e){return e.type.split("_").map(mh).join(" ")}class Ft extends Error{constructor(...t){const n=t[0],r=t.length===2?t[1]:t[2];if(t.length===2)super(`${Fn(n)} ${X(n.key)} not found in store "${r.config.name}".`);else{const i=t[1];super(`${Fn(n)} "${n.key}" member ${X(i)} not found in store "${r.config.name}".`)}}}function ro(e,t,n){return(...r)=>{const i=D(e,n);if(i)return i.run(r,t);throw new Ft(e,n)}}const tc=e=>t=>e instanceof Function?e(t):e,io=(e,t)=>{if(t.valueMap.has(e.key))return t.logger.info("📖",e.type,e.key,"reading cached value"),Gh(e,t);if(e.type==="selector"||e.type==="readonly_selector")return t.logger.info("🧮",e.type,e.key,"computing value"),e.get();const n=e.default instanceof Function?e.default():e.default;return t.logger.info("💁","atom",e.key,"could not find cached value; using default",n),e.default instanceof Function?e.default():e.default},gh=(e,t)=>{if(e.operation.open){const n=performance.now();return e.logger.info("❗",t.type,t.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:t},e.logger.info("⭕",t.type,t.key,`operation start in store "${e.config.name}"${ge(e)?` ${e.transactionMeta.phase} "${e.transactionMeta.update.key}"`:""}`)},vh=e=>{e.operation.open&&e.logger.info("🔴",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)},wh=(e,t)=>e.operation.open?e.operation.done.has(t):(e.logger.error("🐞","unknown",t,"isDone called outside of an operation. This is probably a bug in AtomIO."),!0),dl=(e,t)=>{if(!e.operation.open){e.logger.error("🐞","unknown",t,"markDone called outside of an operation. This is probably a bug in AtomIO.");return}e.operation.done.add(t)},kh=(e,t,n)=>{switch(e.type){case"mutable_atom":n.logger.info("📢",e.type,e.key,"is now (",t.newValue,") subscribers:",e.subject.subscribers);break;default:n.logger.info("📢",e.type,e.key,"went (",t.oldValue,"->",t.newValue,") subscribers:",e.subject.subscribers)}e.subject.next(t)},Eu=(e,t)=>{const n=$(t),r=n.selectorAtoms.getRelatedKeys(e.key);if(n.logger.info("🧹",e.type,e.key,r?`evicting ${r.size} states downstream:`:"no downstream states",r??"to evict"),r){n.operation.open&&n.logger.info("🧹",e.type,e.key,`[ ${[...n.operation.done].join(", ")} ] already done`);for(const i of r)wh(n,i)||(ep(i,n),dl(n,i))}};function Sh(e,t){return!(Zh(t.newValue)||e.includes("🔍"))}const _h=(e,t,n)=>{const{key:r}=e,i=$(n);if(!ge(i)||i.transactionMeta.phase!=="building"){n.logger.error("🐞","atom",r,"stowUpdate called outside of a transaction. This is probably a bug.");return}if(!Sh(r,t))return;const s={type:"atom_update",key:r,...t};e.family&&(s.family=e.family),i.transactionMeta.update.updates.push(s),n.logger.info("📁","atom",r,"stowed (",t.oldValue,"->",t.newValue,")")},Eh=(e,t,n)=>{const r=io(e,n);let i=r;if(e.type==="mutable_atom"&&ge(n)){const{parent:s}=n;i=os(e,s,n)}i=tc(t)(i),n.logger.info("📝","atom",e.key,"set to",i),i=on(e.key,i,e.subject,n),tp(e.key,n)&&np(e.key,n),dl(n,e.key),Eu(e,n);const o={oldValue:r,newValue:i};if(Je(n))kh(e,o,n);else if(n.parent){if(n.on.transactionApplying.state===null)_h(e,o,n);else if(e.key.startsWith("*")){const s=e.key.slice(1),l=n.atoms.get(s);let u=n.valueMap.get(s);if(l.type==="mutable_atom"&&ge(n)){const{parent:c}=n;u=os(l,c,n)}u.do(o.newValue)===null&&Eu(l,n)}}},nc=(e,t,n)=>{switch(e.type){case"atom":case"mutable_atom":Eh(e,t,n);break;case"selector":e.set(t);break}};function rc(e,t){if(t.family){const n=e.families.get(t.family.key);if(n)return n}}function oe(e,...t){let n,r,i,o;if(t.length===2?(n=t[0],o=t[1],r=rc(e,n)??null,r&&(i=n.family?rn(n.family.subKey):null,n=Oe(e,r,i))):(r=t[0],i=t[1],o=t[2],n=Oe(e,r,i)),"counterfeit"in n){const u=e.disposalTraces.buffer.find(a=>(a==null?void 0:a.key)===n.key);e.logger.error("❌",n.type,n.key,`could not be set because it was not found in the store "${e.config.name}".`,u?`This state was previously disposed:
17
+ */var Zd=fe,Gd=Symbol.for("react.element"),eh=Symbol.for("react.fragment"),th=Object.prototype.hasOwnProperty,nh=Zd.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner,rh={key:!0,ref:!0,__self:!0,__source:!0};function ec(e,t,n){var r,i={},o=null,s=null;n!==void 0&&(o=""+n),t.key!==void 0&&(o=""+t.key),t.ref!==void 0&&(s=t.ref);for(r in t)th.call(t,r)&&!rh.hasOwnProperty(r)&&(i[r]=t[r]);if(e&&e.defaultProps)for(r in t=e.defaultProps,t)i[r]===void 0&&(i[r]=t[r]);return{$$typeof:Gd,type:e,key:o,ref:s,props:i,_owner:nh.current}}no.Fragment=eh;no.jsx=ec;no.jsxs=ec;ba.exports=no;var K=ba.exports;function al(e=Math.random){return e().toString(36).slice(2)}class es extends Promise{constructor(n){let r,i;super((o,s)=>{r=o,i=s});C(this,"fate");C(this,"resolve");C(this,"reject");C(this,"done",!1);this.resolve=r,this.reject=i,this.use(n instanceof Promise?n:new Promise(n))}pass(n,r){n===this.fate&&(this.resolve(r),this.done=!0)}fail(n,r){n===this.fate&&(this.reject(r),this.done=!0)}use(n){if(n instanceof Promise){const r=n;this.fate=r,r.then(i=>{this.pass(r,i)},i=>{this.fail(r,i)})}else this.resolve(n),this.fate=void 0}}const ih=(e,t,n=_e.STORE)=>cc(n,{key:`${e.key}:JSON`,get:({get:r})=>t.toJson(r(e)),set:({set:r},i)=>{r(e,t.fromJson(i))}});function $(e){for(;e.child!==null;)e=e.child;return e}function oh(e){for(;e.parent!==null;)e=e.parent;return e}class Y{constructor(){C(this,"Subscriber");C(this,"subscribers",new Map)}subscribe(t,n){return this.subscribers.set(t,n),()=>{this.unsubscribe(t)}}unsubscribe(t){this.subscribers.delete(t)}next(t){const n=this.subscribers.values();for(const r of n)r(t)}}class sh extends Y{constructor(n){super();C(this,"state");this.state=n}next(n){this.state=n,super.next(n)}}function cl(e,t,n){const r={key:t.key,type:"atom_family"},i=e.families.get(t.key);i&&e.logger.error("❗","atom_family",t.key,`Overwriting an existing ${Fn(i)} "${i.key}" in store "${e.config.name}". You can safely ignore this warning if it is due to hot module replacement.`);const o=new Y,l=Object.assign(u=>{const a=X(u),c={key:t.key,subKey:a},p=`${t.key}(${a})`,d=$(e),m=t.default,w={key:p,default:m instanceof Function?m(u):m};t.effects&&(w.effects=t.effects(u));const S=oo(d,w,c);return o.next({type:"state_creation",token:S}),S},r,{subject:o,install:u=>cl(u,t),internalRoles:n});return e.families.set(t.key,l),e.defaults.set(t.key,t.default),r}function vn(e,t,n){const r={key:t.key,type:"readonly_selector_family"},i=e.families.get(t.key);i&&e.logger.error("❗","readonly_selector_family",t.key,`Overwriting an existing ${Fn(i)} "${i.key}" in store "${e.config.name}". You can safely ignore this warning if it is due to hot module replacement.`);const o=new Y,l=Object.assign(u=>{const a=X(u),c={key:t.key,subKey:a},p=`${t.key}(${a})`,d=$(e),m=gl(d,{key:p,get:t.get(u)},c);return o.next({type:"state_creation",token:m}),m},r,{internalRoles:n,subject:o,install:u=>vn(u,t),default:u=>t.get(u)({get:(...c)=>je(e,...c),find:(c,p)=>Oe(e,c,p),seek:(c,p)=>ke(e,c,p),json:c=>Kt(e,c)})});return e.families.set(t.key,l),r}function lh(e,t){return"set"in t?wl(e,t):vn(e,t)}const uh={atom_family:"atom",mutable_atom_family:"mutable_atom",selector_family:"selector",readonly_selector_family:"readonly_selector",molecule_family:"molecule"};function fl(e,t){const n=X(t),r=`${e.key}(${n})`,i=uh[e.type],o={key:r,type:i};return i==="molecule"?Object.assign(o,{key:t,family:e}):Object.assign(o,{family:{key:e.key,subKey:n}}),Object.assign(o,{counterfeit:!0}),o}function tn(e){const t={key:e.key,type:e.type};return"family"in e&&(t.family=e.family),t}function fn(e){return rp(_e.STORE,e)}const xo=e=>(t,n,r,i,...o)=>{console[e](`${t} ${n} "${r}" ${i}`,...o)},ah={error:xo("error"),info:xo("info"),warn:xo("warn")};class ch{constructor(t,n,r=ah){C(this,"error",(...t)=>{var n;(((n=this.filter)==null?void 0:n.call(this,...t))??!0)&&this.logLevel!==null&&this.logger.error(...t)});C(this,"info",(...t)=>{var n;(((n=this.filter)==null?void 0:n.call(this,...t))??!0)&&this.logLevel==="info"&&this.logger.info(...t)});C(this,"warn",(...t)=>{var n;(((n=this.filter)==null?void 0:n.call(this,...t))??!0)&&this.logLevel!=="error"&&this.logLevel!==null&&this.logger.warn(...t)});this.logLevel=t,this.filter=n,this.logger=r}}function fh(e){return cc(_e.STORE,e)}function dh(e){return lh(_e.STORE,e)}function hh(e){return lc(e,_e.STORE)}function ph(e,t=al()){return ro(e,t,_e.STORE)}class pt{constructor(t,n){C(this,"a");C(this,"b");C(this,"cardinality");C(this,"relations",new Map);C(this,"contents",new Map);C(this,"isContent");C(this,"makeContentKey",(t,n)=>`${t}:${n}`);C(this,"warn");var r;if(this.a=t.between[0],this.b=t.between[1],this.cardinality=t.cardinality,n!=null&&n.externalStore||(this.relations=new Map((r=t.relations)==null?void 0:r.map(([i,o])=>[i,new Set(o)])),this.contents=new Map(t.contents)),this.isContent=(n==null?void 0:n.isContent)??null,n!=null&&n.makeContentKey&&(this.makeContentKey=n.makeContentKey),n!=null&&n.externalStore){const i=n.externalStore;this.has=(o,s)=>i.has(o,s),this.addRelation=(o,s)=>{i.addRelation(o,s)},this.deleteRelation=(o,s)=>{i.deleteRelation(o,s)},this.replaceRelationsSafely=(o,s)=>{i.replaceRelationsSafely(o,s)},this.replaceRelationsUnsafely=(o,s)=>{i.replaceRelationsUnsafely(o,s)},this.getRelatedKeys=o=>i.getRelatedKeys(o),i.getContent&&(this.getContentInternal=o=>i.getContent(o),this.setContent=(o,s)=>{i.setContent(o,s)},this.deleteContent=o=>{i.deleteContent(o)});for(const[o,s]of t.relations??[])for(const l of s)this.addRelation(o,l);for(const[o,s]of t.contents??[])this.setContent(o,s)}n!=null&&n.warn&&(this.warn=n.warn)}getRelatedKeys(t){return this.relations.get(t)}addRelation(t,n){let r=this.relations.get(t),i=this.relations.get(n);r?r.add(n):(r=new Set([n]),this.relations.set(t,r)),i?i.add(t):(i=new Set([t]),this.relations.set(n,i))}deleteRelation(t,n){const r=this.relations.get(t);if(r){r.delete(n),r.size===0&&this.relations.delete(t);const i=this.relations.get(n);i&&(i.delete(t),i.size===0&&this.relations.delete(n))}}replaceRelationsUnsafely(t,n){this.relations.set(t,new Set(n));for(const r of n){const i=new Set([t]);this.relations.set(r,i)}}replaceRelationsSafely(t,n){const r=this.relations.get(t);if(r)for(const i of r){const o=this.relations.get(i);o&&(o.size===1?this.relations.delete(i):o.delete(t),this.contents.delete(this.makeContentKey(t,i)))}this.relations.set(t,new Set(n));for(const i of n){let o=this.relations.get(i);o?o.add(t):(o=new Set([t]),this.relations.set(i,o))}}getContentInternal(t){return this.contents.get(t)}setContent(t,n){this.contents.set(t,n)}deleteContent(t){this.contents.delete(t)}toJSON(){return{between:[this.a,this.b],cardinality:this.cardinality,relations:[...this.relations.entries()].map(([t,n])=>[t,[...n]]),contents:[...this.contents.entries()]}}set(t,...n){const r=typeof n[0]=="string"?n[0]:t[this.b],i=n[1]??typeof n[0]=="string"?void 0:n[0];switch(t=typeof t=="string"?t:t[this.a],this.cardinality){case"1:1":{const o=this.getRelatedKey(t);o&&o!==r&&this.delete(o,t)}case"1:n":{const o=this.getRelatedKey(r);o&&o!==t&&this.delete(o,r)}}if(i){const o=this.makeContentKey(t,r);this.setContent(o,i)}return this.addRelation(t,r),this}delete(t,n){n=typeof n=="string"?n:t[this.b];const r=typeof t=="string"?t:t[this.a];if(r===void 0&&typeof n=="string"){const i=this.getRelatedKeys(n);if(i)for(const o of i)this.delete(o,n)}if(typeof r=="string"&&n===void 0){const i=this.getRelatedKeys(r);if(i)for(const o of i)this.delete(r,o)}if(typeof r=="string"&&typeof n=="string"){this.deleteRelation(r,n);const i=this.makeContentKey(r,n);this.deleteContent(i)}return this}getRelatedKey(t){var r;const n=this.getRelatedKeys(t);if(n){n.size>1&&((r=this.warn)==null||r.call(this,`${n.size} related keys were found for key "${t}": (${[...n].map(i=>`"${i}"`).join(", ")}). Only one related key was expected.`));for(const i of n)return i}}replaceRelations(t,n,r){const i=!Array.isArray(n),o=i?Object.keys(n):n;if(r!=null&&r.reckless?this.replaceRelationsUnsafely(t,o):this.replaceRelationsSafely(t,o),i)for(const s of o){const l=this.makeContentKey(t,s),u=n[s];this.setContent(l,u)}return this}getContent(t,n){const r=this.makeContentKey(t,n);return this.getContentInternal(r)}getRelationEntries(t){const n=t[this.a],r=t[this.b];if(n!==void 0&&r===void 0){const i=this.getRelatedKeys(n);if(i)return[...i].map(o=>[o,this.getContent(n,o)??null])}if(n===void 0&&r!==void 0){const i=this.getRelatedKeys(r);if(i)return[...i].map(o=>[o,this.getContent(o,r)??null])}return[]}has(t,n){if(n){const r=this.getRelatedKeys(t);return(r==null?void 0:r.has(n))??!1}return this.relations.has(t)}}function Je(e){return"epoch"in e.transactionMeta}function ge(e){return"phase"in e.transactionMeta}const yh=e=>{const t=$(e);if(!ge(t)){e.logger.warn("🐞","transaction","???","abortTransaction called outside of a transaction. This is probably a bug in AtomIO.");return}e.logger.info("🪂","transaction",t.transactionMeta.update.key,"Aborting transaction"),t.parent.child=null},mh=e=>e[0].toUpperCase()+e.slice(1);function Fn(e){return e.type.split("_").map(mh).join(" ")}class Ft extends Error{constructor(...t){const n=t[0],r=t.length===2?t[1]:t[2];if(t.length===2)super(`${Fn(n)} ${X(n.key)} not found in store "${r.config.name}".`);else{const i=t[1];super(`${Fn(n)} "${n.key}" member ${X(i)} not found in store "${r.config.name}".`)}}}function ro(e,t,n){return(...r)=>{const i=D(e,n);if(i)return i.run(r,t);throw new Ft(e,n)}}const tc=e=>t=>e instanceof Function?e(t):e,io=(e,t)=>{if(t.valueMap.has(e.key))return t.logger.info("📖",e.type,e.key,"reading cached value"),Gh(e,t);if(e.type==="selector"||e.type==="readonly_selector")return t.logger.info("🧮",e.type,e.key,"computing value"),e.get();const n=e.default instanceof Function?e.default():e.default;return t.logger.info("💁","atom",e.key,"could not find cached value; using default",n),e.default instanceof Function?e.default():e.default},gh=(e,t)=>{if(e.operation.open){const n=performance.now();return e.logger.info("❗",t.type,t.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:t},e.logger.info("⭕",t.type,t.key,`operation start in store "${e.config.name}"${ge(e)?` ${e.transactionMeta.phase} "${e.transactionMeta.update.key}"`:""}`)},vh=e=>{e.operation.open&&e.logger.info("🔴",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)},wh=(e,t)=>e.operation.open?e.operation.done.has(t):(e.logger.error("🐞","unknown",t,"isDone called outside of an operation. This is probably a bug in AtomIO."),!0),dl=(e,t)=>{if(!e.operation.open){e.logger.error("🐞","unknown",t,"markDone called outside of an operation. This is probably a bug in AtomIO.");return}e.operation.done.add(t)},kh=(e,t,n)=>{switch(e.type){case"mutable_atom":n.logger.info("📢",e.type,e.key,"is now (",t.newValue,") subscribers:",e.subject.subscribers);break;default:n.logger.info("📢",e.type,e.key,"went (",t.oldValue,"->",t.newValue,") subscribers:",e.subject.subscribers)}e.subject.next(t)},Eu=(e,t)=>{const n=$(t),r=n.selectorAtoms.getRelatedKeys(e.key);if(n.logger.info("🧹",e.type,e.key,r?`evicting ${r.size} states downstream:`:"no downstream states",r??"to evict"),r){n.operation.open&&n.logger.info("🧹",e.type,e.key,`[ ${[...n.operation.done].join(", ")} ] already done`);for(const i of r)wh(n,i)||(ep(i,n),dl(n,i))}};function Sh(e,t){return!(Zh(t.newValue)||e.includes("🔍"))}const _h=(e,t,n)=>{const{key:r}=e,i=$(n);if(!ge(i)||i.transactionMeta.phase!=="building"){n.logger.error("🐞","atom",r,"stowUpdate called outside of a transaction. This is probably a bug.");return}if(!Sh(r,t))return;const s={type:"atom_update",key:r,...t};e.family&&(s.family=e.family),i.transactionMeta.update.updates.push(s),n.logger.info("📁","atom",r,"stowed (",t.oldValue,"->",t.newValue,")")},Eh=(e,t,n)=>{const r=io(e,n);let i=r;if(e.type==="mutable_atom"&&ge(n)){const{parent:s}=n;i=os(e,s,n)}i=tc(t)(i),n.logger.info("📝","atom",e.key,"set to",i),i=on(e.key,i,e.subject,n),tp(e.key,n)&&np(e.key,n),dl(n,e.key),Eu(e,n);const o={oldValue:r,newValue:i};if(Je(n))kh(e,o,n);else if(n.parent){if(n.on.transactionApplying.state===null)_h(e,o,n);else if(e.key.startsWith("*")){const s=e.key.slice(1),l=n.atoms.get(s);let u=n.valueMap.get(s);if(l.type==="mutable_atom"&&ge(n)){const{parent:c}=n;u=os(l,c,n)}u.do(o.newValue)===null&&Eu(l,n)}}},nc=(e,t,n)=>{switch(e.type){case"atom":case"mutable_atom":Eh(e,t,n);break;case"selector":e.set(t);break}};function rc(e,t){if(t.family){const n=e.families.get(t.family.key);if(n)return n}}function oe(e,...t){let n,r,i,o;if(t.length===2?(n=t[0],o=t[1],r=rc(e,n)??null,r&&(i=n.family?rn(n.family.subKey):null,n=Oe(e,r,i))):(r=t[0],i=t[1],o=t[2],n=Oe(e,r,i)),"counterfeit"in n){const u=e.disposalTraces.buffer.find(a=>(a==null?void 0:a.key)===n.key);e.logger.error("❌",n.type,n.key,`could not be set because it was not found in the store "${e.config.name}".`,u?`This state was previously disposed:
18
18
  ${u.trace}`:"No previous disposal trace was found.");return}const s=gh(e,n);if(s){const u=e.on.operationClose.subscribe(`waiting to set "${n.key}" at T-${s}`,()=>{u(),e.logger.info("🟢",n.type,n.key,`resuming deferred setState from T-${s}`),oe(e,n,o)});return}const l=D(n,e);nc(l,o,e),vh(e)}function Ch(e,t,n){const{key:r,newValue:i,oldValue:o}=t,s=e==="newValue"?i:o,l={key:r,type:"atom"};t.family&&Object.assign(l,{family:t.family}),oe(n,l,s)}function xh(e,t){const n=new Y,r={type:"molecule_family",key:t.key,dependsOn:t.dependsOn??"all"},i={...r,subject:n,new:t.new};return e.moleculeFamilies.set(t.key,i),r}function nn(e,t,n){const r=Bn(n,t,e.key);e.tokens.set(r.key,r);const i=ge(n)&&n.transactionMeta.phase==="building",o=n.moleculeInProgress===e.key;return!i&&!o&&e.subject.next({type:"state_creation",token:r}),r}class lt extends Set{constructor(n,r=0){super(n);C(this,"mode","record");C(this,"subject",new Y);C(this,"cacheLimit",0);C(this,"cache",[]);C(this,"cacheIdx",-1);C(this,"cacheUpdateNumber",-1);C(this,"parent");C(this,"child",null);C(this,"transactionUpdates",null);n instanceof lt&&(this.parent=n,this.cacheUpdateNumber=n.cacheUpdateNumber),r&&(this.cacheLimit=r,this.cache=new Array(r),this.subscribe("auto cache",i=>{this.cacheIdx++,this.cacheIdx%=this.cacheLimit,this.cache[this.cacheIdx]=i}))}toJSON(){return{members:[...this],cache:this.cache,cacheLimit:this.cacheLimit,cacheIdx:this.cacheIdx,cacheUpdateNumber:this.cacheUpdateNumber}}static fromJSON(n){const r=new lt(n.members,n.cacheLimit);return r.cache=n.cache,r.cacheIdx=n.cacheIdx,r.cacheUpdateNumber=n.cacheUpdateNumber,r}add(n){const r=super.add(n);return this.mode==="record"&&(this.cacheUpdateNumber++,this.emit(`add:${X(n)}`)),r}clear(){const n=this.mode==="record"?[...this]:null;super.clear(),n&&(this.cacheUpdateNumber++,this.emit(`clear:${JSON.stringify(n)}`))}delete(n){const r=super.delete(n);return this.mode==="record"&&(this.cacheUpdateNumber++,this.emit(`del:${X(n)}`)),r}transaction(n){this.mode="transaction",this.transactionUpdates=[],this.child=new lt(this);const r=this.child._subscribe("transaction",i=>{var o;(o=this.transactionUpdates)==null||o.push(i)});try{if(n(this.child)){for(const o of this.transactionUpdates)this.doStep(o);this.cacheUpdateNumber++,this.emit(`tx:${this.transactionUpdates.join(";")}`)}}catch(i){throw console.error("Failed to apply transaction to SetRTX:",i),i}finally{r(),this.child=null,this.transactionUpdates=null,this.mode="record"}}_subscribe(n,r){return this.subject.subscribe(n,r)}subscribe(n,r){return this.subject.subscribe(n,i=>{r(`${this.cacheUpdateNumber}=${i}`)})}emit(n){this.subject.next(n)}doStep(n){const r=n.indexOf(":"),i=n.substring(0,r),o=n.substring(r+1);switch(i){case"add":this.add(JSON.parse(o));break;case"clear":this.clear();break;case"del":this.delete(JSON.parse(o));break;case"tx":for(const s of o.split(";"))this.doStep(s)}}getUpdateNumber(n){const r=n.indexOf("=");return Number(n.substring(0,r))}do(n){const r=n.indexOf("="),i=Number(n.substring(0,r)),o=i-this.cacheUpdateNumber;if(o>0){if(o===1){this.mode="playback";const l=n.substring(r+1);return this.doStep(l),this.mode="record",this.cacheUpdateNumber=i,null}return this.cacheUpdateNumber+1}if(Math.abs(o)<this.cacheLimit){const l=this.cacheIdx+o;if(this.cache[l]===n)return null;this.mode="playback";let a=!1;for(;!a;){this.cacheIdx%=this.cacheLimit;const p=this.cache[this.cacheIdx];if(this.cacheIdx--,!p)return"OUT_OF_RANGE";this.undo(p),a=this.cacheIdx===l-1}const c=n.substring(r+1);return this.doStep(c),this.mode="record",this.cacheUpdateNumber=i,null}return"OUT_OF_RANGE"}undoStep(n){const r=n.indexOf(":"),i=n.substring(0,r),o=n.substring(r+1);switch(i){case"add":this.delete(JSON.parse(o));break;case"del":this.add(JSON.parse(o));break;case"clear":{const s=JSON.parse(o);for(const l of s)this.add(l);break}case"tx":{const s=o.split(";");for(let l=s.length-1;l>=0;l--)this.undoStep(s[l])}}}undo(n){const r=n.indexOf("=");if(Number(n.substring(0,r))===this.cacheUpdateNumber){this.mode="playback";const o=n.substring(r+1);return this.undoStep(o),this.mode="record",this.cacheUpdateNumber--,null}return this.cacheUpdateNumber}}function H(e){return e[0].toUpperCase()+e.slice(1)}class hl{constructor(t,n,r=_e.STORE){C(this,"options");C(this,"defaultContent");C(this,"toolkit");C(this,"retrieve");C(this,"molecules",new Map);C(this,"relations");C(this,"states");C(this,"core");C(this,"store");C(this,"alternates");this.store=r,this.options=t,this.defaultContent=n,this.alternates=new Map,this.alternates.set(r.config.name,this),this.store.miscResources.set(`join:${t.key}`,this),this.retrieve=(g,v)=>{const _=this.toolkit.seek(g,v);if(_)return _;const M=this.molecules.get(X(v));if(M){const x=D(g,r);return nn(M,x,r)}if(r.config.lifespan==="immortal")throw new Ft(g,v,r);return Bn(r,g,v)},this.toolkit={get:(...g)=>je(r,...g),set:(...g)=>{oe(r,...g)},find:this.retrieve,seek:(...g)=>ke(r,...g),json:g=>Kt(r,g),dispose:(...g)=>{Ut(r,...g)}};const i=t.between[0],o=t.between[1],s=fc(r,{key:`${t.key}/relatedKeys`,default:()=>new lt,mutable:!0,fromJson:g=>lt.fromJSON(g),toJson:g=>g.toJSON()},["join","relations"]);this.core={findRelatedKeysState:s};const l=({get:g},v)=>g(this.retrieve(s,v)),u=(g,v,_)=>{const{set:M}=g,x=this.retrieve(s,v),A=this.retrieve(s,_);M(x,I=>I.add(_)),M(A,I=>I.add(v))},a=(g,v,_)=>{const{set:M}=g,x=this.retrieve(s,v),A=this.retrieve(s,_);M(x,I=>(I.delete(_),I.size,I)),M(A,I=>(I.delete(v),I.size,I))},c=(g,v,_)=>{const{get:M,set:x}=g,A=this.retrieve(s,v),I=M(A);for(const U of I){if(_.includes(U))continue;const nt=this.retrieve(s,U);x(nt,Re=>(Re.delete(v),Re))}x(A,U=>(U.transaction(xe=>{xe.clear();for(const nt of _){const Re=l(g,nt),pn=Re.has(v);if(this.relations.cardinality==="1:n"){const N=[];for(const P of Re){if(P===v)continue;const L=l(g,P);L.delete(nt),L.size===0&&N.push(P)}!pn&&Re.size>0&&Re.clear();for(const P of N){const L=[nt,P].sort(),Q=`"${L[0]}:${L[1]}"`;this.molecules.delete(Q)}}pn||Re.add(v),xe.add(nt)}return!0}),U))},p=(g,v,_)=>{const{set:M}=g,x=this.retrieve(s,v);M(x,A=>(A.transaction(I=>{for(const U of _)I.add(U);return!0}),A));for(const A of _){const I=this.retrieve(s,A);M(I,U=>(U.add(v),U))}return!0},d=(g,v,_)=>{const M=l(g,v);return _?M.has(_):M.size>0},m={getRelatedKeys:g=>l(this.toolkit,g),addRelation:(g,v)=>{u(this.toolkit,g,v)},deleteRelation:(g,v)=>{a(this.toolkit,g,v)},replaceRelationsSafely:(g,v)=>{c(this.toolkit,g,v)},replaceRelationsUnsafely:(g,v)=>{p(this.toolkit,g,v)},has:(g,v)=>d(this.toolkit,g,v)};let w,S,R;if(n){S=cl(r,{key:`${t.key}/content`,default:n},["join","content"]),t.key,t.between[0],t.between[1],t.cardinality,R=xh(r,{key:`${t.key}/content-molecules`,new:class{constructor(A,I){this.key=I}}});const g=({get:x},A)=>x(this.retrieve(S,A)),v=({set:x},A,I)=>{x(this.retrieve(S,A),I)},_=(x,A)=>{const I=r.molecules.get(`"${A}"`);I&&(this.toolkit.dispose(I),this.molecules.delete(`"${A}"`))};w=Object.assign(m,{getContent:x=>g(this.toolkit,x),setContent:(x,A)=>{v(this.toolkit,x,A)},deleteContent:x=>{_(this.toolkit,x)}})}else w=m;const h=new pt(t,{externalStore:w,makeContentKey:(...g)=>{const v=g.sort(),_=`${v[0]}:${v[1]}`,[M,x]=v.map(A=>this.molecules.get(X(A)));if(r.config.lifespan==="immortal"&&M&&x){const A=$(r),I=Dn(A,[M,x],R,_);this.molecules.set(`"${_}"`,D(I,A))}return _}}),f=()=>vn(r,{key:`${t.key}/singleRelatedKey`,get:g=>({get:v})=>{const _=this.retrieve(s,g),M=v(_);for(const x of M)return x;return null}},["join","keys"]),y=()=>vn(r,{key:`${t.key}/multipleRelatedKeys`,get:g=>({get:v})=>{const _=Tu(s,r),M=this.retrieve(_,g);return v(M).members}},["join","keys"]),k=()=>vn(r,{key:`${t.key}/singleRelatedEntry`,get:g=>({get:v})=>{const _=this.retrieve(s,g),M=v(_);for(const x of M){const A=h.makeContentKey(g,x),I=this.retrieve(S,A),U=v(I);return[x,U]}return null}},["join","entries"]),T=()=>vn(r,{key:`${t.key}/multipleRelatedEntries`,get:g=>({get:v})=>{const _=Tu(s,r),M=this.retrieve(_,g);return v(M).members.map(A=>{const I=h.makeContentKey(g,A),U=this.retrieve(S,I),xe=v(U);return[A,xe]})}},["join","entries"]);switch(t.cardinality){case"1:1":{const g=f(),v=`${i}KeyOf${H(o)}`,_=`${o}KeyOf${H(i)}`,M={[v]:g,[_]:g};let x;if(n){const A=k(),I=`${i}EntryOf${H(o)}`,U=`${o}EntryOf${H(i)}`,xe={[I]:A,[U]:A};x=Object.assign(M,xe)}else x=M;this.relations=h,this.states=x;break}case"1:n":{const g=f(),v=y(),_=`${i}KeyOf${H(o)}`,M=`${o}KeysOf${H(i)}`,x={[_]:g,[M]:v};let A;if(n){const I=k(),U=T(),xe=`${i}EntryOf${H(o)}`,nt=`${o}EntriesOf${H(i)}`,Re={[xe]:I,[nt]:U};A=Object.assign(x,Re)}else A=x;this.relations=h,this.states=A;break}default:{const g=y(),v=`${i}KeysOf${H(o)}`,_=`${o}KeysOf${H(i)}`,M={[v]:g,[_]:g};let x;if(n){const A=T(),I=`${i}EntriesOf${H(o)}`,U=`${o}EntriesOf${H(i)}`,xe={[I]:A,[U]:A};x=Object.assign(M,xe)}else x=M;this.relations=h,this.states=x}}}transact(t,n){const r=this.toolkit;this.toolkit=t,n(this),this.toolkit=r}[Symbol.dispose](){this.alternates.delete(this.store.config.name)}in(t){const n=t.config.name,r=this.alternates.get(n);if(r)return r;const i=new hl(this.options,this.defaultContent,t);return this.alternates.set(n,i),i.alternates=this.alternates,i}}function Rh(e,t,n=_e.STORE){return ts(n).set(e.key,new hl(e,t,n)),{key:e.key,type:"join",a:e.between[0],b:e.between[1],cardinality:e.cardinality}}function ts(e){if("joins"in e&&e.joins instanceof Map)return e.joins;const t=new Map;return e.joins=t,t}function pl(e,t){var i;const n=ts(t);let r=n.get(e.key);if(r===void 0){if(r=(i=ts(_e.STORE).get(e.key))==null?void 0:i.in(t),r===void 0)throw new Error(`Join "${e.key}" not found in store "${t.config.name}"`);n.set(e.key,r)}return r}function Th(e,t,n){const r=pl(e,n);let i;switch(e.cardinality){case"1:1":{const o=`${e.a}KeyOf${H(e.b)}`,s=`${e.b}KeyOf${H(e.a)}`;i={get[o](){const u=r.states[o];return r.retrieve(u,t)},get[s](){const u=r.states[s];return r.retrieve(u,t)}};const l=`${e.a}EntryOf${H(e.b)}`;if(l in r.states){const u=`${e.b}EntryOf${H(e.a)}`;Object.assign(i,{get[l](){const a=r.states[l];return r.retrieve(a,t)},get[u](){const a=r.states[u];return r.retrieve(a,t)}})}break}case"1:n":{const o=`${e.a}KeyOf${H(e.b)}`,s=`${e.b}KeysOf${H(e.a)}`;i={get[o](){const u=r.states[o];return r.retrieve(u,t)},get[s](){const u=r.states[s];return r.retrieve(u,t)}};const l=`${e.a}EntryOf${H(e.b)}`;if(l in r.states){const u=`${e.b}EntriesOf${H(e.a)}`;Object.assign(i,{get[l](){const a=r.states[l];return r.retrieve(a,t)},get[u](){const a=r.states[u];return r.retrieve(a,t)}})}break}case"n:n":{const o=`${e.a}KeysOf${H(e.b)}`,s=`${e.b}KeysOf${H(e.a)}`;i={get[o](){const u=r.states[o];return r.retrieve(u,t)},get[s](){const u=r.states[s];return r.retrieve(u,t)}};const l=`${e.a}EntriesOf${H(e.b)}`;if(l in r.states){const u=`${e.b}EntriesOf${H(e.a)}`;Object.assign(i,{get[l](){const a=r.states[l];return r.retrieve(a,t)},get[u](){const a=r.states[u];return r.retrieve(a,t)}})}}}return i}function Nh(e,t){return Th(e,t,_e.STORE)}function Oh(e,t){return pl(e,t).core.findRelatedKeysState}function Mh(e){return Oh(e,_e.STORE)}function ic(e){return{store:e}}function je(e,...t){let n,r,i;if(t.length===1)n=t[0],n.type!=="molecule"&&(r=rc(e,n)??null,r&&(i=n.family?rn(n.family.subKey):null,n=Oe(e,r,i)));else{r=t[0],i=t[1];let o;r.type==="molecule_family"?o=ke(e,r,i)??fl(r,i):o=Oe(e,r,i),n=o}if("counterfeit"in n&&"family"in n){r=n.type==="molecule"?D(n.family,e):e.families.get(n.family.key);const o=n.type==="molecule"?n.key:n.family.subKey,s=e.disposalTraces.buffer.find(l=>(l==null?void 0:l.key)===n.key);switch(e.logger.error("❌",n.type,n.key,`could not be retrieved because it was not found in the store "${e.config.name}".`,s?`This state was previously disposed:
19
19
  ${s.trace}`:"No previous disposal trace was found."),r.type){case"atom_family":case"mutable_atom_family":return e.defaults.get(r.key);case"selector_family":case"readonly_selector_family":{if(e.defaults.has(r.key))return e.defaults.get(n.family.key);const l=D(r,e).default(o);return e.defaults.set(r.key,l),l}case"molecule_family":throw new Ft(r,o,e)}}switch(n.type){case"atom":case"mutable_atom":case"selector":case"readonly_selector":return io(D(n,e),e);case"molecule":return D(n,e).instance}}class Cu{constructor(t,n,r){C(this,"type","molecule");C(this,"stringKey");C(this,"family");C(this,"subject",new Y);C(this,"tokens",new Map);C(this,"above",new Map);C(this,"below",new Map);C(this,"joins",new Map);C(this,"instance");if(this.key=n,this.stringKey=X(n),r&&(this.family=r),t)if(Array.isArray(t))for(const i of t)this.above.set(i.stringKey,i);else this.above.set(t.stringKey,t)}}function xu(e){return e[0].toUpperCase()+e.slice(1)}function Dn(e,t,n,r,...i){const o=oh(e),s=$(e),l=X(r);s.moleculeInProgress=l;const u=Array.isArray(t)?t:[t],a=u.map(R=>{if(R instanceof Cu)return R;const h=X(R.key),f=e.molecules.get(h);if(!f)throw new Error(`Molecule ${h} not found in store "${e.config.name}"`);return f}),c=new Cu(a,r,n);s.molecules.set(l,c);for(const R of a)R.below.set(c.stringKey,c);const p={get:(...R)=>je($(o),...R),set:(...R)=>{oe($(o),...R)},seek:(R,h)=>ke($(o),R,h),json:R=>Kt($(o),R),run:(R,h=al())=>ro(R,h,$(e)),make:(R,h,f,...y)=>Dn($(o),R,h,f,...y),dispose:R=>{Ut($(o),R)},env:()=>ic($(o)),bond:(R,h)=>{if(R.type==="join"){const{as:f}=h,y=pl(R,o);y.molecules.set(l,c),c.joins.set(R.key,y);const k=d.subject.subscribe(`join:${R.key}-${l}`,x=>{x.type==="molecule_disposal"&&X(x.token.key)===l&&(k(),y.molecules.delete(l))});if(f===null)return;const T=R.a===f?R.b:R.a,g=Nh(R,r),v=g[`${T}KeysOf${xu(f)}`],_=g[`${T}EntriesOf${xu(f)}`];let M={relatedKeys:v};return _&&(M=Object.assign(M,{relatedEntries:_})),M}return nn(c,D(R,o),$(o))},claim:(R,h)=>{const{exclusive:f}=h,y=$(e).molecules.get(X(R.key));if(y)if(f){for(const k of y.above.values())k.below.delete(y.stringKey);y.above.clear(),y.above.set(c.stringKey,c),c.below.set(y.stringKey,y)}else y.above.set(c.stringKey,c),c.below.set(y.stringKey,y)},spawn:(R,h,...f)=>Dn($(e),[c],D(R,e),h,...f)},d=D(n,e),m=d.new;c.instance=new m(p,r,...i);const w={type:"molecule",key:r,family:n},S={type:"molecule_creation",token:w,family:n,context:u,params:i};return Je(s)?d.subject.next(S):ge(s)&&s.on.transactionApplying.state===null&&s.transactionMeta.update.updates.push(S),s.moleculeInProgress=null,w}function Ah(e,t,n){switch(t){case"newValue":{oc(e,n);break}case"oldValue":{Ut(n,e.token);break}}}function $h(e,t,n){switch(t){case"newValue":{Ut(n,e.token);break}case"oldValue":{oc(e,n),n.valueMap.set(e.token.key,e.value);break}}}function oc(e,t){const{family:n}=e.token;if(n){const r=t.families.get(n.key);r&&Oe(t,r,rn(n.subKey))}}function Ph(e,t,n){switch(t){case"newValue":Dn(n,e.context,e.family,e.token.key,...e.params);break;case"oldValue":Ut(n,e.token);break}}function Lh(e,t,n){switch(t){case"newValue":Ut(n,e.token);break;case"oldValue":{const r=Dn(n,e.context,e.family,e.token.key);for(const[i,o]of e.values){const s=`${i}(${X(r.key)})`,l=D(r,n),u=l.tokens.has(s),a=n.families.get(i);a&&!u&&nn(l,a,n),n.valueMap.set(s,o)}}break}}function wn(e,t,n){const r=e==="newValue"?t.updates:[...t.updates].reverse();for(const i of r)switch(i.type){case"atom_update":case"selector_update":Ch(e,i,n);break;case"state_creation":Ah(i,e,n);break;case"state_disposal":$h(i,e,n);break;case"molecule_creation":Ph(i,e,n);break;case"molecule_disposal":Lh(i,e,n);break;case"transaction_update":wn(e,i,n);break}}function ns(e,t,n){Je(n)&&e&&n.transactionMeta.epoch.set(e,t)}function Ih(e,t,n){if(!Je(n))return;const i=n.transactionMeta.actionContinuities.getRelatedKey(e);i!==void 0&&n.transactionMeta.epoch.set(i,t)}const jh=(e,t)=>{var o;const n=$(t),{parent:r}=n;if(r===null||!ge(n)||((o=n.transactionMeta)==null?void 0:o.phase)!=="building"){t.logger.warn("🐞","transaction","???","applyTransaction called outside of a transaction. This is probably a bug in AtomIO.");return}n.transactionMeta.phase="applying",n.transactionMeta.update.output=e,r.child=null,r.on.transactionApplying.next(n.transactionMeta);const{updates:i}=n.transactionMeta.update;if(t.logger.info("🛄","transaction",n.transactionMeta.update.key,`Applying transaction with ${i.length} updates:`,i),wn("newValue",n.transactionMeta.update,r),Je(r)){Ih(n.transactionMeta.update.key,n.transactionMeta.update.epoch,r);const s=D({key:n.transactionMeta.update.key,type:"transaction"},t);s==null||s.subject.next(n.transactionMeta.update),t.logger.info("🛬","transaction",n.transactionMeta.update.key,"Finished applying transaction.")}else ge(r)&&r.transactionMeta.update.updates.push(n.transactionMeta.update);r.on.transactionApplying.next(null)};function sc(e,t,n){if(!Je(n))return;const{epoch:i,actionContinuities:o}=n.transactionMeta;o.set(e,t),i.has(e)||i.set(e,-1)}class rt extends Map{constructor(n){super();C(this,"deleted",new Set);this.source=n}get(n){if(super.has(n))return super.get(n);if(!this.deleted.has(n)&&this.source.has(n))return this.source.get(n)}set(n,r){return this.deleted.delete(n),super.set(n,r)}hasOwn(n){return super.has(n)}has(n){return!this.deleted.has(n)&&(super.has(n)||this.source.has(n))}delete(n){return this.deleted.add(n),super.delete(n)}}const zh=(e,t,n,r)=>{const i=$(n),o={parent:i,child:null,on:i.on,loggers:i.loggers,logger:i.logger,config:i.config,atoms:new rt(i.atoms),atomsThatAreDefault:new Set(i.atomsThatAreDefault),families:new rt(i.families),operation:{open:!1},readonlySelectors:new rt(i.readonlySelectors),timelines:new rt(i.timelines),timelineTopics:new pt(i.timelineTopics.toJSON()),trackers:new Map,transactions:new rt(i.transactions),selectorAtoms:new pt(i.selectorAtoms.toJSON()),selectorGraph:new pt(i.selectorGraph.toJSON(),{makeContentKey:(...a)=>a.sort().join(":")}),selectors:new rt(i.selectors),valueMap:new rt(i.valueMap),defaults:i.defaults,disposalTraces:n.disposalTraces.copy(),molecules:new rt(i.molecules),moleculeFamilies:new rt(i.moleculeFamilies),moleculeInProgress:i.moleculeInProgress,miscResources:new rt(i.miscResources)},s=Dh(e,n),l={phase:"building",update:{type:"transaction_update",key:e,id:r,epoch:s===void 0?Number.NaN:s+1,updates:[],params:t,output:void 0},toolkit:{get:(...a)=>je(u,...a),set:(...a)=>{oe(u,...a)},run:(a,c=al())=>ro(a,c,u),find:(a,c)=>Oe(u,a,c),seek:(a,c)=>ke(u,a,c),json:a=>Kt(u,a),make:(a,c,p,...d)=>Dn(u,a,c,p,...d),dispose:(...a)=>{Ut(u,...a)},env:()=>ic(u)}},u=Object.assign(o,{transactionMeta:l});return i.child=u,n.logger.info("🛫","transaction",e,"Building transaction with params:",t),u};function lc(e,t){const n={key:e.key,type:"transaction",run:(o,s)=>{const l=zh(e.key,o,t,s);try{const u=$(t),{toolkit:a}=l.transactionMeta,c=e.do(a,...o);return jh(c,u),c}catch(u){throw yh(r),t.logger.warn("💥","transaction",e.key,"caught:",u),u}},install:o=>lc(e,o),subject:new Y},r=$(t);r.transactions.set(n.key,n);const i=tn(n);return t.on.transactionCreation.next(i),i}function Fh(e,t){return Je(t)&&e?t.transactionMeta.epoch.get(e):void 0}function Dh(e,t){const n=Je(t),r=n?t.transactionMeta.actionContinuities.getRelatedKey(e):void 0;return n&&r!==void 0?t.transactionMeta.epoch.get(r):void 0}class yl{constructor(t){C(this,"_buffer");C(this,"_index",0);let n;typeof t=="number"?n=t:n=t.length,this._buffer=Array.from({length:n})}get buffer(){return this._buffer}get index(){return this._index}add(t){this._buffer[this._index]=t,this._index=(this._index+1)%this._buffer.length}copy(){const t=new yl([...this._buffer]);return t._index=this._index,t}}class Bh{constructor(t,n=null){C(this,"parent",null);C(this,"child",null);C(this,"valueMap",new Map);C(this,"defaults",new Map);C(this,"atoms",new Map);C(this,"selectors",new Map);C(this,"readonlySelectors",new Map);C(this,"atomsThatAreDefault",new Set);C(this,"selectorAtoms",new pt({between:["selectorKey","atomKey"],cardinality:"n:n"}));C(this,"selectorGraph",new pt({between:["upstreamSelectorKey","downstreamSelectorKey"],cardinality:"n:n"},{makeContentKey:(...t)=>t.sort().join(":")}));C(this,"trackers",new Map);C(this,"families",new Map);C(this,"transactions",new Map);C(this,"transactionMeta",{epoch:new Map,actionContinuities:new pt({between:["continuity","action"],cardinality:"1:n"})});C(this,"timelines",new Map);C(this,"timelineTopics",new pt({between:["timelineKey","topicKey"],cardinality:"1:n"}));C(this,"disposalTraces",new yl(100));C(this,"molecules",new Map);C(this,"moleculeFamilies",new Map);C(this,"moleculeInProgress",null);C(this,"miscResources",new Map);C(this,"on",{atomCreation:new Y,atomDisposal:new Y,selectorCreation:new Y,selectorDisposal:new Y,timelineCreation:new Y,transactionCreation:new Y,transactionApplying:new sh(null),operationClose:new Y,moleculeCreationStart:new Y,moleculeCreationDone:new Y,moleculeDisposal:new Y});C(this,"operation",{open:!1});C(this,"config",{name:"IMPLICIT_STORE",lifespan:"ephemeral"});C(this,"loggers",[new ch("warn",(t,n,r)=>!r.includes("🔍"))]);C(this,"logger",{error:(...t)=>{for(const n of this.loggers)n.error(...t)},info:(...t)=>{for(const n of this.loggers)n.info(...t)},warn:(...t)=>{for(const n of this.loggers)n.warn(...t)}});var r,i;if(this.config={...n==null?void 0:n.config,...t},n!==null){this.valueMap=new Map(n==null?void 0:n.valueMap),this.operation={...n==null?void 0:n.operation},Je(n)&&(this.transactionMeta={epoch:new Map(n==null?void 0:n.transactionMeta.epoch),actionContinuities:new pt(n==null?void 0:n.transactionMeta.actionContinuities.toJSON())});for(const[,s]of n.families)(r=s.internalRoles)!=null&&r.includes("mutable")||(i=s.internalRoles)!=null&&i.includes("join")||s.install(this);const o=new Set;for(const[,s]of n.atoms)if(!o.has(s.key)&&(s.install(this),s.type==="mutable_atom")){const l=Kt(n,s),u=dc(s);o.add(l.key),o.add(u.key)}for(const[,s]of n.readonlySelectors)s.install(this);for(const[,s]of n.selectors)o.has(s.key)||s.install(this);for(const[,s]of n.transactions)s.install(this);for(const[,s]of n.timelines)s.install(this)}}}const _e={get STORE(){return globalThis.ATOM_IO_IMPLICIT_STORE||(globalThis.ATOM_IO_IMPLICIT_STORE=new Bh({name:"IMPLICIT_STORE",lifespan:"ephemeral"})),globalThis.ATOM_IO_IMPLICIT_STORE}};function D(e,t){let n,r=t;for(;r!==null;){switch(e.type){case"atom":case"mutable_atom":n=r.atoms.get(e.key);break;case"selector":n=r.selectors.get(e.key);break;case"readonly_selector":n=r.readonlySelectors.get(e.key);break;case"atom_family":case"mutable_atom_family":case"selector_family":case"readonly_selector_family":n=r.families.get(e.key);break;case"timeline":n=r.timelines.get(e.key);break;case"transaction":n=r.transactions.get(e.key);break;case"molecule":n=r.molecules.get(X(e.key));break;case"molecule_family":n=r.moleculeFamilies.get(e.key);break}if(n)return n;r=r.child}throw new Ft(e,t)}function rs(e,t){var s;let n;n=D(e,t);const{family:r}=e;for(const l of n.joins.values())l.relations.delete(n.key),l.molecules.delete(n.stringKey);const i=[];for(const l of n.above.values())i.push(tn(l));const o=[];for(const l of n.tokens.values()){const u=l.family;o.push([u.key,t.valueMap.get(l.key)])}if(r){const l=D(r,t),u={type:"molecule_disposal",token:e,family:r,context:i,values:o};e.family&&(u.family=e.family);for(const c of n.tokens.values())Ut(t,c);for(const c of n.below.values())((s=c.family)==null?void 0:s.dependsOn)==="all"?rs(c,t):(c.above.delete(n.stringKey),c.above.size===0&&rs(c,t));n.below.clear(),ge(t)&&t.transactionMeta.phase==="building"?t.transactionMeta.update.updates.push(u):l.subject.next(u),t.molecules.delete(n.stringKey)}for(const l of n.above.values())l.below.delete(n.stringKey)}function Bn(e,t,n){const r=e.families.get(t.key);if(r===void 0)throw new Ft(t,e);const i=r(n),o=$(e);if(i.family&&o.moleculeInProgress===null)if(Je(o))switch(i.type){case"atom":case"mutable_atom":e.on.atomCreation.next(i);break;case"selector":case"readonly_selector":e.on.selectorCreation.next(i);break}else ge(o)&&o.on.transactionApplying.state===null&&o.transactionMeta.update.updates.push({type:"state_creation",token:i});return i}function ke(e,t,n){const r=X(n),i=`${t.key}(${r})`,o=$(e);let s;switch(t.type){case"atom_family":case"mutable_atom_family":s=o.atoms.get(i);break;case"selector_family":s=o.selectors.get(i);break;case"readonly_selector_family":s=o.readonlySelectors.get(i);break;case"molecule_family":s=o.molecules.get(X(n))}return s&&tn(s)}function Oe(e,t,n){let r=ke(e,t,n);if(r)return r;const i=e.molecules.get(X(n));if(i)return nn(i,t,e);if(e.config.lifespan==="immortal"){const o=fl(t,n);return e.logger.error("❌",o.type,o.key,`was not found in store "${e.config.name}"; returned a counterfeit token.`),o}return r=Bn(e,t,n),r}function Ut(e,...t){var i,o;let n;if(t.length===1)n=t[0],n.key;else{const s=t[0],l=t[1];n=s.type==="molecule_family"?ke(e,s,l)??fl(s,l):Oe(e,s,l)}try{D(n,e)}catch{const l=e.disposalTraces.buffer.find(u=>(u==null?void 0:u.key)===n.key);e.logger.error("❌",n.type,n.key,`could not be disposed because it was not found in the store "${e.config.name}".`,l?`
20
20
  This state was most recently disposed
package/app/index.html CHANGED
@@ -5,7 +5,7 @@
5
5
  <link rel="icon" type="image/svg+xml" href="/vite.svg" />
6
6
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
7
7
  <title>Vite + React + TS</title>
8
- <script type="module" crossorigin src="/assets/index-CmKKhrTw.js"></script>
8
+ <script type="module" crossorigin src="/assets/index-lvPFnDlJ.js"></script>
9
9
  <link rel="stylesheet" crossorigin href="/assets/index-BLiTugnk.css">
10
10
  </head>
11
11
  <body>