@javelin/ecs
Advanced tools
Comparing version 1.0.0-alpha.6 to 1.0.0-alpha.7
@@ -6,2 +6,13 @@ # Change Log | ||
# [1.0.0-alpha.7](https://github.com/3mcd/javelin/compare/v1.0.0-alpha.6...v1.0.0-alpha.7) (2021-06-09) | ||
### Bug Fixes | ||
* ignore transient entities in monitor ([db43078](https://github.com/3mcd/javelin/commit/db4307852df3852eed415a4265f40020322b9a15)) | ||
# [1.0.0-alpha.6](https://github.com/3mcd/javelin/compare/v1.0.0-alpha.5...v1.0.0-alpha.6) (2021-06-04) | ||
@@ -8,0 +19,0 @@ |
@@ -7,2 +7,3 @@ import { EffectOptions } from "./effect"; | ||
}, []>; | ||
export declare function createImmutableRef<T>(initializer: RefInitializer<T>, options?: EffectOptions): import("./effect").EffectApi<T, []>; | ||
//# sourceMappingURL=effect_utils.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createRef = void 0; | ||
exports.createImmutableRef = exports.createRef = void 0; | ||
const effect_1 = require("./effect"); | ||
@@ -11,2 +11,9 @@ const use_ref_1 = require("./effects/core/use_ref"); | ||
exports.createRef = createRef; | ||
function createImmutableRef(initializer, options = {}) { | ||
return effect_1.createEffect(world => { | ||
const initialValue = initializer(world); | ||
return () => use_ref_1.useRef(initialValue).value; | ||
}, options); | ||
} | ||
exports.createImmutableRef = createImmutableRef; | ||
//# sourceMappingURL=effect_utils.js.map |
@@ -46,2 +46,3 @@ "use strict"; | ||
const { storage: { entityRelocating, entityRelocated, archetypes: [rootArchetype], }, } = world; | ||
const matched = new Set(); | ||
let stagedEnter = []; | ||
@@ -70,27 +71,28 @@ let stagedExit = []; | ||
entityRelocating.subscribe(function detectMonitorExit(entity, prev, next, diff) { | ||
if (_query === null) { | ||
if (_query === null) | ||
return; | ||
} | ||
const matchExit = _query.matchesArchetype(prev); | ||
// entity matched previously and was destroyed | ||
if (matchExit && next === rootArchetype) { | ||
const matchCurr = matched.has(entity); | ||
const matchPrev = _query.matchesArchetype(prev); | ||
const matchNext = _query.matchesArchetype(next); | ||
const isExit = matchPrev && (!matchNext || next === rootArchetype); | ||
if (!isExit) | ||
return; | ||
if (matchCurr) { | ||
const index = stagedEnter.findIndex(([e]) => e === entity); | ||
// entity matched and unmatched during the same tick | ||
if (index !== -1) { | ||
stagedEnter.splice(index, 1); | ||
} | ||
core_1.assert(index !== -1); | ||
core_1.mutableRemoveByIndexUnordered(stagedEnter, index); | ||
return; | ||
} | ||
if (matchExit && !_query.matchesArchetype(next)) { | ||
const snapshot = snapshots.retain(); | ||
snapshot[0] = entity; | ||
_query.get(entity, snapshot[1]); | ||
_query.match(diff, snapshot[2]); | ||
stagedExit.push(snapshot); | ||
} | ||
const snapshot = snapshots.retain(); | ||
snapshot[0] = entity; | ||
_query.get(entity, snapshot[1]); | ||
_query.match(diff, snapshot[2]); | ||
stagedExit.push(snapshot); | ||
}); | ||
entityRelocated.subscribe(function detectMonitorEnter(entity, prev, next, diff) { | ||
if (_query === null) { | ||
if (_query === null) | ||
return; | ||
} | ||
if (!_query.matchesArchetype(prev) && _query.matchesArchetype(next)) { | ||
const matchPrev = _query.matchesArchetype(prev); | ||
const matchNext = _query.matchesArchetype(next); | ||
if (!matchPrev && matchNext) { | ||
const snapshot = snapshots.retain(); | ||
@@ -101,2 +103,3 @@ snapshot[0] = entity; | ||
stagedEnter.push(snapshot); | ||
matched.add(entity); | ||
} | ||
@@ -117,2 +120,3 @@ }); | ||
} | ||
matched.clear(); | ||
if (onEnter !== undefined) { | ||
@@ -119,0 +123,0 @@ for (let i = 0; i < readyEnter.length; i++) { |
@@ -7,2 +7,3 @@ import { EffectOptions } from "./effect"; | ||
}, []>; | ||
export declare function createImmutableRef<T>(initializer: RefInitializer<T>, options?: EffectOptions): import("./effect").EffectApi<T, []>; | ||
//# sourceMappingURL=effect_utils.d.ts.map |
@@ -7,2 +7,8 @@ import { createEffect } from "./effect"; | ||
}, options); | ||
export function createImmutableRef(initializer, options = {}) { | ||
return createEffect(world => { | ||
const initialValue = initializer(world); | ||
return () => useRef(initialValue).value; | ||
}, options); | ||
} | ||
//# sourceMappingURL=effect_utils.js.map |
@@ -1,2 +0,2 @@ | ||
import { createStackPool, mutableEmpty } from "@javelin/core"; | ||
import { assert, createStackPool, mutableEmpty, mutableRemoveByIndexUnordered, } from "@javelin/core"; | ||
import { createEffect } from "../../effect"; | ||
@@ -43,2 +43,3 @@ const snapshots = createStackPool(() => [-1, [], []], c => { | ||
const { storage: { entityRelocating, entityRelocated, archetypes: [rootArchetype], }, } = world; | ||
const matched = new Set(); | ||
let stagedEnter = []; | ||
@@ -67,27 +68,28 @@ let stagedExit = []; | ||
entityRelocating.subscribe(function detectMonitorExit(entity, prev, next, diff) { | ||
if (_query === null) { | ||
if (_query === null) | ||
return; | ||
} | ||
const matchExit = _query.matchesArchetype(prev); | ||
// entity matched previously and was destroyed | ||
if (matchExit && next === rootArchetype) { | ||
const matchCurr = matched.has(entity); | ||
const matchPrev = _query.matchesArchetype(prev); | ||
const matchNext = _query.matchesArchetype(next); | ||
const isExit = matchPrev && (!matchNext || next === rootArchetype); | ||
if (!isExit) | ||
return; | ||
if (matchCurr) { | ||
const index = stagedEnter.findIndex(([e]) => e === entity); | ||
// entity matched and unmatched during the same tick | ||
if (index !== -1) { | ||
stagedEnter.splice(index, 1); | ||
} | ||
assert(index !== -1); | ||
mutableRemoveByIndexUnordered(stagedEnter, index); | ||
return; | ||
} | ||
if (matchExit && !_query.matchesArchetype(next)) { | ||
const snapshot = snapshots.retain(); | ||
snapshot[0] = entity; | ||
_query.get(entity, snapshot[1]); | ||
_query.match(diff, snapshot[2]); | ||
stagedExit.push(snapshot); | ||
} | ||
const snapshot = snapshots.retain(); | ||
snapshot[0] = entity; | ||
_query.get(entity, snapshot[1]); | ||
_query.match(diff, snapshot[2]); | ||
stagedExit.push(snapshot); | ||
}); | ||
entityRelocated.subscribe(function detectMonitorEnter(entity, prev, next, diff) { | ||
if (_query === null) { | ||
if (_query === null) | ||
return; | ||
} | ||
if (!_query.matchesArchetype(prev) && _query.matchesArchetype(next)) { | ||
const matchPrev = _query.matchesArchetype(prev); | ||
const matchNext = _query.matchesArchetype(next); | ||
if (!matchPrev && matchNext) { | ||
const snapshot = snapshots.retain(); | ||
@@ -98,2 +100,3 @@ snapshot[0] = entity; | ||
stagedEnter.push(snapshot); | ||
matched.add(entity); | ||
} | ||
@@ -114,2 +117,3 @@ }); | ||
} | ||
matched.clear(); | ||
if (onEnter !== undefined) { | ||
@@ -116,0 +120,0 @@ for (let i = 0; i < readyEnter.length; i++) { |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).Javelin={})}(this,(function(e){"use strict";function t(e,t="",n){if(!e)throw new Error(void 0!==n?`${r[n]}: ${t}`:t)}var n;!function(e){e[e.Internal=0]="Internal",e[e.Query=1]="Query"}(n||(n={}));const r={[n.Internal]:"Internal Error",[n.Query]:"Query Error"},o=Symbol("javelin_field_kind"),s=Symbol("javelin_model_flat");var c;function l(e){for(;e.length>0;)e.pop();return e}!function(e){e[e.Number=0]="Number",e[e.String=1]="String",e[e.Boolean=2]="Boolean",e[e.Array=3]="Array",e[e.Object=4]="Object",e[e.Set=5]="Set",e[e.Map=6]="Map",e[e.Dynamic=7]="Dynamic"}(c||(c={}));const a={[o]:c.Number,get:()=>0},i={[o]:c.String,get:()=>""},u={[o]:c.Boolean,get:()=>!1};function f(e){return o in e}function d(e){return o in e}function h(e){return!(o in e)}function p(e){if(!f(e))return!1;const t=e[o];return t===c.Number||t===c.String||t===c.Boolean||t===c.Dynamic}function g(e,t){if(t[e.id]=e,f(e))"element"in e&&g(e.element,t);else for(let n=0;n<e.fields.length;n++)g(e.fields[n],t)}function y(e){const t={};for(const n in e)g(e[n],t[n]={});return t}function m(e){const t={},n={id:-1};return e.forEach((e,r)=>{n.id=-1,t[r]=function e(t,n,r=[]){let o,s={id:++n.id,lo:n.id,hi:n.id,deep:r.length>0,traverse:r};if(f(t))o={...s,...t},"element"in o&&(o.element=e(o.element,n,"key"in o?[...r,o.key]:r));else{const c=Object.keys(t),l=[],a=[],i={},u={};for(let o=0;o<c.length;o++){const s=c[o],f=e(t[s],n,r);l[f.id]=s,i[s]=f,u[s]=f.id,a.push(f)}o={...s,keys:c,keysByFieldId:l,fields:a,fieldsByKey:i,fieldIdsByKey:u}}return o.hi=n.id,o}(e,n)}),Object.defineProperty(t,s,{enumerable:!1,writable:!1,value:y(t)})}function b(e){return h(e)?e.fields.every(p):!("element"in e)||p(e.element)}function v(e,t,n){const r=[],o=()=>{for(let t=0;t<n;t++)r.push(e(s))},s={allocate:o,retain:()=>(r.length||o(),r.pop()),release:e=>{r.push(t(e))}};return s}const _=()=>{const e=[];return{subscribe:t=>(e.push(t),()=>function(e,t){const n=e.length,r=e.indexOf(t);if(-1===r)return!1;const o=e.pop();return r<n-1&&(e[r]=o),!0}(e,t)),dispatch:(t,n,r,o)=>{for(let s=0;s<e.length;s++)e[s](t,n,r,o)}}},w={schemaIndex:new WeakMap,schemaPools:new Map,model:{[s]:{}},worlds:[],worldIds:0,currentWorldId:-1},S=_();function I(e){w.model=e,S.dispatch(e)}let k=0;function O(e,t){return v(()=>{return function e(t,n){for(const r in n){const o=n[r];let s;s=d(o)?o.get():e({},o),t[r]=s}return t}((t=e,Object.defineProperties({},{__type__:{value:w.schemaIndex.get(t),writable:!1,enumerable:!0}})),e);var t},t=>function e(t,n){for(const r in n){const o=n[r];d(o)?t[r]=o.get(t[r]):e(t[r],o)}return t}(t,e),t)}const x=new Map;function j(e,t,n=1e3){let r=w.schemaIndex.get(e);if(void 0!==r)return r;if(r=t,void 0===r){for(;x.has(k);)k++;r=k}else if(x.has(r))throw new Error("Failed to register component type: a component with same id is already registered");return w.schemaPools.set(r,O(e,n)),x.set(r,e),w.schemaIndex.set(e,r),I(m(x)),r}function A(e,t={throw:!1,global:!1}){const{global:n}=t,r=[];let o,s,c,l,a,i=-1;return function(...t){l=w.currentWorldId;const u=w.worlds[l],f=u.latestStep;a=n?0:u.latestSystem;let d=r[l];void 0===r[l]&&(d=r[l]=[]);let h=d[a];if(void 0===h&&(h=d[a]={cells:[],cellCount:-1}),!0===n||s!==l&&void 0!==s)i=0;else if(void 0===c||o===f&&c===a)i++;else{let e=d[c];if(-1!==e.cellCount&&e.cellCount!==i)throw new Error(`Failed to execute effect: encountered too ${e.cellCount>i?"few":"many"} effects this step`);e.cellCount=i,i=0}let p=h.cells[i];if(p||(p=h.cells[i]={executor:e(u),lockGlobal:!1,lockAsync:!1,lockGlobalTick:null,state:null}),n&&(p.lockGlobalTick!==u.latestStep?(p.lockGlobal=!1,p.lockGlobalTick=u.latestStep):p.lockGlobal=!0),p.lockGlobal||p.lockAsync)return p.state;const g=p.executor(...t);var y;return"object"==typeof(y=g)&&null!==y&&"then"in y?(p.lockAsync=!0,g.then(e=>p.state=e).catch(e=>console.error("Uncaught error in effect: "+e.message,e)).then(()=>p.lockAsync=!1)):p.state=g,o=f,s=l,c=a,p.state}}const M=A(()=>{let e=!0;const t={value:null};return function(n){return e&&(t.value=n,e=!1),t}}),C=A(()=>{let e,t=0;return function(n,r=!1){return r&&(t=0,clearTimeout(e)),0===t&&(t=1,e=setTimeout(()=>{t=2},n)),2===t}}),E=A(()=>function(e){const t=M(!1),n=C(e,t.value);return t.value=n,n}),D=v(()=>[-1,[],[]],e=>(e[0]=-1,l(e[1]),l(e[2]),e),1e3),T=A(e=>{const{storage:{entityRelocating:t,entityRelocated:n,archetypes:[r]}}=e;let o=[],s=[],c=[],a=[],i=null;return t.subscribe((function(e,t,n,c){if(null===i)return;const l=i.matchesArchetype(t);if(l&&n===r){const t=o.findIndex(([t])=>t===e);-1!==t&&o.splice(t,1)}if(l&&!i.matchesArchetype(n)){const t=D.retain();t[0]=e,i.get(e,t[1]),i.match(c,t[2]),s.push(t)}})),n.subscribe((function(e,t,n,r){if(null!==i&&!i.matchesArchetype(t)&&i.matchesArchetype(n)){const t=D.retain();t[0]=e,i.get(e,t[1]),i.match(r,t[2]),o.push(t)}})),function(e,t,n){let r;for(i===e||(null==i?void 0:i.equals(e))||(e=>{i=e,l(o),l(s),l(c),l(a);for(const[t]of e)for(let n=0;n<t.length;n++){const r=t[n],s=D.retain();s[0]=r,e.get(r,s[1]),e.get(r,s[2]),o.push(s)}})(e),l(c),l(a);void 0!==(r=o.pop());)c.push(r);for(;void 0!==(r=s.pop());)a.push(r);if(void 0!==t)for(let e=0;e<c.length;e++){const n=c[e];t(n[0],n[1],n[2]),D.release(n)}if(void 0!==n)for(let e=0;e<a.length;e++){const t=a[e];n(t[0],t[1],t[2]),D.release(t)}}});const P=A(()=>{let e,t={response:null,error:null,done:!1},n=!1,r=new window.AbortController;return function(o,s,c=void 0!==e&&o!==e){return(null===o||c)&&(r.abort(),r=new AbortController),null===o?t:(c&&(t={response:t.response,error:null,done:!1}),t.done||n||(n=!0,e=o,fetch(o,{...s,signal:r.signal}).then(e=>{t={response:e,error:null,done:!0}}).catch(e=>{t={response:t.response,error:e,done:!0}}).then(()=>{n=!1})),t)}}),B=A(()=>{let e;return function(...t){const n=M(null),r=P(...t);return r.response&&r.response!==n.value&&(r.response.json().then(t=>{e=t}),n.value=r.response),{...r,response:e||null}}}),F=Symbol("javelin_proxy_self"),W=Symbol("javelin_proxy_touched"),N=Symbol("javelin_proxy_changes"),G=Symbol("javelin_proxy_deleted"),Q=new WeakMap,R={get:(e,t)=>t===F?e:(e[W]=!0,e[t]),set(e,t,n){const r=e[N];return e[t]=n,e[W]=!0,r.changes[t]=n,r.dirty=!0,!0}},$={get(e,t){if(t===F)return e;const n=e[t];return e[W]=!0,"object"==typeof n&&null!==n?Z(n,e,t):n},set:R.set},q=R,U={get:$.get,set:q.set},K={...R,deleteProperty(e,t){const n=e[N];return delete e[t],e[W]=!0,n.changes[t]=G,n.dirty=!0,!0}},z={...$,deleteProperty:K.deleteProperty},J={get(e,t){if(t===F)return e;const n=e[t];return e[W]=!0,"function"==typeof n?new Proxy(n,V):n}},V={apply(e,t,n){const{[F]:r,[N]:o}=t;switch(t[W]=!0,e){case Set.prototype.add:o.changes.add.push(n[0]),o.dirty=!0;break;case Set.prototype.delete:o.changes.delete.push(n[0]),o.dirty=!0;break;case Set.prototype.clear:r.forEach(e=>o.changes.delete.push(e)),o.dirty=!0}return e.apply(r,n)}},H={get(e,t,n){if(t===F)return e;const r=Reflect.get(e,t,n);return e[W]=!0,"function"==typeof r?new Proxy(r,L):r}},L={apply(e,t,n){const{[F]:r}=t,{[N]:o}=r;switch(r[W]=!0,e){case Map.prototype.get:{const t=e.apply(r,n);if("object"==typeof t&&null!==t)return Z(t,r,n[0])}case Map.prototype.set:o.changes.set(n[0],n[1]),o.dirty=!0;break;case Map.prototype.delete:o.changes.set(n[0],G),o.dirty=!0;break;case Map.prototype.clear:return r.forEach((e,t)=>o.changes.set(t,G)),o.dirty=!0,r.clear()}return e.apply(r,n)}};const X={configurable:!1,enumerable:!0,writable:!1};function Y(e,t){const n=function(e){const t={dirty:!1,node:e};if(f(e))switch(e[o]){case c.Array:case c.Object:return{...t,changes:{}};case c.Set:return{...t,changes:{add:[],delete:[]}};case c.Map:return{...t,changes:new Map}}return{...t,changes:{}}}(t),r=Object.defineProperties(e,{[F]:{...X,value:e},[N]:{...X,value:n}}),s=function(e){const t=b(e);if(f(e))switch(e[o]){case c.Array:return t?q:U;case c.Object:return t?K:z;case c.Set:return J;case c.Map:return H;default:throw new Error("Failed to observe object: cannot observe a primitive type")}return t?R:$}(t),l=new Proxy(r,s);return Q.set(e,l),l}function Z(e,n,r){var o;const s=n[N].node;let c;return h(s)?c=s.fieldsByKey[r]:(t("element"in s),c=s.element),null!==(o=Q.get(e))&&void 0!==o?o:Y(e,c)}const ee="a query must be executed within a system or bound to a world using Query.bind()",te=(e,t,n)=>function(e,t){let n=0,r=0;if(e.length<t.length)return!1;for(;n<e.length&&r<t.length;)if(e[n]<t[r])n++;else{if(e[n]!==t[r])return!1;n++,r++}return r===t.length}(n.signature,e)&&n.signature.every(e=>!t.not.has(e));function ne(e){const t="snapshot"in e?e.snapshot:null,n=t?Object.keys(t.indices).map(Number):[],r=t?function(e){const t=[];for(const n in e){const r=parseInt(n,10);isNaN(r)||(t[r]=e[n])}return t}(t.indices):[],o=("signature"in e?e.signature:e.snapshot.signature).slice().sort((e,t)=>e-t),s=t?t.table.map(e=>e.slice()):o.map(()=>[]),c=o.reduce((e,t,n)=>(e[t]=n,e),[]);return{entities:n,indices:r,signature:o,signatureInverse:c,table:s}}function re(e){const{signature:t,signatureInverse:n,entities:r,indices:o,table:s}=ne(e);return{entities:r,indices:o,insert:function(e,t){for(let e=0;e<t.length;e++){const r=t[e],o=n[r.__type__];s[o].push(r)}o[e]=r.push(e)-1},remove:function(e){const t=r.length,n=o[e],c=r.pop();if(delete o[e],n===t-1)for(const e of s)e.pop();else{for(const e of s)e[n]=e.pop();r[n]=c,o[c]=n}},signature:t,signatureInverse:n,table:s}}const oe="Failed to locate component: schema not registered";function se(e={}){const n=e.snapshot?e.snapshot.archetypes.map(e=>re({snapshot:e})):[re({signature:[]})],r=[],o=_(),s=_(),c=_();function a(e){let t=function(e){const t=e.length;e:for(let r=0;r<n.length;r++){const o=n[r],{signature:s,signatureInverse:c}=o;if(s.length===t){for(let n=0;n<t;n++)if(void 0===c[e[n].__type__])continue e;return o}}return null}(e);return null===t&&(t=re({signature:e.map(e=>e.__type__)}),n.push(t),c.dispatch(t)),t}function i(e){const n=r[e];return t(void 0!==n,"Failed to locate entity: entity has not been created"),t(null!==n,"Failed to locate entity: entity has been destroyed"),n}function u(e,t,n,c){const l=a(n);o.dispatch(t,e,l,c),e.remove(t),l.insert(t,n),r[t]=l,s.dispatch(t,e,l,c)}function f(e,t){const c=r[e];if(null==c){const c=a(t);o.dispatch(e,n[0],c,t),c.insert(e,t),r[e]=c,s.dispatch(e,n[0],c,t)}else{const n=c.indices[e],r=t.slice();for(let e=0;e<c.signature.length;e++){const o=c.signature[e];t.find(e=>e.__type__===o)||r.push(c.table[e][n])}u(c,e,r,t)}}function d(e,t){const n=i(e),r=[],o=[],s=n.indices[e];for(let e=0;e<n.signature.length;e++){const c=n.signature[e],l=n.table[e][s];(t.includes(c)?r:o).push(l)}u(n,e,o,r)}const h=[];function p(e,t){const n=i(e),r=n.signatureInverse[t];if(void 0===r)return null;const o=n.indices[e];return n.table[r][o]}return{archetypeCreated:c,archetypes:n,attachComponents:f,attachOrUpdateComponents:function(e,t){const n=i(e),r=n.indices[e];l(h);for(let e=0;e<t.length;e++){const o=t[e],s=n.signatureInverse[o.__type__];void 0===s?h.push(o):Object.assign(n.table[s][r],o)}h.length>0&&f(e,h)},reset:function(){l(n),l(r)},clearComponents:function(e){d(e,i(e).signature),r[e]=null},detachBySchemaId:d,entityRelocated:s,entityRelocating:o,getComponentBySchemaId:p,getComponentBySchema:function(e,n){const r=w.schemaIndex.get(n);return t(void 0!==r,oe),p(e,r)},getAllComponents:function(e){const t=i(e),n=t.indices[e],r=[];for(let e=0;e<t.table.length;e++)r.push(t.table[e][n]);return r},getSnapshot:function(){return{archetypes:n.map(e=>{return{signature:e.signature.slice(),table:e.table.map(e=>e.map(e=>({...e}))),indices:(t=e.indices,t.reduce((e,t,n)=>(e[n]=t,e),{}))};var t})}},hasComponentOfSchema:function(e,n){const r=i(e),o=w.schemaIndex.get(n);return t(void 0!==o,oe),r.signature.includes(o)}}}const ce=Symbol("javelin_system_id");var le;(le=e.DeferredOpType||(e.DeferredOpType={}))[le.Spawn=0]="Spawn",le[le.Attach=1]="Attach",le[le.Detach=2]="Detach",le[le.Mutate=3]="Mutate",le[le.Destroy=4]="Destroy",e.$changes=N,e.$delete=G,e.$self=F,e.$touched=W,e.UNSAFE_internals=w,e.UNSAFE_modelChanged=S,e.UNSAFE_setModel=I,e.arrayOf=function(e){return{[o]:c.Array,get:(e=[])=>l(e),element:e}},e.boolean=u,e.clear=function(e){const t=F in e?e[F]:e;return function e(t,n){if(!0!==t[W])return;const r=t[N];if(h(n)){for(const e in r.changes)delete r.changes[e];for(let r=0;r<n.fields.length;r++)e(t[n.keys[r]],n.fields[r])}else if("element"in n){const s=n.element;switch(n[o]){case c.Array:for(const e in r.changes)delete r.changes[e];for(let n=0;n<t.length;n++)e(t[n],s);break;case c.Object:for(const e in r.changes)delete r.changes[e];for(const n in t)e(t[n],s);break;case c.Set:l(r.changes.add),l(r.changes.delete);break;case c.Map:r.changes.clear(),t.forEach(t=>e(t,s))}}r.dirty=!1,t[W]=!1}(t,w.model[t.__type__])},e.component=function(e,t){const n=j(e),r=w.schemaPools.get(n).retain();return void 0!==t&&Object.assign(r,t),r},e.createComponentPool=O,e.createEffect=A,e.createPatch=function(e,t={schemaId:e.__type__,children:new Map,changes:new Map}){return N in e&&function e(t,n={changes:new Map,children:new Map}){const r=t[F],{[N]:{node:s,changes:l}}=r,a=b(s);if(h(s))if(a)for(let e=0;e<s.fields.length;e++){const t=s.keys[e];t in l&&n.changes.set(t,r[t])}else for(let t=0;t<s.fields.length;t++){const o=s.keys[t],c=r[o];o in l&&n.changes.set(o,c),c[W]&&n.children.set(o,e(c,n.children.get(o)))}else if("element"in s)switch(s[o]){case c.Array:if(a)for(let e=0;e<r.length;e++)e in l&&n.changes.set(e,r[e]);else for(let t=0;t<r.length;t++){const o=r[t];t in l&&n.changes.set(t,o),o[W]&&n.children.set(t,e(o,n.children.get(t)))}break;case c.Map:a?l.forEach((e,t)=>n.changes.set(t,e)):r.forEach((t,r)=>{l.has(r)&&(n.changes.set(r,t),t[W]&&n.children.set(r,e(t,n.children.get(r))))})}return n}(e,t),t},e.createQuery=(...e)=>function e(r){var o,s;const c=r.select.length,a=null!==(o=r.filters)&&void 0!==o?o:{not:new Set},i=r.select.map(e=>j(e)).sort((e,t)=>e-t),u=(null!==(s=r.include)&&void 0!==s?s:r.select).map(e=>j(e)),f=[];let d=r.context;const h=(e,t)=>{if(te(i,a,e)){const n=u.map(t=>e.table[e.signature.indexOf(t)]);t.push([e.entities,n,e.indices])}},p=e=>{const t=w.worlds[e],n=[];return f[e]=n,t.storage.archetypes.forEach(e=>h(e,n)),t.storage.archetypeCreated.subscribe(e=>h(e,n)),n},g=v(()=>[],e=>(l(e),e),1e3),y=e=>{const r=null!=d?d:w.currentWorldId;t(null!==r&&-1!==r,ee,n.Query);const o=f[r]||p(r),s=g.retain();for(let t=0;t<o.length;t++){const[n,r]=o[t];for(let t=0;t<n.length;t++){for(let e=0;e<c;e++)s[e]=r[e][t];e(n[t],s)}}g.release(s)};return y.signature=i,y.filters=a,y.not=(...t)=>e({...r,filters:{not:new Set(t.map(e=>w.schemaIndex.get(e)).filter(e=>"number"==typeof e))}}),y.select=(...t)=>e({...r,include:t}),y.get=(e,t=[])=>{const n=null!=d?d:w.currentWorldId,r=f[n];for(let n=0;n<r.length;n++){const[,o,s]=r[n],c=s[e];if(void 0!==c){for(let e=0;e<o.length;e++)t[e]=o[e][c];return t}}throw new Error("Failed to get components of query: entity does not match query")},y.bind=t=>e({...r,context:t.id}),y.test=e=>{const t=null!=d?d:w.currentWorldId,n=f[t];for(let t=0;t<n.length;t++){if(void 0!==n[t][2][e])return!0}return!1},y.matchesArchetype=e=>te(i,a,e),y[Symbol.iterator]=()=>{const e=null!=d?d:w.currentWorldId;t(null!==e&&-1!==e,ee,n.Query);return(f[e]||p(e))[Symbol.iterator]()},y.equals=e=>{if(e.signature.length!==i.length)return!1;for(let t=0;t<e.signature.length;t++)if(e.signature[t]!==i[t])return!1;if(e.filters.not.size!==a.not.size)return!1;let t=!0;return e.filters.not.forEach(e=>t=t&&a.not.has(e)),t},y.match=(e,t=[])=>{for(let e=0;e<u.length;e++)t[e]=null;for(let n=0;n<e.length;n++){const r=e[n],o=u.indexOf(r.__type__);-1!==o&&(t[o]=r)}return t},y}({select:e}),e.createRef=(e,t={})=>A(t=>{const n=e(t);return()=>M(n)},t),e.createTopic=()=>{const e=[],t=[];return{*[Symbol.iterator](){for(let e=0;e<t.length;e++)yield t[e]},push:t=>e.push(t),pushImmediate:e=>t.push(e),flush:()=>{l(t);for(let n=e.length-1;n>=0;n--)t[n]=e.pop()},clear:()=>{l(e),l(t)}}},e.createWorld=function(n={}){var r,o;const{topics:s=[]}=n,c=[],a=[],i=v(()=>[],l,1e3),u=new Set,f=se({snapshot:null===(r=n.snapshot)||void 0===r?void 0:r.storage});let d=0,h=0;function p(...e){const t=i.retain();for(let n=0;n<e.length;n++)t[n]=e[n];return t}function g(e){const t=w.schemaPools.get(e.__type__);t&&t.release(e)}function y(e){c.push(e),e[ce]=h++}function m(e,t){f.attachComponents(e,t)}function b(e,n){const r=[];for(let o=0;o<n.length;o++){const s=n[o],c=f.getComponentBySchemaId(e,s);t(null!==c,"Failed to detach component: entity does not have component of type "+s),r.push(c)}f.detachBySchemaId(e,n),r.forEach(g)}function _(e){f.clearComponents(e)}function S(t){switch(t[0]){case e.DeferredOpType.Attach:!function(e){const[,t,n]=e;m(t,n)}(t);break;case e.DeferredOpType.Detach:!function(e){const[,t,n]=e;b(t,n)}(t);break;case e.DeferredOpType.Destroy:!function(e){const[,t]=e;_(t)}(t)}i.release(t)}null===(o=n.systems)||void 0===o||o.forEach(y);const I=w.worldIds++,k={id:I,storage:f,latestStep:-1,latestStepData:null,latestSystem:-1,attach:function(t,...n){a.push(p(e.DeferredOpType.Attach,t,n))},attachImmediate:m,addSystem:y,addTopic:function(e){s.push(e)},create:function(...t){const n=d++;return t.length>0&&a.push(p(e.DeferredOpType.Attach,n,t)),n},destroy:function(t){u.has(t)||(a.push(p(e.DeferredOpType.Destroy,t)),u.add(t))},destroyImmediate:_,get:function(e,t){j(t);const n=f.getComponentBySchema(e,t);if(null===n)throw new Error("Failed to get component: entity does not have component");return n},getSnapshot:function(){return{storage:f.getSnapshot()}},has:function(e,t){return j(t),f.hasComponentOfSchema(e,t)},detach:function(t,...n){if(0===n.length)return;const r=n.map(e=>{var t;return"number"==typeof e?e:null!==(t=w.schemaIndex.get(e))&&void 0!==t?t:e.__type__});a.push(p(e.DeferredOpType.Detach,t,r))},detachImmediate:b,removeSystem:function(e){const t=c.indexOf(e);t>-1&&c.splice(t,1)},removeTopic:function(e){const t=s.indexOf(e);t>-1&&s.splice(t,1)},reset:function(){for(u.clear();a.length>0;)i.release(a.pop());l(a),l(c),s.forEach(e=>e.clear()),l(s),d=0,k.latestStep=-1,k.latestStepData=null,k.latestSystem=-1;for(let e=0;e<f.archetypes.length;e++){const t=f.archetypes[e];for(let e=0;e<t.signature.length;e++){const n=t.table[e],r=w.schemaPools.get(t.signature[e]);for(let e=0;e<n.length;e++){const t=n[e];null==r||r.release(t)}}}f.reset()},step:function(e){let t=w.currentWorldId;w.currentWorldId=I,k.latestStepData=e;for(let e=0;e<a.length;e++)S(a[e]);l(a);for(let e=0;e<s.length;e++)s[e].flush();for(let e=0;e<c.length;e++){const t=c[e];k.latestSystem=t[ce],t(k)}u.clear(),k.latestStep++,w.currentWorldId=t},tryGet:function(e,t){return j(t),f.getComponentBySchema(e,t)}};return w.worlds.push(k),k},e.getFieldValue=function(e,n,r,s){let l=0,a=null,i=n;e:for(;e.id!==r;)if(f(e)){switch(t("element"in e),a=s[l++],e[o]){case c.Array:case c.Object:i=i[a];break;case c.Map:i=i.get(a);break;default:throw new Error("Failed to apply change: invalid target field")}e=e.element}else for(let t=0;t<e.fields.length;t++){const n=e.fields[t];if(n.lo<=r&&n.hi>=r){a=e.keys[t],(e=n).id!==r&&(i=i[a]);continue e}}return i},e.isComponentOf=function(e,t){return e.__type__===w.schemaIndex.get(t)},e.mapOf=function(e,t){return{[o]:c.Map,get:(e=new Map)=>(e.clear(),e),key:e,element:t}},e.number=a,e.objectOf=function(e,t=i){return{[o]:c.Object,get:(e={})=>{for(const t in e)delete e[t];return e},key:t,element:e}},e.observe=function(e){var t;return e[W]=!0,null!==(t=Q.get(e))&&void 0!==t?t:Y(e,w.model[e.__type__])},e.registerSchema=j,e.resetPatch=function(e){e.changes.clear(),e.children.clear()},e.setOf=function(e){return{[o]:c.Set,get:(e=new Set)=>(e.clear(),e),element:e}},e.string=i,e.useInterval=E,e.useJson=B,e.useMonitor=T,e.useRef=M,e.useRequest=P,e.useTimer=C,e.useWorld=function(){return w.worlds[w.currentWorldId]},Object.defineProperty(e,"__esModule",{value:!0})})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).Javelin={})}(this,(function(e){"use strict";function t(e,t="",n){if(!e)throw new Error(void 0!==n?`${r[n]}: ${t}`:t)}var n;!function(e){e[e.Internal=0]="Internal",e[e.Query=1]="Query"}(n||(n={}));const r={[n.Internal]:"Internal Error",[n.Query]:"Query Error"},o=Symbol("javelin_field_kind"),s=Symbol("javelin_model_flat");var c;function l(e){for(;e.length>0;)e.pop();return e}!function(e){e[e.Number=0]="Number",e[e.String=1]="String",e[e.Boolean=2]="Boolean",e[e.Array=3]="Array",e[e.Object=4]="Object",e[e.Set=5]="Set",e[e.Map=6]="Map",e[e.Dynamic=7]="Dynamic"}(c||(c={}));const a={[o]:c.Number,get:()=>0},i={[o]:c.String,get:()=>""},u={[o]:c.Boolean,get:()=>!1};function f(e){return o in e}function d(e){return o in e}function h(e){return!(o in e)}function p(e){if(!f(e))return!1;const t=e[o];return t===c.Number||t===c.String||t===c.Boolean||t===c.Dynamic}function g(e,t){if(t[e.id]=e,f(e))"element"in e&&g(e.element,t);else for(let n=0;n<e.fields.length;n++)g(e.fields[n],t)}function y(e){const t={};for(const n in e)g(e[n],t[n]={});return t}function m(e){const t={},n={id:-1};return e.forEach((e,r)=>{n.id=-1,t[r]=function e(t,n,r=[]){let o,s={id:++n.id,lo:n.id,hi:n.id,deep:r.length>0,traverse:r};if(f(t))o={...s,...t},"element"in o&&(o.element=e(o.element,n,"key"in o?[...r,o.key]:r));else{const c=Object.keys(t),l=[],a=[],i={},u={};for(let o=0;o<c.length;o++){const s=c[o],f=e(t[s],n,r);l[f.id]=s,i[s]=f,u[s]=f.id,a.push(f)}o={...s,keys:c,keysByFieldId:l,fields:a,fieldsByKey:i,fieldIdsByKey:u}}return o.hi=n.id,o}(e,n)}),Object.defineProperty(t,s,{enumerable:!1,writable:!1,value:y(t)})}function b(e){return h(e)?e.fields.every(p):!("element"in e)||p(e.element)}function v(e,t,n){const r=[],o=()=>{for(let t=0;t<n;t++)r.push(e(s))},s={allocate:o,retain:()=>(r.length||o(),r.pop()),release:e=>{r.push(t(e))}};return s}const _=()=>{const e=[];return{subscribe:t=>(e.push(t),()=>function(e,t){const n=e.length,r=e.indexOf(t);if(-1===r)return!1;const o=e.pop();return r<n-1&&(e[r]=o),!0}(e,t)),dispatch:(t,n,r,o)=>{for(let s=0;s<e.length;s++)e[s](t,n,r,o)}}},w={schemaIndex:new WeakMap,schemaPools:new Map,model:{[s]:{}},worlds:[],worldIds:0,currentWorldId:-1},S=_();function I(e){w.model=e,S.dispatch(e)}let k=0;function O(e,t){return v(()=>{return function e(t,n){for(const r in n){const o=n[r];let s;s=d(o)?o.get():e({},o),t[r]=s}return t}((t=e,Object.defineProperties({},{__type__:{value:w.schemaIndex.get(t),writable:!1,enumerable:!0}})),e);var t},t=>function e(t,n){for(const r in n){const o=n[r];d(o)?t[r]=o.get(t[r]):e(t[r],o)}return t}(t,e),t)}const x=new Map;function j(e,t,n=1e3){let r=w.schemaIndex.get(e);if(void 0!==r)return r;if(r=t,void 0===r){for(;x.has(k);)k++;r=k}else if(x.has(r))throw new Error("Failed to register component type: a component with same id is already registered");return w.schemaPools.set(r,O(e,n)),x.set(r,e),w.schemaIndex.set(e,r),I(m(x)),r}function A(e,t={throw:!1,global:!1}){const{global:n}=t,r=[];let o,s,c,l,a,i=-1;return function(...t){l=w.currentWorldId;const u=w.worlds[l],f=u.latestStep;a=n?0:u.latestSystem;let d=r[l];void 0===r[l]&&(d=r[l]=[]);let h=d[a];if(void 0===h&&(h=d[a]={cells:[],cellCount:-1}),!0===n||s!==l&&void 0!==s)i=0;else if(void 0===c||o===f&&c===a)i++;else{let e=d[c];if(-1!==e.cellCount&&e.cellCount!==i)throw new Error(`Failed to execute effect: encountered too ${e.cellCount>i?"few":"many"} effects this step`);e.cellCount=i,i=0}let p=h.cells[i];if(p||(p=h.cells[i]={executor:e(u),lockGlobal:!1,lockAsync:!1,lockGlobalTick:null,state:null}),n&&(p.lockGlobalTick!==u.latestStep?(p.lockGlobal=!1,p.lockGlobalTick=u.latestStep):p.lockGlobal=!0),p.lockGlobal||p.lockAsync)return p.state;const g=p.executor(...t);var y;return"object"==typeof(y=g)&&null!==y&&"then"in y?(p.lockAsync=!0,g.then(e=>p.state=e).catch(e=>console.error("Uncaught error in effect: "+e.message,e)).then(()=>p.lockAsync=!1)):p.state=g,o=f,s=l,c=a,p.state}}const M=A(()=>{let e=!0;const t={value:null};return function(n){return e&&(t.value=n,e=!1),t}}),C=A(()=>{let e,t=0;return function(n,r=!1){return r&&(t=0,clearTimeout(e)),0===t&&(t=1,e=setTimeout(()=>{t=2},n)),2===t}}),E=A(()=>function(e){const t=M(!1),n=C(e,t.value);return t.value=n,n}),D=v(()=>[-1,[],[]],e=>(e[0]=-1,l(e[1]),l(e[2]),e),1e3),T=A(e=>{const{storage:{entityRelocating:n,entityRelocated:r,archetypes:[o]}}=e,s=new Set;let c=[],a=[],i=[],u=[],f=null;return n.subscribe((function(e,n,r,l){if(null===f)return;const i=s.has(e),u=f.matchesArchetype(n),d=f.matchesArchetype(r);if(!(u&&(!d||r===o)))return;if(i){const n=c.findIndex(([t])=>t===e);return t(-1!==n),void function(e,t){const n=e.length;if(-1===t)return!1;const r=e.pop();t<n-1&&(e[t]=r)}(c,n)}const h=D.retain();h[0]=e,f.get(e,h[1]),f.match(l,h[2]),a.push(h)})),r.subscribe((function(e,t,n,r){if(null===f)return;const o=f.matchesArchetype(t),l=f.matchesArchetype(n);if(!o&&l){const t=D.retain();t[0]=e,f.get(e,t[1]),f.match(r,t[2]),c.push(t),s.add(e)}})),function(e,t,n){let r;for(f===e||(null==f?void 0:f.equals(e))||(e=>{f=e,l(c),l(a),l(i),l(u);for(const[t]of e)for(let n=0;n<t.length;n++){const r=t[n],o=D.retain();o[0]=r,e.get(r,o[1]),e.get(r,o[2]),c.push(o)}})(e),l(i),l(u);void 0!==(r=c.pop());)i.push(r);for(;void 0!==(r=a.pop());)u.push(r);if(s.clear(),void 0!==t)for(let e=0;e<i.length;e++){const n=i[e];t(n[0],n[1],n[2]),D.release(n)}if(void 0!==n)for(let e=0;e<u.length;e++){const t=u[e];n(t[0],t[1],t[2]),D.release(t)}}});const P=A(()=>{let e,t={response:null,error:null,done:!1},n=!1,r=new window.AbortController;return function(o,s,c=void 0!==e&&o!==e){return(null===o||c)&&(r.abort(),r=new AbortController),null===o?t:(c&&(t={response:t.response,error:null,done:!1}),t.done||n||(n=!0,e=o,fetch(o,{...s,signal:r.signal}).then(e=>{t={response:e,error:null,done:!0}}).catch(e=>{t={response:t.response,error:e,done:!0}}).then(()=>{n=!1})),t)}}),B=A(()=>{let e;return function(...t){const n=M(null),r=P(...t);return r.response&&r.response!==n.value&&(r.response.json().then(t=>{e=t}),n.value=r.response),{...r,response:e||null}}});const F=Symbol("javelin_proxy_self"),W=Symbol("javelin_proxy_touched"),N=Symbol("javelin_proxy_changes"),R=Symbol("javelin_proxy_deleted"),G=new WeakMap,Q={get:(e,t)=>t===F?e:(e[W]=!0,e[t]),set(e,t,n){const r=e[N];return e[t]=n,e[W]=!0,r.changes[t]=n,r.dirty=!0,!0}},$={get(e,t){if(t===F)return e;const n=e[t];return e[W]=!0,"object"==typeof n&&null!==n?Z(n,e,t):n},set:Q.set},q=Q,U={get:$.get,set:q.set},K={...Q,deleteProperty(e,t){const n=e[N];return delete e[t],e[W]=!0,n.changes[t]=R,n.dirty=!0,!0}},z={...$,deleteProperty:K.deleteProperty},J={get(e,t){if(t===F)return e;const n=e[t];return e[W]=!0,"function"==typeof n?new Proxy(n,V):n}},V={apply(e,t,n){const{[F]:r,[N]:o}=t;switch(t[W]=!0,e){case Set.prototype.add:o.changes.add.push(n[0]),o.dirty=!0;break;case Set.prototype.delete:o.changes.delete.push(n[0]),o.dirty=!0;break;case Set.prototype.clear:r.forEach(e=>o.changes.delete.push(e)),o.dirty=!0}return e.apply(r,n)}},H={get(e,t,n){if(t===F)return e;const r=Reflect.get(e,t,n);return e[W]=!0,"function"==typeof r?new Proxy(r,L):r}},L={apply(e,t,n){const{[F]:r}=t,{[N]:o}=r;switch(r[W]=!0,e){case Map.prototype.get:{const t=e.apply(r,n);if("object"==typeof t&&null!==t)return Z(t,r,n[0])}case Map.prototype.set:o.changes.set(n[0],n[1]),o.dirty=!0;break;case Map.prototype.delete:o.changes.set(n[0],R),o.dirty=!0;break;case Map.prototype.clear:return r.forEach((e,t)=>o.changes.set(t,R)),o.dirty=!0,r.clear()}return e.apply(r,n)}};const X={configurable:!1,enumerable:!0,writable:!1};function Y(e,t){const n=function(e){const t={dirty:!1,node:e};if(f(e))switch(e[o]){case c.Array:case c.Object:return{...t,changes:{}};case c.Set:return{...t,changes:{add:[],delete:[]}};case c.Map:return{...t,changes:new Map}}return{...t,changes:{}}}(t),r=Object.defineProperties(e,{[F]:{...X,value:e},[N]:{...X,value:n}}),s=function(e){const t=b(e);if(f(e))switch(e[o]){case c.Array:return t?q:U;case c.Object:return t?K:z;case c.Set:return J;case c.Map:return H;default:throw new Error("Failed to observe object: cannot observe a primitive type")}return t?Q:$}(t),l=new Proxy(r,s);return G.set(e,l),l}function Z(e,n,r){var o;const s=n[N].node;let c;return h(s)?c=s.fieldsByKey[r]:(t("element"in s),c=s.element),null!==(o=G.get(e))&&void 0!==o?o:Y(e,c)}const ee="a query must be executed within a system or bound to a world using Query.bind()",te=(e,t,n)=>function(e,t){let n=0,r=0;if(e.length<t.length)return!1;for(;n<e.length&&r<t.length;)if(e[n]<t[r])n++;else{if(e[n]!==t[r])return!1;n++,r++}return r===t.length}(n.signature,e)&&n.signature.every(e=>!t.not.has(e));function ne(e){const t="snapshot"in e?e.snapshot:null,n=t?Object.keys(t.indices).map(Number):[],r=t?function(e){const t=[];for(const n in e){const r=parseInt(n,10);isNaN(r)||(t[r]=e[n])}return t}(t.indices):[],o=("signature"in e?e.signature:e.snapshot.signature).slice().sort((e,t)=>e-t),s=t?t.table.map(e=>e.slice()):o.map(()=>[]),c=o.reduce((e,t,n)=>(e[t]=n,e),[]);return{entities:n,indices:r,signature:o,signatureInverse:c,table:s}}function re(e){const{signature:t,signatureInverse:n,entities:r,indices:o,table:s}=ne(e);return{entities:r,indices:o,insert:function(e,t){for(let e=0;e<t.length;e++){const r=t[e],o=n[r.__type__];s[o].push(r)}o[e]=r.push(e)-1},remove:function(e){const t=r.length,n=o[e],c=r.pop();if(delete o[e],n===t-1)for(const e of s)e.pop();else{for(const e of s)e[n]=e.pop();r[n]=c,o[c]=n}},signature:t,signatureInverse:n,table:s}}const oe="Failed to locate component: schema not registered";function se(e={}){const n=e.snapshot?e.snapshot.archetypes.map(e=>re({snapshot:e})):[re({signature:[]})],r=[],o=_(),s=_(),c=_();function a(e){let t=function(e){const t=e.length;e:for(let r=0;r<n.length;r++){const o=n[r],{signature:s,signatureInverse:c}=o;if(s.length===t){for(let n=0;n<t;n++)if(void 0===c[e[n].__type__])continue e;return o}}return null}(e);return null===t&&(t=re({signature:e.map(e=>e.__type__)}),n.push(t),c.dispatch(t)),t}function i(e){const n=r[e];return t(void 0!==n,"Failed to locate entity: entity has not been created"),t(null!==n,"Failed to locate entity: entity has been destroyed"),n}function u(e,t,n,c){const l=a(n);o.dispatch(t,e,l,c),e.remove(t),l.insert(t,n),r[t]=l,s.dispatch(t,e,l,c)}function f(e,t){const c=r[e];if(null==c){const c=a(t);o.dispatch(e,n[0],c,t),c.insert(e,t),r[e]=c,s.dispatch(e,n[0],c,t)}else{const n=c.indices[e],r=t.slice();for(let e=0;e<c.signature.length;e++){const o=c.signature[e];t.find(e=>e.__type__===o)||r.push(c.table[e][n])}u(c,e,r,t)}}function d(e,t){const n=i(e),r=[],o=[],s=n.indices[e];for(let e=0;e<n.signature.length;e++){const c=n.signature[e],l=n.table[e][s];(t.includes(c)?r:o).push(l)}u(n,e,o,r)}const h=[];function p(e,t){const n=i(e),r=n.signatureInverse[t];if(void 0===r)return null;const o=n.indices[e];return n.table[r][o]}return{archetypeCreated:c,archetypes:n,attachComponents:f,attachOrUpdateComponents:function(e,t){const n=i(e),r=n.indices[e];l(h);for(let e=0;e<t.length;e++){const o=t[e],s=n.signatureInverse[o.__type__];void 0===s?h.push(o):Object.assign(n.table[s][r],o)}h.length>0&&f(e,h)},reset:function(){l(n),l(r)},clearComponents:function(e){d(e,i(e).signature),r[e]=null},detachBySchemaId:d,entityRelocated:s,entityRelocating:o,getComponentBySchemaId:p,getComponentBySchema:function(e,n){const r=w.schemaIndex.get(n);return t(void 0!==r,oe),p(e,r)},getAllComponents:function(e){const t=i(e),n=t.indices[e],r=[];for(let e=0;e<t.table.length;e++)r.push(t.table[e][n]);return r},getSnapshot:function(){return{archetypes:n.map(e=>{return{signature:e.signature.slice(),table:e.table.map(e=>e.map(e=>({...e}))),indices:(t=e.indices,t.reduce((e,t,n)=>(e[n]=t,e),{}))};var t})}},hasComponentOfSchema:function(e,n){const r=i(e),o=w.schemaIndex.get(n);return t(void 0!==o,oe),r.signature.includes(o)}}}const ce=Symbol("javelin_system_id");var le;(le=e.DeferredOpType||(e.DeferredOpType={}))[le.Spawn=0]="Spawn",le[le.Attach=1]="Attach",le[le.Detach=2]="Detach",le[le.Mutate=3]="Mutate",le[le.Destroy=4]="Destroy",e.$changes=N,e.$delete=R,e.$self=F,e.$touched=W,e.UNSAFE_internals=w,e.UNSAFE_modelChanged=S,e.UNSAFE_setModel=I,e.arrayOf=function(e){return{[o]:c.Array,get:(e=[])=>l(e),element:e}},e.boolean=u,e.clear=function(e){const t=F in e?e[F]:e;return function e(t,n){if(!0!==t[W])return;const r=t[N];if(h(n)){for(const e in r.changes)delete r.changes[e];for(let r=0;r<n.fields.length;r++)e(t[n.keys[r]],n.fields[r])}else if("element"in n){const s=n.element;switch(n[o]){case c.Array:for(const e in r.changes)delete r.changes[e];for(let n=0;n<t.length;n++)e(t[n],s);break;case c.Object:for(const e in r.changes)delete r.changes[e];for(const n in t)e(t[n],s);break;case c.Set:l(r.changes.add),l(r.changes.delete);break;case c.Map:r.changes.clear(),t.forEach(t=>e(t,s))}}r.dirty=!1,t[W]=!1}(t,w.model[t.__type__])},e.component=function(e,t){const n=j(e),r=w.schemaPools.get(n).retain();return void 0!==t&&Object.assign(r,t),r},e.createComponentPool=O,e.createEffect=A,e.createImmutableRef=function(e,t={}){return A(t=>{const n=e(t);return()=>M(n).value},t)},e.createPatch=function(e,t={schemaId:e.__type__,children:new Map,changes:new Map}){return N in e&&function e(t,n={changes:new Map,children:new Map}){const r=t[F],{[N]:{node:s,changes:l}}=r,a=b(s);if(h(s))if(a)for(let e=0;e<s.fields.length;e++){const t=s.keys[e];t in l&&n.changes.set(t,r[t])}else for(let t=0;t<s.fields.length;t++){const o=s.keys[t],c=r[o];o in l&&n.changes.set(o,c),c[W]&&n.children.set(o,e(c,n.children.get(o)))}else if("element"in s)switch(s[o]){case c.Array:if(a)for(let e=0;e<r.length;e++)e in l&&n.changes.set(e,r[e]);else for(let t=0;t<r.length;t++){const o=r[t];t in l&&n.changes.set(t,o),o[W]&&n.children.set(t,e(o,n.children.get(t)))}break;case c.Map:a?l.forEach((e,t)=>n.changes.set(t,e)):r.forEach((t,r)=>{l.has(r)&&(n.changes.set(r,t),t[W]&&n.children.set(r,e(t,n.children.get(r))))})}return n}(e,t),t},e.createQuery=(...e)=>function e(r){var o,s;const c=r.select.length,a=null!==(o=r.filters)&&void 0!==o?o:{not:new Set},i=r.select.map(e=>j(e)).sort((e,t)=>e-t),u=(null!==(s=r.include)&&void 0!==s?s:r.select).map(e=>j(e)),f=[];let d=r.context;const h=(e,t)=>{if(te(i,a,e)){const n=u.map(t=>e.table[e.signature.indexOf(t)]);t.push([e.entities,n,e.indices])}},p=e=>{const t=w.worlds[e],n=[];return f[e]=n,t.storage.archetypes.forEach(e=>h(e,n)),t.storage.archetypeCreated.subscribe(e=>h(e,n)),n},g=v(()=>[],e=>(l(e),e),1e3),y=e=>{const r=null!=d?d:w.currentWorldId;t(null!==r&&-1!==r,ee,n.Query);const o=f[r]||p(r),s=g.retain();for(let t=0;t<o.length;t++){const[n,r]=o[t];for(let t=0;t<n.length;t++){for(let e=0;e<c;e++)s[e]=r[e][t];e(n[t],s)}}g.release(s)};return y.signature=i,y.filters=a,y.not=(...t)=>e({...r,filters:{not:new Set(t.map(e=>w.schemaIndex.get(e)).filter(e=>"number"==typeof e))}}),y.select=(...t)=>e({...r,include:t}),y.get=(e,t=[])=>{const n=null!=d?d:w.currentWorldId,r=f[n];for(let n=0;n<r.length;n++){const[,o,s]=r[n],c=s[e];if(void 0!==c){for(let e=0;e<o.length;e++)t[e]=o[e][c];return t}}throw new Error("Failed to get components of query: entity does not match query")},y.bind=t=>e({...r,context:t.id}),y.test=e=>{const t=null!=d?d:w.currentWorldId,n=f[t];for(let t=0;t<n.length;t++){if(void 0!==n[t][2][e])return!0}return!1},y.matchesArchetype=e=>te(i,a,e),y[Symbol.iterator]=()=>{const e=null!=d?d:w.currentWorldId;t(null!==e&&-1!==e,ee,n.Query);return(f[e]||p(e))[Symbol.iterator]()},y.equals=e=>{if(e.signature.length!==i.length)return!1;for(let t=0;t<e.signature.length;t++)if(e.signature[t]!==i[t])return!1;if(e.filters.not.size!==a.not.size)return!1;let t=!0;return e.filters.not.forEach(e=>t=t&&a.not.has(e)),t},y.match=(e,t=[])=>{for(let e=0;e<u.length;e++)t[e]=null;for(let n=0;n<e.length;n++){const r=e[n],o=u.indexOf(r.__type__);-1!==o&&(t[o]=r)}return t},y}({select:e}),e.createRef=(e,t={})=>A(t=>{const n=e(t);return()=>M(n)},t),e.createTopic=()=>{const e=[],t=[];return{*[Symbol.iterator](){for(let e=0;e<t.length;e++)yield t[e]},push:t=>e.push(t),pushImmediate:e=>t.push(e),flush:()=>{l(t);for(let n=e.length-1;n>=0;n--)t[n]=e.pop()},clear:()=>{l(e),l(t)}}},e.createWorld=function(n={}){var r,o;const{topics:s=[]}=n,c=[],a=[],i=v(()=>[],l,1e3),u=new Set,f=se({snapshot:null===(r=n.snapshot)||void 0===r?void 0:r.storage});let d=0,h=0;function p(...e){const t=i.retain();for(let n=0;n<e.length;n++)t[n]=e[n];return t}function g(e){const t=w.schemaPools.get(e.__type__);t&&t.release(e)}function y(e){c.push(e),e[ce]=h++}function m(e,t){f.attachComponents(e,t)}function b(e,n){const r=[];for(let o=0;o<n.length;o++){const s=n[o],c=f.getComponentBySchemaId(e,s);t(null!==c,"Failed to detach component: entity does not have component of type "+s),r.push(c)}f.detachBySchemaId(e,n),r.forEach(g)}function _(e){f.clearComponents(e)}function S(t){switch(t[0]){case e.DeferredOpType.Attach:!function(e){const[,t,n]=e;m(t,n)}(t);break;case e.DeferredOpType.Detach:!function(e){const[,t,n]=e;b(t,n)}(t);break;case e.DeferredOpType.Destroy:!function(e){const[,t]=e;_(t)}(t)}i.release(t)}null===(o=n.systems)||void 0===o||o.forEach(y);const I=w.worldIds++,k={id:I,storage:f,latestStep:-1,latestStepData:null,latestSystem:-1,attach:function(t,...n){a.push(p(e.DeferredOpType.Attach,t,n))},attachImmediate:m,addSystem:y,addTopic:function(e){s.push(e)},create:function(...t){const n=d++;return t.length>0&&a.push(p(e.DeferredOpType.Attach,n,t)),n},destroy:function(t){u.has(t)||(a.push(p(e.DeferredOpType.Destroy,t)),u.add(t))},destroyImmediate:_,get:function(e,t){j(t);const n=f.getComponentBySchema(e,t);if(null===n)throw new Error("Failed to get component: entity does not have component");return n},getSnapshot:function(){return{storage:f.getSnapshot()}},has:function(e,t){return j(t),f.hasComponentOfSchema(e,t)},detach:function(t,...n){if(0===n.length)return;const r=n.map(e=>{var t;return"number"==typeof e?e:null!==(t=w.schemaIndex.get(e))&&void 0!==t?t:e.__type__});a.push(p(e.DeferredOpType.Detach,t,r))},detachImmediate:b,removeSystem:function(e){const t=c.indexOf(e);t>-1&&c.splice(t,1)},removeTopic:function(e){const t=s.indexOf(e);t>-1&&s.splice(t,1)},reset:function(){for(u.clear();a.length>0;)i.release(a.pop());l(a),l(c),s.forEach(e=>e.clear()),l(s),d=0,k.latestStep=-1,k.latestStepData=null,k.latestSystem=-1;for(let e=0;e<f.archetypes.length;e++){const t=f.archetypes[e];for(let e=0;e<t.signature.length;e++){const n=t.table[e],r=w.schemaPools.get(t.signature[e]);for(let e=0;e<n.length;e++){const t=n[e];null==r||r.release(t)}}}f.reset()},step:function(e){let t=w.currentWorldId;w.currentWorldId=I,k.latestStepData=e;for(let e=0;e<a.length;e++)S(a[e]);l(a);for(let e=0;e<s.length;e++)s[e].flush();for(let e=0;e<c.length;e++){const t=c[e];k.latestSystem=t[ce],t(k)}u.clear(),k.latestStep++,w.currentWorldId=t},tryGet:function(e,t){return j(t),f.getComponentBySchema(e,t)}};return w.worlds.push(k),k},e.getFieldValue=function(e,n,r,s){let l=0,a=null,i=n;e:for(;e.id!==r;)if(f(e)){switch(t("element"in e),a=s[l++],e[o]){case c.Array:case c.Object:i=i[a];break;case c.Map:i=i.get(a);break;default:throw new Error("Failed to apply change: invalid target field")}e=e.element}else for(let t=0;t<e.fields.length;t++){const n=e.fields[t];if(n.lo<=r&&n.hi>=r){a=e.keys[t],(e=n).id!==r&&(i=i[a]);continue e}}return i},e.isComponentOf=function(e,t){return e.__type__===w.schemaIndex.get(t)},e.mapOf=function(e,t){return{[o]:c.Map,get:(e=new Map)=>(e.clear(),e),key:e,element:t}},e.number=a,e.objectOf=function(e,t=i){return{[o]:c.Object,get:(e={})=>{for(const t in e)delete e[t];return e},key:t,element:e}},e.observe=function(e){var t;return e[W]=!0,null!==(t=G.get(e))&&void 0!==t?t:Y(e,w.model[e.__type__])},e.registerSchema=j,e.resetPatch=function(e){e.changes.clear(),e.children.clear()},e.setOf=function(e){return{[o]:c.Set,get:(e=new Set)=>(e.clear(),e),element:e}},e.string=i,e.useInterval=E,e.useJson=B,e.useMonitor=T,e.useRef=M,e.useRequest=P,e.useTimer=C,e.useWorld=function(){return w.worlds[w.currentWorldId]},Object.defineProperty(e,"__esModule",{value:!0})})); |
{ | ||
"name": "@javelin/ecs", | ||
"version": "1.0.0-alpha.6", | ||
"version": "1.0.0-alpha.7", | ||
"main": "dist/cjs/index.js", | ||
@@ -24,6 +24,6 @@ "module": "dist/esm/index.js", | ||
], | ||
"gitHead": "49279600fcd772df11e815cfd2dbe0d2ae3c50d2", | ||
"gitHead": "e07c53c0ad11fd6d2eb1f799b0d1f1ef0b4917c5", | ||
"dependencies": { | ||
"@javelin/core": "1.0.0-alpha.5" | ||
"@javelin/core": "1.0.0-alpha.7" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
339958
4520
+ Added@javelin/core@1.0.0-alpha.7(transitive)
- Removed@javelin/core@1.0.0-alpha.5(transitive)
Updated@javelin/core@1.0.0-alpha.7