aiecsjs 0.5.9 → 0.6.0
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/README.md +46 -12
- package/README_ZHTW.md +46 -12
- package/api.json +171 -48
- package/dist/chunk-27JX7WHY.js +2 -0
- package/dist/chunk-27JX7WHY.js.map +1 -0
- package/dist/chunk-GJ6U2TIU.js +2 -0
- package/dist/chunk-GJ6U2TIU.js.map +1 -0
- package/dist/chunk-LNZZ4WWC.js +2 -0
- package/dist/chunk-LNZZ4WWC.js.map +1 -0
- package/dist/chunk-MVBFYRHY.cjs +2 -0
- package/dist/chunk-MVBFYRHY.cjs.map +1 -0
- package/dist/chunk-NPNWRCB5.cjs +2 -0
- package/dist/chunk-NPNWRCB5.cjs.map +1 -0
- package/dist/chunk-O4UVAWFU.cjs +2 -0
- package/dist/chunk-O4UVAWFU.cjs.map +1 -0
- package/dist/commands.cjs +1 -1
- package/dist/commands.cjs.map +1 -1
- package/dist/commands.d.cts +1 -1
- package/dist/commands.d.ts +1 -1
- package/dist/commands.js +1 -1
- package/dist/commands.js.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +79 -13
- package/dist/index.d.ts +79 -13
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/loop.cjs +1 -1
- package/dist/loop.cjs.map +1 -1
- package/dist/loop.d.cts +12 -0
- package/dist/loop.d.ts +12 -0
- package/dist/loop.js +1 -1
- package/dist/loop.js.map +1 -1
- package/dist/observers.cjs +1 -1
- package/dist/observers.cjs.map +1 -1
- package/dist/observers.d.cts +7 -1
- package/dist/observers.d.ts +7 -1
- package/dist/observers.js +1 -1
- package/dist/observers.js.map +1 -1
- package/dist/relations.cjs +1 -1
- package/dist/relations.cjs.map +1 -1
- package/dist/relations.d.cts +18 -9
- package/dist/relations.d.ts +18 -9
- package/dist/relations.js +1 -1
- package/dist/relations.js.map +1 -1
- package/dist/serialize.cjs +1 -1
- package/dist/serialize.d.cts +30 -5
- package/dist/serialize.d.ts +30 -5
- package/dist/serialize.js +1 -1
- package/dist/{types-BeVLA7xG.d.cts → types-C6TkQ7HW.d.cts} +33 -0
- package/dist/{types-BeVLA7xG.d.ts → types-C6TkQ7HW.d.ts} +33 -0
- package/dist/worker.cjs +1 -1
- package/dist/worker.cjs.map +1 -1
- package/dist/worker.d.cts +10 -3
- package/dist/worker.d.ts +10 -3
- package/dist/worker.js +1 -1
- package/dist/worker.js.map +1 -1
- package/llms-full.txt +114 -18
- package/llms.txt +1 -0
- package/package.json +57 -22
- package/dist/chunk-JDV5JZP2.cjs +0 -2
- package/dist/chunk-JDV5JZP2.cjs.map +0 -1
- package/dist/chunk-OBMQME2P.js +0 -2
- package/dist/chunk-OBMQME2P.js.map +0 -1
- package/dist/chunk-Q2XLL6YG.cjs +0 -2
- package/dist/chunk-Q2XLL6YG.cjs.map +0 -1
- package/dist/chunk-USHKPJQG.cjs +0 -2
- package/dist/chunk-USHKPJQG.cjs.map +0 -1
- package/dist/chunk-XKN3ULCC.js +0 -2
- package/dist/chunk-XKN3ULCC.js.map +0 -1
- package/dist/chunk-ZZBNE7C5.js +0 -2
- package/dist/chunk-ZZBNE7C5.js.map +0 -1
package/dist/relations.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/relations.ts"],"names":["nextRelationId","defineRelation","options","ChildOf","getOrCreateStorage","state","rel","storage","linkIncoming","src","tgt","incoming","set","unlinkIncoming","addRelation","world","source","target","data","getWorldState","next","prevTgt","prevInner","list","inner","removeRelation","idx","getRelationTargets","gen","packEid","tIdx","getRelationData","registerRelationsCleanup","eid","e","cur","incomingSet"],"mappings":"wCAMA,IAAIA,EAAiB,CAAA,CAEd,SAASC,EAAyBC,CAAAA,CAAgD,CAEvF,OAAO,CACL,MAAA,CAAQ,WACR,IAAA,CAHSF,CAAAA,EAAAA,CAIT,YAAaE,CAAAA,EAAS,SAAA,EAAa,M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A,CAAc7B,CAAAA,CAAQ,UAAU,GAAA,CAAI2B,CAAC,EAC3C,GAAIE,CAAAA,CAAa,CACf,IAAA,IAAW3B,CAAAA,IAAO2B,EACZ3B,CAAAA,CAAMF,CAAAA,CAAQ,UAAU,MAAA,GAAQA,CAAAA,CAAQ,UAAUE,CAAG,CAAA,CAAI,EAAA,CAAA,CAE/DF,CAAAA,CAAQ,UAAU,MAAA,CAAO2B,CAAC,EAC5B,CACF,CACA3B,EAAQ,QAAA,CAAS,MAAA,CAAO2B,CAAC,CAAA,CACzB,IAAA,GAAW,CAACzB,CAAAA,CAAKc,CAAI,IAAKhB,CAAAA,CAAQ,QAAA,CAAU,CAC1C,IAAMmB,CAAAA,CAAMH,CAAAA,CAAK,OAAA,CAAQW,CAAC,CAAA,CACtBR,CAAAA,EAAO,IACTH,CAAAA,CAAK,MAAA,CAAOG,EAAK,CAAC,CAAA,CACdH,EAAK,MAAA,GAAW,CAAA,EAAGhB,EAAQ,QAAA,CAAS,MAAA,CAAOE,CAAG,CAAA,EAEtD,CAIAF,EAAQ,IAAA,CAAK,MAAA,CAAO2B,CAAC,CAAA,CACrB,IAAA,GAAW,CAACzB,CAAAA,CAAKe,CAAK,IAAKjB,CAAAA,CAAQ,IAAA,CACjCiB,EAAM,MAAA,CAAOU,CAAC,EACVV,CAAAA,CAAM,IAAA,GAAS,GAAGjB,CAAAA,CAAQ,IAAA,CAAK,OAAOE,CAAG,EAEjD,CACF,CAAC,CAAA","file":"relations.js","sourcesContent":["// aiecsjs/relations — entity-to-entity relations (experimental in 0.1).\n\nimport { packEid, registerRelationsCleanup } from './internal/entity.js'\nimport type { EntityId, Relation, RelationStorage, World, WorldState } from './internal/types.js'\nimport { getWorldState } from './internal/world.js'\n\nlet nextRelationId = 1\n\nexport function defineRelation<T = void>(options?: { exclusive?: boolean }): Relation<T> {\n const id = nextRelationId++\n return {\n __kind: 'relation',\n __id: id,\n __exclusive: options?.exclusive ?? false,\n __hasData: false,\n } as Relation<T>\n}\n\nexport const ChildOf: Relation = defineRelation({ exclusive: true })\n\nfunction getOrCreateStorage(state: WorldState, rel: Relation<unknown>): RelationStorage {\n let storage = state.relationStorage.get(rel.__id)\n if (storage) return storage\n storage = {\n rel,\n exclusive: rel.__exclusive ? new Int32Array(state.capacity).fill(-1) : null,\n incoming: rel.__exclusive ? new Map<number, Set<number>>() : null,\n outgoing: new Map<number, number[]>(),\n data: new Map<number, Map<number, unknown>>(),\n }\n state.relationStorage.set(rel.__id, storage)\n return storage\n}\n\n// --- Exclusive reverse-index (incoming) helpers ---\n// `incoming` mirrors the `exclusive` array: incoming.get(tgt) is the set of\n// source slots whose exclusive[src] === tgt. Every place that writes a non-`-1`\n// value into `exclusive` must call `linkIncoming`, and every place that clears a\n// slot (sets it back to -1) must call `unlinkIncoming`, so the reverse index\n// never drifts from the forward array.\n\nfunction linkIncoming(storage: RelationStorage, src: number, tgt: number): void {\n const incoming = storage.incoming\n if (!incoming) return\n let set = incoming.get(tgt)\n if (!set) {\n set = new Set<number>()\n incoming.set(tgt, set)\n }\n set.add(src)\n}\n\nfunction unlinkIncoming(storage: RelationStorage, src: number, tgt: number): void {\n const incoming = storage.incoming\n if (!incoming) return\n const set = incoming.get(tgt)\n if (!set) return\n set.delete(src)\n if (set.size === 0) incoming.delete(tgt)\n}\n\nexport function addRelation<T>(\n world: World,\n source: EntityId,\n rel: Relation<T>,\n target: EntityId,\n data?: T,\n): void {\n const state = getWorldState(world)\n if (state.readOnly) throw new Error('aiecsjs: cannot mutate a read-only world')\n const storage = getOrCreateStorage(state, rel as Relation<unknown>)\n // Use raw idx as keys in relation storage so slot reuse invalidation is consistent\n const src = (source as number) & state.options.indexMask\n const tgt = (target as number) & state.options.indexMask\n\n if (storage.exclusive) {\n if (src >= storage.exclusive.length) {\n const next = new Int32Array(Math.max(src + 1, storage.exclusive.length * 2)).fill(-1)\n next.set(storage.exclusive)\n storage.exclusive = next\n }\n // Exclusive redirect: if this source already pointed at a different target,\n // drop that previous target's data entry so getRelationData stays consistent\n // with getRelationTargets, which reports only the current exclusive target.\n const prevTgt = storage.exclusive[src] ?? -1\n if (prevTgt >= 0 && prevTgt !== tgt) {\n const prevInner = storage.data.get(src)\n prevInner?.delete(prevTgt)\n if (prevInner && prevInner.size === 0) storage.data.delete(src)\n unlinkIncoming(storage, src, prevTgt)\n }\n storage.exclusive[src] = tgt\n if (prevTgt !== tgt) linkIncoming(storage, src, tgt)\n } else {\n let list = storage.outgoing.get(src)\n if (!list) {\n list = []\n storage.outgoing.set(src, list)\n }\n if (!list.includes(tgt)) list.push(tgt)\n }\n\n if (data !== undefined) {\n let inner = storage.data.get(src)\n if (!inner) {\n inner = new Map<number, unknown>()\n storage.data.set(src, inner)\n }\n inner.set(tgt, data)\n }\n}\n\nexport function removeRelation(\n world: World,\n source: EntityId,\n rel: Relation,\n target: EntityId,\n): void {\n const state = getWorldState(world)\n if (state.readOnly) throw new Error('aiecsjs: cannot mutate a read-only world')\n const storage = state.relationStorage.get(rel.__id)\n if (!storage) return\n const src = (source as number) & state.options.indexMask\n const tgt = (target as number) & state.options.indexMask\n if (storage.exclusive) {\n if (src < storage.exclusive.length && storage.exclusive[src] === tgt) {\n storage.exclusive[src] = -1\n unlinkIncoming(storage, src, tgt)\n }\n } else {\n const list = storage.outgoing.get(src)\n if (list) {\n const idx = list.indexOf(tgt)\n if (idx >= 0) list.splice(idx, 1)\n if (list.length === 0) storage.outgoing.delete(src)\n }\n }\n const inner = storage.data.get(src)\n if (inner) {\n inner.delete(tgt)\n if (inner.size === 0) storage.data.delete(src)\n }\n}\n\nexport function getRelationTargets(\n world: World,\n source: EntityId,\n rel: Relation,\n): readonly EntityId[] {\n const state = getWorldState(world)\n const storage = state.relationStorage.get(rel.__id)\n if (!storage) return []\n const src = (source as number) & state.options.indexMask\n if (storage.exclusive) {\n if (src < storage.exclusive.length) {\n const tgt = storage.exclusive[src] ?? -1\n if (tgt >= 0) {\n const gen = state.generations[tgt] ?? 0\n return [packEid(tgt, gen, state.options)]\n }\n }\n return []\n }\n const list = storage.outgoing.get(src)\n if (!list) return []\n return list.map((tIdx) => {\n const gen = state.generations[tIdx] ?? 0\n return packEid(tIdx, gen, state.options)\n })\n}\n\n/**\n * Read the data payload attached to a relation edge.\n *\n * Returns the `data` value that was supplied to {@link addRelation} for the\n * `(source, rel, target)` triple, or `undefined` when:\n * - the relation has never been stored for this world,\n * - no edge from `source` to `target` via `rel` exists, or\n * - the edge was added without a data argument.\n *\n * **Slot-keying semantic (ABA caveat):** relation storage keys edges by raw\n * entity slot index (`entityId & indexMask`), not by the full packed EntityId\n * that includes the generation counter. This means that if entity A is\n * destroyed and a *different* entity B is later created in the same slot, B\n * will inherit any edges that A had — unless the destroy cleanup hook ran\n * (which it does when `destroyEntity` is called). Callers that cache a\n * source/target EntityId should validate liveness with `entityExists` before\n * calling `getRelationData` if slot recycling is a concern.\n */\nexport function getRelationData<T>(\n world: World,\n source: EntityId,\n rel: Relation<T>,\n target: EntityId,\n): T | undefined {\n const state = getWorldState(world)\n const storage = state.relationStorage.get(rel.__id)\n if (!storage) return undefined\n const src = (source as number) & state.options.indexMask\n const tgt = (target as number) & state.options.indexMask\n return storage.data.get(src)?.get(tgt) as T | undefined\n}\n\n// Cleanup hook: when an entity is destroyed, remove all relations involving it.\nregisterRelationsCleanup((state: WorldState, eid: EntityId) => {\n const e = (eid as number) & state.options.indexMask\n for (const storage of state.relationStorage.values()) {\n if (storage.exclusive) {\n // `e` as a SOURCE: clear its own exclusive slot and unlink it from whatever\n // target it currently points at, so the reverse index stays consistent.\n if (e < storage.exclusive.length) {\n const cur = storage.exclusive[e] ?? -1\n if (cur >= 0) unlinkIncoming(storage, e, cur)\n storage.exclusive[e] = -1\n }\n // `e` as a TARGET: clear every source that points at it. Use the reverse\n // index to touch only the actual incoming sources (O(incoming)) instead of\n // scanning the entire `exclusive` capacity (O(capacity)) — the bounded-cost\n // path for large sparse relation tables. Behaviour is identical to the old\n // full scan: every src with exclusive[src] === e is reset to -1.\n const incomingSet = storage.incoming?.get(e)\n if (incomingSet) {\n for (const src of incomingSet) {\n if (src < storage.exclusive.length) storage.exclusive[src] = -1\n }\n storage.incoming?.delete(e)\n }\n }\n storage.outgoing.delete(e)\n for (const [src, list] of storage.outgoing) {\n const idx = list.indexOf(e)\n if (idx >= 0) {\n list.splice(idx, 1)\n if (list.length === 0) storage.outgoing.delete(src)\n }\n }\n // Clear data entries involving this entity. The outer key is the source\n // eid; entries whose source IS this entity drop entirely. Entries that\n // reference this entity as a target are pruned from each inner map.\n storage.data.delete(e)\n for (const [src, inner] of storage.data) {\n inner.delete(e)\n if (inner.size === 0) storage.data.delete(src)\n }\n }\n})\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/relations.ts"],"names":["defineRelation","options","ids","shared","ChildOf","storageOf","state","rel","EcsError","getOrCreateStorage","storage","linkIncoming","src","tgt","incoming","set","unlinkIncoming","addRelation","world","source","target","data","getWritableState","isAliveInternal","next","prevTgt","prevInner","list","inner","removeRelation","idx","getRelationTargets","getWorldState","gen","packEid","tIdx","getRelationData","registerRelationsCleanup","eid","e","cur","incomingSet"],"mappings":"uEAQO,SAASA,EAAyBC,CAAAA,CAAgD,CAEvF,OAAO,CACL,MAAA,CAAQ,WACR,IAAA,CAHSC,CAAAA,CAAI,WAIb,WAAA,CAAaD,CAAAA,EAAS,WAAa,KAAA,CACnC,SAAA,CAAW,KACb,CACF,CAIAE,IAAO,OAAA,GAAYH,CAAAA,CAAe,CAAE,SAAA,CAAW,IAAK,CAAC,CAAA,CAC9C,IAAMI,EAAoBD,GAAAA,CAAO,QAIxC,SAASE,CAAAA,CAAUC,CAAAA,CAAmBC,EAAqD,CACzF,GAAIA,GAAK,MAAA,GAAW,UAAA,CAAY,MAAM,IAAIC,GAAAA,CAAS,iCAAiC,CAAA,CACpF,OAAOF,EAAM,eAAA,CAAgB,GAAA,CAAIC,EAAI,IAAI,CAC3C,CAEA,SAASE,CAAAA,CAAmBH,EAAmBC,CAAAA,CAAyC,CACtF,I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AAU,SAAQA,CAAAA,CAAQ,SAAA,CAAUE,CAAG,CAAA,CAAI,EAAA,CAAA,CAE/DF,EAAQ,QAAA,EAAU,MAAA,CAAO6B,CAAC,EAC5B,CACF,CACA7B,CAAAA,CAAQ,QAAA,CAAS,OAAO6B,CAAC,CAAA,CACzB,OAAW,CAAC3B,CAAAA,CAAKe,CAAI,CAAA,GAAKjB,CAAAA,CAAQ,SAAU,CAC1C,IAAMoB,EAAMH,CAAAA,CAAK,OAAA,CAAQY,CAAC,CAAA,CACtBT,CAAAA,EAAO,IACTH,CAAAA,CAAK,MAAA,CAAOG,EAAK,CAAC,CAAA,CACdH,EAAK,MAAA,GAAW,CAAA,EAAGjB,EAAQ,QAAA,CAAS,MAAA,CAAOE,CAAG,CAAA,EAEtD,CAIAF,EAAQ,IAAA,CAAK,MAAA,CAAO6B,CAAC,CAAA,CACrB,IAAA,GAAW,CAAC3B,CAAAA,CAAKgB,CAAK,IAAKlB,CAAAA,CAAQ,IAAA,CACjCkB,EAAM,MAAA,CAAOW,CAAC,EACVX,CAAAA,CAAM,IAAA,GAAS,GAAGlB,CAAAA,CAAQ,IAAA,CAAK,OAAOE,CAAG,EAEjD,CACF,CAAC,CAAA","file":"relations.js","sourcesContent":["// aiecsjs/relations — entity-to-entity relations (experimental in 0.1).\n\nimport { isAliveInternal, packEid, registerRelationsCleanup } from './internal/entity.js'\nimport { EcsError } from './internal/errors.js'\nimport { ids, shared } from './internal/registry.js'\nimport type { EntityId, Relation, RelationStorage, World, WorldState } from './internal/types.js'\nimport { getWorldState, getWritableState } from './internal/world.js'\n\nexport function defineRelation<T = void>(options?: { exclusive?: boolean }): Relation<T> {\n const id = ids.relation++\n return {\n __kind: 'relation',\n __id: id,\n __exclusive: options?.exclusive ?? false,\n __hasData: false,\n } as Relation<T>\n}\n\n// One ChildOf for every loaded copy of the package (see registry.ts), so a\n// hierarchy built through one copy is visible through another.\nshared.childOf ??= defineRelation({ exclusive: true })\nexport const ChildOf: Relation = shared.childOf\n\n// The relation's storage in this world (undefined until its first edge).\n// Rejects a value that is not a `defineRelation` handle.\nfunction storageOf(state: WorldState, rel: Relation<unknown>): RelationStorage | undefined {\n if (rel?.__kind !== 'relation') throw new EcsError('aiecsjs: rel must be a relation')\n return state.relationStorage.get(rel.__id)\n}\n\nfunction getOrCreateStorage(state: WorldState, rel: Relation<unknown>): RelationStorage {\n let storage = storageOf(state, rel)\n if (storage) return storage\n storage = {\n rel,\n exclusive: rel.__exclusive ? new Int32Array(state.capacity).fill(-1) : null,\n incoming: rel.__exclusive ? new Map<number, Set<number>>() : null,\n outgoing: new Map<number, number[]>(),\n data: new Map<number, Map<number, unknown>>(),\n }\n state.relationStorage.set(rel.__id, storage)\n return storage\n}\n\n// --- Exclusive reverse-index (incoming) helpers ---\n// `incoming` mirrors the `exclusive` array: incoming.get(tgt) is the set of\n// source slots whose exclusive[src] === tgt. Every place that writes a non-`-1`\n// value into `exclusive` must call `linkIncoming`, and every place that clears a\n// slot (sets it back to -1) must call `unlinkIncoming`, so the reverse index\n// never drifts from the forward array.\n\nfunction linkIncoming(storage: RelationStorage, src: number, tgt: number): void {\n const incoming = storage.incoming\n if (!incoming) return\n let set = incoming.get(tgt)\n if (!set) {\n set = new Set<number>()\n incoming.set(tgt, set)\n }\n set.add(src)\n}\n\nfunction unlinkIncoming(storage: RelationStorage, src: number, tgt: number): void {\n const incoming = storage.incoming\n if (!incoming) return\n const set = incoming.get(tgt)\n if (!set) return\n set.delete(src)\n if (set.size === 0) incoming.delete(tgt)\n}\n\n/**\n * Add a relation edge from `source` to `target` (replacing `source`'s current\n * target for an exclusive relation such as `ChildOf`), optionally with a data\n * payload read back by {@link getRelationData}.\n *\n * Throws `EcsError` when `source` or `target` is not alive, mirroring\n * `addComponent`: edges are stored by slot, so an edge to a dead entity would\n * otherwise be inherited by whatever entity is later recycled into that slot.\n * Check `entityExists` first when an endpoint may be stale.\n */\nexport function addRelation<T>(\n world: World,\n source: EntityId,\n rel: Relation<T>,\n target: EntityId,\n data?: T,\n): void {\n const state = getWritableState(world)\n if (!isAliveInternal(state, source)) {\n throw new EcsError(`aiecsjs: addRelation on dead source entity ${source}`)\n }\n if (!isAliveInternal(state, target)) {\n throw new EcsError(`aiecsjs: addRelation on dead target entity ${target}`)\n }\n const storage = getOrCreateStorage(state, rel as Relation<unknown>)\n // Use raw idx as keys in relation storage so slot reuse invalidation is consistent\n const src = (source as number) & state.options.indexMask\n const tgt = (target as number) & state.options.indexMask\n\n if (storage.exclusive) {\n if (src >= storage.exclusive.length) {\n const next = new Int32Array(Math.max(src + 1, storage.exclusive.length * 2)).fill(-1)\n next.set(storage.exclusive)\n storage.exclusive = next\n }\n // Exclusive redirect: if this source already pointed at a different target,\n // drop that previous target's data entry so getRelationData stays consistent\n // with getRelationTargets, which reports only the current exclusive target.\n const prevTgt = storage.exclusive[src] ?? -1\n if (prevTgt >= 0 && prevTgt !== tgt) {\n const prevInner = storage.data.get(src)\n prevInner?.delete(prevTgt)\n if (prevInner && prevInner.size === 0) storage.data.delete(src)\n unlinkIncoming(storage, src, prevTgt)\n }\n storage.exclusive[src] = tgt\n if (prevTgt !== tgt) linkIncoming(storage, src, tgt)\n } else {\n let list = storage.outgoing.get(src)\n if (!list) {\n list = []\n storage.outgoing.set(src, list)\n }\n if (!list.includes(tgt)) list.push(tgt)\n }\n\n if (data !== undefined) {\n let inner = storage.data.get(src)\n if (!inner) {\n inner = new Map<number, unknown>()\n storage.data.set(src, inner)\n }\n inner.set(tgt, data)\n }\n}\n\nexport function removeRelation(\n world: World,\n source: EntityId,\n rel: Relation,\n target: EntityId,\n): void {\n const state = getWritableState(world)\n const storage = storageOf(state, rel as Relation<unknown>)\n if (!storage) return\n const src = (source as number) & state.options.indexMask\n const tgt = (target as number) & state.options.indexMask\n if (storage.exclusive) {\n if (src < storage.exclusive.length && storage.exclusive[src] === tgt) {\n storage.exclusive[src] = -1\n unlinkIncoming(storage, src, tgt)\n }\n } else {\n const list = storage.outgoing.get(src)\n if (list) {\n const idx = list.indexOf(tgt)\n if (idx >= 0) list.splice(idx, 1)\n if (list.length === 0) storage.outgoing.delete(src)\n }\n }\n const inner = storage.data.get(src)\n if (inner) {\n inner.delete(tgt)\n if (inner.size === 0) storage.data.delete(src)\n }\n}\n\nexport function getRelationTargets(\n world: World,\n source: EntityId,\n rel: Relation,\n): readonly EntityId[] {\n const state = getWorldState(world)\n const storage = storageOf(state, rel as Relation<unknown>)\n if (!storage) return []\n const src = (source as number) & state.options.indexMask\n if (storage.exclusive) {\n if (src < storage.exclusive.length) {\n const tgt = storage.exclusive[src] ?? -1\n if (tgt >= 0) {\n const gen = state.generations[tgt] ?? 0\n return [packEid(tgt, gen, state.options)]\n }\n }\n return []\n }\n const list = storage.outgoing.get(src)\n if (!list) return []\n return list.map((tIdx) => {\n const gen = state.generations[tIdx] ?? 0\n return packEid(tIdx, gen, state.options)\n })\n}\n\n/**\n * Read the data payload attached to a relation edge.\n *\n * Returns the `data` value that was supplied to {@link addRelation} for the\n * `(source, rel, target)` triple, or `undefined` when:\n * - the relation has never been stored for this world,\n * - no edge from `source` to `target` via `rel` exists, or\n * - the edge was added without a data argument.\n *\n * **Slot keying:** relation storage keys edges by raw entity slot index\n * (`entityId & indexMask`), not by the full packed EntityId. `addRelation`\n * rejects dead endpoints and `destroyEntity` / `resetWorld` drop every edge of\n * the entities they remove, so storage never holds an edge for a dead entity.\n * A *stale* EntityId whose slot was recycled still addresses the slot's\n * current occupant here, so validate cached ids with `entityExists` (or use\n * `EntityRef`) before reading.\n */\nexport function getRelationData<T>(\n world: World,\n source: EntityId,\n rel: Relation<T>,\n target: EntityId,\n): T | undefined {\n const state = getWorldState(world)\n const storage = storageOf(state, rel as Relation<unknown>)\n if (!storage) return undefined\n const src = (source as number) & state.options.indexMask\n const tgt = (target as number) & state.options.indexMask\n return storage.data.get(src)?.get(tgt) as T | undefined\n}\n\n// Cleanup hook: when an entity is destroyed, remove all relations involving it.\nregisterRelationsCleanup((state: WorldState, eid: EntityId) => {\n const e = (eid as number) & state.options.indexMask\n for (const storage of state.relationStorage.values()) {\n if (storage.exclusive) {\n // `e` as a SOURCE: clear its own exclusive slot and unlink it from whatever\n // target it currently points at, so the reverse index stays consistent.\n if (e < storage.exclusive.length) {\n const cur = storage.exclusive[e] ?? -1\n if (cur >= 0) unlinkIncoming(storage, e, cur)\n storage.exclusive[e] = -1\n }\n // `e` as a TARGET: clear every source that points at it. Use the reverse\n // index to touch only the actual incoming sources (O(incoming)) instead of\n // scanning the entire `exclusive` capacity (O(capacity)) — the bounded-cost\n // path for large sparse relation tables. Behaviour is identical to the old\n // full scan: every src with exclusive[src] === e is reset to -1.\n const incomingSet = storage.incoming?.get(e)\n if (incomingSet) {\n for (const src of incomingSet) {\n if (src < storage.exclusive.length) storage.exclusive[src] = -1\n }\n storage.incoming?.delete(e)\n }\n }\n storage.outgoing.delete(e)\n for (const [src, list] of storage.outgoing) {\n const idx = list.indexOf(e)\n if (idx >= 0) {\n list.splice(idx, 1)\n if (list.length === 0) storage.outgoing.delete(src)\n }\n }\n // Clear data entries involving this entity. The outer key is the source\n // eid; entries whose source IS this entity drop entirely. Entries that\n // reference this entity as a target are pruned from each inner map.\n storage.data.delete(e)\n for (const [src, inner] of storage.data) {\n inner.delete(e)\n if (inner.size === 0) storage.data.delete(src)\n }\n }\n})\n"]}
|
package/dist/serialize.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
'use strict';var
|
|
1
|
+
'use strict';var chunkMVBFYRHY_cjs=require('./chunk-MVBFYRHY.cjs');require('./chunk-NPNWRCB5.cjs');Object.defineProperty(exports,"createDeltaSerializer",{enumerable:true,get:function(){return chunkMVBFYRHY_cjs.e}});Object.defineProperty(exports,"deserializeWorld",{enumerable:true,get:function(){return chunkMVBFYRHY_cjs.b}});Object.defineProperty(exports,"fromJSON",{enumerable:true,get:function(){return chunkMVBFYRHY_cjs.d}});Object.defineProperty(exports,"serializeWorld",{enumerable:true,get:function(){return chunkMVBFYRHY_cjs.a}});Object.defineProperty(exports,"toJSON",{enumerable:true,get:function(){return chunkMVBFYRHY_cjs.c}});//# sourceMappingURL=serialize.cjs.map
|
|
2
2
|
//# sourceMappingURL=serialize.cjs.map
|
package/dist/serialize.d.cts
CHANGED
|
@@ -1,16 +1,41 @@
|
|
|
1
|
-
import { W as World, S as SerializeOptions, D as DeltaSerializer, e as DeserializeOptions, p as WorldSnapshot } from './types-
|
|
1
|
+
import { W as World, S as SerializeOptions, D as DeltaSerializer, e as DeserializeOptions, p as WorldSnapshot } from './types-C6TkQ7HW.cjs';
|
|
2
2
|
|
|
3
3
|
declare function serializeWorld(world: World, options?: SerializeOptions): Uint8Array;
|
|
4
|
+
/**
|
|
5
|
+
* Restore a world from `serializeWorld` bytes. Same resolution rules and
|
|
6
|
+
* options as {@link fromJSON}; the binary header's format version is checked
|
|
7
|
+
* against `onUnknownVersion` first.
|
|
8
|
+
*/
|
|
4
9
|
declare function deserializeWorld(bytes: Uint8Array, options?: DeserializeOptions): World;
|
|
5
10
|
/**
|
|
6
|
-
* Serialize a world snapshot to a plain JSON-compatible object.
|
|
11
|
+
* Serialize a world snapshot to a plain JSON-compatible object (format 2).
|
|
12
|
+
*
|
|
13
|
+
* `components` lists every component the entities reference with its stable
|
|
14
|
+
* `key` (from `defineComponent(..., { key })`), kind and SoA fields, so a
|
|
15
|
+
* loader can match data to components by key rather than by creation order.
|
|
16
|
+
*
|
|
17
|
+
* AoS component data is deep-copied (`structuredClone`), so the snapshot never
|
|
18
|
+
* aliases live component instances; AoS values must therefore be cloneable.
|
|
7
19
|
*
|
|
8
20
|
* Note: `EntityRef` is in-memory only — not preserved across serialize/deserialize.
|
|
9
|
-
*
|
|
10
|
-
*
|
|
21
|
+
* `fromJSON` / `deserializeWorld` re-create entities in snapshot order with
|
|
22
|
+
* fresh ids (generation 0, holes in the slot range closed up), so stale refs
|
|
23
|
+
* deref to null and EntityIds stored inside component data are not remapped.
|
|
11
24
|
*/
|
|
12
25
|
declare function toJSON(world: World): WorldSnapshot;
|
|
13
|
-
|
|
26
|
+
/**
|
|
27
|
+
* Restore a world from a `toJSON` snapshot.
|
|
28
|
+
*
|
|
29
|
+
* Every component the snapshot references is resolved before the world is
|
|
30
|
+
* created: by `key` for keyed components, by creation-order id otherwise. A
|
|
31
|
+
* component this process has not defined throws `EcsError` unless
|
|
32
|
+
* `onUnknownComponent: 'skip'`; a kind or SoA-field mismatch always throws
|
|
33
|
+
* `EcsError`. A snapshot without `formatVersion: 2` (every 0.5.x snapshot)
|
|
34
|
+
* throws `EcsError` unless `onUnknownVersion: 'best-effort'`, which loads it by
|
|
35
|
+
* id with a kind check only. `options.components` restricts which components
|
|
36
|
+
* are loaded.
|
|
37
|
+
*/
|
|
38
|
+
declare function fromJSON(snapshot: WorldSnapshot, options?: DeserializeOptions): World;
|
|
14
39
|
declare function createDeltaSerializer(world: World, options?: SerializeOptions): DeltaSerializer;
|
|
15
40
|
|
|
16
41
|
export { createDeltaSerializer, deserializeWorld, fromJSON, serializeWorld, toJSON };
|
package/dist/serialize.d.ts
CHANGED
|
@@ -1,16 +1,41 @@
|
|
|
1
|
-
import { W as World, S as SerializeOptions, D as DeltaSerializer, e as DeserializeOptions, p as WorldSnapshot } from './types-
|
|
1
|
+
import { W as World, S as SerializeOptions, D as DeltaSerializer, e as DeserializeOptions, p as WorldSnapshot } from './types-C6TkQ7HW.js';
|
|
2
2
|
|
|
3
3
|
declare function serializeWorld(world: World, options?: SerializeOptions): Uint8Array;
|
|
4
|
+
/**
|
|
5
|
+
* Restore a world from `serializeWorld` bytes. Same resolution rules and
|
|
6
|
+
* options as {@link fromJSON}; the binary header's format version is checked
|
|
7
|
+
* against `onUnknownVersion` first.
|
|
8
|
+
*/
|
|
4
9
|
declare function deserializeWorld(bytes: Uint8Array, options?: DeserializeOptions): World;
|
|
5
10
|
/**
|
|
6
|
-
* Serialize a world snapshot to a plain JSON-compatible object.
|
|
11
|
+
* Serialize a world snapshot to a plain JSON-compatible object (format 2).
|
|
12
|
+
*
|
|
13
|
+
* `components` lists every component the entities reference with its stable
|
|
14
|
+
* `key` (from `defineComponent(..., { key })`), kind and SoA fields, so a
|
|
15
|
+
* loader can match data to components by key rather than by creation order.
|
|
16
|
+
*
|
|
17
|
+
* AoS component data is deep-copied (`structuredClone`), so the snapshot never
|
|
18
|
+
* aliases live component instances; AoS values must therefore be cloneable.
|
|
7
19
|
*
|
|
8
20
|
* Note: `EntityRef` is in-memory only — not preserved across serialize/deserialize.
|
|
9
|
-
*
|
|
10
|
-
*
|
|
21
|
+
* `fromJSON` / `deserializeWorld` re-create entities in snapshot order with
|
|
22
|
+
* fresh ids (generation 0, holes in the slot range closed up), so stale refs
|
|
23
|
+
* deref to null and EntityIds stored inside component data are not remapped.
|
|
11
24
|
*/
|
|
12
25
|
declare function toJSON(world: World): WorldSnapshot;
|
|
13
|
-
|
|
26
|
+
/**
|
|
27
|
+
* Restore a world from a `toJSON` snapshot.
|
|
28
|
+
*
|
|
29
|
+
* Every component the snapshot references is resolved before the world is
|
|
30
|
+
* created: by `key` for keyed components, by creation-order id otherwise. A
|
|
31
|
+
* component this process has not defined throws `EcsError` unless
|
|
32
|
+
* `onUnknownComponent: 'skip'`; a kind or SoA-field mismatch always throws
|
|
33
|
+
* `EcsError`. A snapshot without `formatVersion: 2` (every 0.5.x snapshot)
|
|
34
|
+
* throws `EcsError` unless `onUnknownVersion: 'best-effort'`, which loads it by
|
|
35
|
+
* id with a kind check only. `options.components` restricts which components
|
|
36
|
+
* are loaded.
|
|
37
|
+
*/
|
|
38
|
+
declare function fromJSON(snapshot: WorldSnapshot, options?: DeserializeOptions): World;
|
|
14
39
|
declare function createDeltaSerializer(world: World, options?: SerializeOptions): DeltaSerializer;
|
|
15
40
|
|
|
16
41
|
export { createDeltaSerializer, deserializeWorld, fromJSON, serializeWorld, toJSON };
|
package/dist/serialize.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export{e as createDeltaSerializer,b as deserializeWorld,d as fromJSON,a as serializeWorld,c as toJSON}from'./chunk-
|
|
1
|
+
export{e as createDeltaSerializer,b as deserializeWorld,d as fromJSON,a as serializeWorld,c as toJSON}from'./chunk-GJ6U2TIU.js';import'./chunk-27JX7WHY.js';//# sourceMappingURL=serialize.js.map
|
|
2
2
|
//# sourceMappingURL=serialize.js.map
|
|
@@ -106,11 +106,44 @@ interface SerializeOptions {
|
|
|
106
106
|
}
|
|
107
107
|
interface DeserializeOptions {
|
|
108
108
|
components?: ComponentLike[];
|
|
109
|
+
/**
|
|
110
|
+
* `'throw'` (default) rejects a snapshot whose format version is not 2 —
|
|
111
|
+
* including every 0.5.x snapshot — with `EcsError`. `'best-effort'` loads it
|
|
112
|
+
* anyway; a snapshot without a format 2 component table is then resolved by
|
|
113
|
+
* creation-order id with a `kind` check only (0.5.x behaviour).
|
|
114
|
+
*/
|
|
109
115
|
onUnknownVersion?: 'throw' | 'best-effort';
|
|
116
|
+
/**
|
|
117
|
+
* `'throw'` (default) rejects a snapshot that references a component this
|
|
118
|
+
* process has not defined with `EcsError`; `'skip'` drops that component's
|
|
119
|
+
* data and loads the rest.
|
|
120
|
+
*/
|
|
121
|
+
onUnknownComponent?: 'throw' | 'skip';
|
|
110
122
|
}
|
|
111
123
|
interface WorldSnapshot {
|
|
124
|
+
/** Snapshot format; 0.6.0 writes 2. 0.5.x snapshots have no `formatVersion`. */
|
|
125
|
+
formatVersion: 2;
|
|
112
126
|
version: string;
|
|
113
127
|
capacity: number;
|
|
128
|
+
maxEntities?: number;
|
|
129
|
+
indexBits?: number;
|
|
130
|
+
generationBits?: number;
|
|
131
|
+
/**
|
|
132
|
+
* One entry per component referenced by `entities`: the source process's
|
|
133
|
+
* component `id`, its stable `key` (null when keyless), its `kind`, and for
|
|
134
|
+
* SoA its fields in declaration order. A loader resolves each entry by `key`
|
|
135
|
+
* (by `id` when keyless) and rejects a kind or field mismatch.
|
|
136
|
+
*/
|
|
137
|
+
components: Array<{
|
|
138
|
+
id: number;
|
|
139
|
+
key: string | null;
|
|
140
|
+
kind: 'soa' | 'aos' | 'tag';
|
|
141
|
+
fields: Array<{
|
|
142
|
+
name: string;
|
|
143
|
+
type: SoAFieldType;
|
|
144
|
+
vectorLen: number;
|
|
145
|
+
}> | null;
|
|
146
|
+
}>;
|
|
114
147
|
entities: Array<{
|
|
115
148
|
eid: number;
|
|
116
149
|
components: Array<{
|
|
@@ -106,11 +106,44 @@ interface SerializeOptions {
|
|
|
106
106
|
}
|
|
107
107
|
interface DeserializeOptions {
|
|
108
108
|
components?: ComponentLike[];
|
|
109
|
+
/**
|
|
110
|
+
* `'throw'` (default) rejects a snapshot whose format version is not 2 —
|
|
111
|
+
* including every 0.5.x snapshot — with `EcsError`. `'best-effort'` loads it
|
|
112
|
+
* anyway; a snapshot without a format 2 component table is then resolved by
|
|
113
|
+
* creation-order id with a `kind` check only (0.5.x behaviour).
|
|
114
|
+
*/
|
|
109
115
|
onUnknownVersion?: 'throw' | 'best-effort';
|
|
116
|
+
/**
|
|
117
|
+
* `'throw'` (default) rejects a snapshot that references a component this
|
|
118
|
+
* process has not defined with `EcsError`; `'skip'` drops that component's
|
|
119
|
+
* data and loads the rest.
|
|
120
|
+
*/
|
|
121
|
+
onUnknownComponent?: 'throw' | 'skip';
|
|
110
122
|
}
|
|
111
123
|
interface WorldSnapshot {
|
|
124
|
+
/** Snapshot format; 0.6.0 writes 2. 0.5.x snapshots have no `formatVersion`. */
|
|
125
|
+
formatVersion: 2;
|
|
112
126
|
version: string;
|
|
113
127
|
capacity: number;
|
|
128
|
+
maxEntities?: number;
|
|
129
|
+
indexBits?: number;
|
|
130
|
+
generationBits?: number;
|
|
131
|
+
/**
|
|
132
|
+
* One entry per component referenced by `entities`: the source process's
|
|
133
|
+
* component `id`, its stable `key` (null when keyless), its `kind`, and for
|
|
134
|
+
* SoA its fields in declaration order. A loader resolves each entry by `key`
|
|
135
|
+
* (by `id` when keyless) and rejects a kind or field mismatch.
|
|
136
|
+
*/
|
|
137
|
+
components: Array<{
|
|
138
|
+
id: number;
|
|
139
|
+
key: string | null;
|
|
140
|
+
kind: 'soa' | 'aos' | 'tag';
|
|
141
|
+
fields: Array<{
|
|
142
|
+
name: string;
|
|
143
|
+
type: SoAFieldType;
|
|
144
|
+
vectorLen: number;
|
|
145
|
+
}> | null;
|
|
146
|
+
}>;
|
|
114
147
|
entities: Array<{
|
|
115
148
|
eid: number;
|
|
116
149
|
components: Array<{
|
package/dist/worker.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
'use strict';var
|
|
1
|
+
'use strict';var chunkMVBFYRHY_cjs=require('./chunk-MVBFYRHY.cjs'),chunkNPNWRCB5_cjs=require('./chunk-NPNWRCB5.cjs');var c=1095320899;function S(e){let r=chunkNPNWRCB5_cjs.i(e),o=chunkMVBFYRHY_cjs.a(e),a=typeof SharedArrayBuffer>"u"?new ArrayBuffer(o.byteLength):new SharedArrayBuffer(o.byteLength);return new Uint8Array(a).set(o),{buffer:a,meta:p(r)}}function b(e,r){return m(e?.meta,r),chunkMVBFYRHY_cjs.b(new Uint8Array(e.buffer),r)}function B(e,r){let o=chunkMVBFYRHY_cjs.b(new Uint8Array(e),r);return r?.readOnly&&(chunkNPNWRCB5_cjs.i(o).readOnly=true),o}function g(e){chunkNPNWRCB5_cjs.k(e.id)&&chunkNPNWRCB5_cjs.m(e);}function p(e){let r=[];for(let o of e.componentInfoByBit)o&&r.push({id:o.id,kind:o.kind,schema:o.schema});return {magic:c,formatVersion:2,aiecsjsVersion:chunkNPNWRCB5_cjs.a,indexBits:e.options.indexBits,generationBits:e.options.generationBits,maxComponents:e.options.maxComponents,maskWordCount:e.options.maskWordCount,capacity:e.capacity,componentSchemas:r}}function m(e,r){if(e?.magic!==c)throw new chunkNPNWRCB5_cjs.f("aiecsjs: invalid snapshot meta (wrong magic)");if(e.formatVersion!==2&&r?.onUnknownVersion!=="best-effort")throw new chunkNPNWRCB5_cjs.f(`aiecsjs: format version ${e.formatVersion} not supported`)}exports.adoptSnapshot=b;exports.attachWorld=B;exports.detachWorld=g;exports.transferableSnapshot=S;//# sourceMappingURL=worker.cjs.map
|
|
2
2
|
//# sourceMappingURL=worker.cjs.map
|
package/dist/worker.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/worker.ts"],"names":["MAGIC","transferableSnapshot","world","state","getWorldState","bytes","serializeWorld","
|
|
1
|
+
{"version":3,"sources":["../src/worker.ts"],"names":["MAGIC","transferableSnapshot","world","state","getWorldState","bytes","serializeWorld","buffer","buildMeta","adoptSnapshot","snap","options","validateMeta","deserializeWorld","attachWorld","detachWorld","isWorldRegistered","destroyWorld","componentSchemas","info","VERSION","meta","EcsError"],"mappings":"qHAyBA,IAAMA,EAAQ,UAAA,CAEP,SAASC,EAAqBC,CAAAA,CAAoC,CACvE,IAAMC,CAAAA,CAAQC,mBAAAA,CAAcF,CAAK,CAAA,CAC3BG,CAAAA,CAAQC,oBAAeJ,CAAK,CAAA,CAK5BK,EACJ,OAAO,iBAAA,CAAsB,IACzB,IAAI,WAAA,CAAYF,EAAM,UAAU,CAAA,CAChC,IAAI,iBAAA,CAAkBA,CAAAA,CAAM,UAAU,CAAA,CAC5C,OAAA,IAAI,WAAWE,CAAM,CAAA,CAAE,IAAIF,CAAK,CAAA,CACzB,CAAE,MAAA,CAAAE,CAAAA,CAAQ,KAAMC,CAAAA,CAAUL,CAAK,CAAE,CAC1C,CAiBO,SAASM,CAAAA,CAAcC,CAAAA,CAA4BC,EAAqC,CAC7F,OAAAC,EAAaF,CAAAA,EAAM,IAAA,CAAMC,CAAO,CAAA,CACzBE,mBAAAA,CAAiB,IAAI,UAAA,CAAWH,CAAAA,CAAK,MAAM,CAAA,CAAGC,CAAO,CAC9D,CAgBO,SAASG,EACdP,CAAAA,CACAI,CAAAA,CACO,CACP,IAAMT,CAAAA,CAAQW,oBAAiB,IAAI,UAAA,CAAWN,CAAM,CAAA,CAAGI,CAAO,EAC9D,OAAIA,CAAAA,EAAS,WAAUP,mBAAAA,CAAcF,CAAK,EAAE,QAAA,CAAW,IAAA,CAAA,CAChDA,CACT,CAEO,SAASa,EAAYb,CAAAA,CAAoB,CAC1Cc,oBAAkBd,CAAAA,CAAM,EAAE,GAC5Be,mBAAAA,CAAaf,CAAK,EAEtB,CAEA,SAASM,EAAUL,CAAAA,CAA8B,CAC/C,IAAMe,CAAAA,CAAkD,GACxD,IAAA,IAAWC,CAAAA,IAAQhB,EAAM,kBAAA,CAClBgB,CAAAA,EACLD,EAAiB,IAAA,CAAK,CAAE,GAAIC,CAAAA,CAAK,EAAA,CAAI,KAAMA,CAAAA,CAAK,IAAA,CAAM,OAAQA,CAAAA,CAAK,MAAO,CAAC,CAAA,CAE7E,OAAO,CACL,KAAA,CAAOnB,CAAAA,CACP,cAAe,CAAA,CACf,cAAA,CAAgBoB,oBAChB,SAAA,CAAWjB,CAAAA,CAAM,QAAQ,SAAA,CACzB,cAAA,CAAgBA,EAAM,OAAA,CAAQ,cAAA,CAC9B,cAAeA,CAAAA,CAAM,OAAA,CAAQ,cAC7B,aAAA,CAAeA,CAAAA,CAAM,QAAQ,aAAA,CAC7B,QAAA,CAAUA,EAAM,QAAA,CAChB,gBAAA,CAAAe,CACF,CACF,CAEA,SAASN,CAAAA,CAAaS,CAAAA,CAAiBV,EAA+C,CACpF,GAAIU,GAAM,KAAA,GAAUrB,CAAAA,CAClB,MAAM,IAAIsB,mBAAAA,CAAS,8CAA8C,CAAA,CAEnE,GAAID,EAAK,aAAA,GAAkB,CAAA,EAAKV,GAAS,gBAAA,GAAqB,aAAA,CAC5D,MAAM,IAAIW,mBAAAA,CAAS,2BAA2BD,CAAAA,CAAK,aAAa,gBAAgB,CAEpF","file":"worker.cjs","sourcesContent":["// aiecsjs/worker — SharedArrayBuffer helpers (experimental, snapshot-copy).\n//\n// Note: 0.x implements SAB as a transferable snapshot pattern rather than true\n// shared-memory aliasing. The world is serialized into the SAB, and the worker\n// reconstructs a fresh world from those bytes via adoptSnapshot/attachWorld.\n// True shared-column memory is targeted for a future stable release.\n// The API matches the documented contract and survives postMessage cleanly.\n//\n// EntityRef is in-memory only — not preserved across worker boundaries.\n// Generation counters reset on adoptSnapshot/attachWorld. Pass `EntityRef.id`\n// (the packed EntityId) across the worker boundary only if you understand that\n// the generation portion will be stale after a round-trip snapshot.\n\nimport { EcsError } from './internal/errors.js'\nimport type {\n DeserializeOptions,\n TransferableSnapshot,\n World,\n WorldMeta,\n WorldState,\n} from './internal/types.js'\nimport { destroyWorld, getWorldState, isWorldRegistered } from './internal/world.js'\nimport { deserializeWorld, serializeWorld } from './serialize.js'\nimport { VERSION } from './version.js'\n\nconst MAGIC = 0x41494543 // 'AIEC' little-endian as uint32\n\nexport function transferableSnapshot(world: World): TransferableSnapshot {\n const state = getWorldState(world)\n const bytes = serializeWorld(world)\n // Fallback: a plain ArrayBuffer where SharedArrayBuffer is unavailable (no\n // cross-origin isolation). TransferableSnapshot.buffer is typed\n // `SharedArrayBuffer | ArrayBuffer`, so the type tells the truth instead of\n // pretending the fallback is a SAB.\n const buffer =\n typeof SharedArrayBuffer === 'undefined'\n ? new ArrayBuffer(bytes.byteLength)\n : new SharedArrayBuffer(bytes.byteLength)\n new Uint8Array(buffer).set(bytes)\n return { buffer, meta: buildMeta(state) }\n}\n\n/**\n * Adopt a snapshot previously produced by `transferableSnapshot`.\n *\n * `options` are the `deserializeWorld` options: component resolution by key,\n * `onUnknownComponent`, and `onUnknownVersion` (which also governs the meta's\n * format version).\n *\n * SECURITY: same trust expectation as `attachWorld` — the sender of the\n * `TransferableSnapshot` (typically a Web Worker) must be trusted. The\n * function runs `validateMeta` and `deserializeWorld`, which enforce magic\n * + format version + length bounds on the binary header; but the inner\n * JSON payload, once decoded, is fed to `addComponent` and reaches AoS\n * components. For untrusted senders, use `aibridgejs` + `toJSON(world)` at\n * the application boundary instead.\n */\nexport function adoptSnapshot(snap: TransferableSnapshot, options?: DeserializeOptions): World {\n validateMeta(snap?.meta, options)\n return deserializeWorld(new Uint8Array(snap.buffer), options)\n}\n\n/**\n * Adopt a SharedArrayBuffer-backed snapshot produced by `transferableSnapshot`.\n *\n * `options` takes the `deserializeWorld` options plus `readOnly`, which makes\n * every mutator (including `resetWorld`) throw `EcsError` on the result.\n *\n * SECURITY: the SAB sender must be trusted. `attachWorld` performs the same\n * magic + version + length-bounds checks as `deserializeWorld`, but the JSON\n * payload itself is deserialised into AoS components via `addComponent`. If\n * the sender is untrusted (e.g. a third-party Web Worker that you do not\n * audit), prefer the higher-level `aibridgejs` channel + `toJSON(world)`\n * pattern, which lets you validate the shape at the application boundary\n * before constructing entities.\n */\nexport function attachWorld(\n buffer: SharedArrayBuffer | ArrayBuffer,\n options?: DeserializeOptions & { readOnly?: boolean },\n): World {\n const world = deserializeWorld(new Uint8Array(buffer), options)\n if (options?.readOnly) getWorldState(world).readOnly = true\n return world\n}\n\nexport function detachWorld(world: World): void {\n if (isWorldRegistered(world.id)) {\n destroyWorld(world)\n }\n}\n\nfunction buildMeta(state: WorldState): WorldMeta {\n const componentSchemas: WorldMeta['componentSchemas'] = []\n for (const info of state.componentInfoByBit) {\n if (!info) continue\n componentSchemas.push({ id: info.id, kind: info.kind, schema: info.schema })\n }\n return {\n magic: MAGIC,\n formatVersion: 2,\n aiecsjsVersion: VERSION,\n indexBits: state.options.indexBits,\n generationBits: state.options.generationBits,\n maxComponents: state.options.maxComponents,\n maskWordCount: state.options.maskWordCount,\n capacity: state.capacity,\n componentSchemas,\n }\n}\n\nfunction validateMeta(meta: WorldMeta, options: DeserializeOptions | undefined): void {\n if (meta?.magic !== MAGIC) {\n throw new EcsError('aiecsjs: invalid snapshot meta (wrong magic)')\n }\n if (meta.formatVersion !== 2 && options?.onUnknownVersion !== 'best-effort') {\n throw new EcsError(`aiecsjs: format version ${meta.formatVersion} not supported`)\n }\n}\n"]}
|
package/dist/worker.d.cts
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
import { m as TransferableSnapshot, W as World } from './types-
|
|
1
|
+
import { m as TransferableSnapshot, e as DeserializeOptions, W as World } from './types-C6TkQ7HW.cjs';
|
|
2
2
|
|
|
3
3
|
declare function transferableSnapshot(world: World): TransferableSnapshot;
|
|
4
4
|
/**
|
|
5
5
|
* Adopt a snapshot previously produced by `transferableSnapshot`.
|
|
6
6
|
*
|
|
7
|
+
* `options` are the `deserializeWorld` options: component resolution by key,
|
|
8
|
+
* `onUnknownComponent`, and `onUnknownVersion` (which also governs the meta's
|
|
9
|
+
* format version).
|
|
10
|
+
*
|
|
7
11
|
* SECURITY: same trust expectation as `attachWorld` — the sender of the
|
|
8
12
|
* `TransferableSnapshot` (typically a Web Worker) must be trusted. The
|
|
9
13
|
* function runs `validateMeta` and `deserializeWorld`, which enforce magic
|
|
@@ -12,10 +16,13 @@ declare function transferableSnapshot(world: World): TransferableSnapshot;
|
|
|
12
16
|
* components. For untrusted senders, use `aibridgejs` + `toJSON(world)` at
|
|
13
17
|
* the application boundary instead.
|
|
14
18
|
*/
|
|
15
|
-
declare function adoptSnapshot(snap: TransferableSnapshot): World;
|
|
19
|
+
declare function adoptSnapshot(snap: TransferableSnapshot, options?: DeserializeOptions): World;
|
|
16
20
|
/**
|
|
17
21
|
* Adopt a SharedArrayBuffer-backed snapshot produced by `transferableSnapshot`.
|
|
18
22
|
*
|
|
23
|
+
* `options` takes the `deserializeWorld` options plus `readOnly`, which makes
|
|
24
|
+
* every mutator (including `resetWorld`) throw `EcsError` on the result.
|
|
25
|
+
*
|
|
19
26
|
* SECURITY: the SAB sender must be trusted. `attachWorld` performs the same
|
|
20
27
|
* magic + version + length-bounds checks as `deserializeWorld`, but the JSON
|
|
21
28
|
* payload itself is deserialised into AoS components via `addComponent`. If
|
|
@@ -24,7 +31,7 @@ declare function adoptSnapshot(snap: TransferableSnapshot): World;
|
|
|
24
31
|
* pattern, which lets you validate the shape at the application boundary
|
|
25
32
|
* before constructing entities.
|
|
26
33
|
*/
|
|
27
|
-
declare function attachWorld(buffer: SharedArrayBuffer | ArrayBuffer, options?: {
|
|
34
|
+
declare function attachWorld(buffer: SharedArrayBuffer | ArrayBuffer, options?: DeserializeOptions & {
|
|
28
35
|
readOnly?: boolean;
|
|
29
36
|
}): World;
|
|
30
37
|
declare function detachWorld(world: World): void;
|
package/dist/worker.d.ts
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
import { m as TransferableSnapshot, W as World } from './types-
|
|
1
|
+
import { m as TransferableSnapshot, e as DeserializeOptions, W as World } from './types-C6TkQ7HW.js';
|
|
2
2
|
|
|
3
3
|
declare function transferableSnapshot(world: World): TransferableSnapshot;
|
|
4
4
|
/**
|
|
5
5
|
* Adopt a snapshot previously produced by `transferableSnapshot`.
|
|
6
6
|
*
|
|
7
|
+
* `options` are the `deserializeWorld` options: component resolution by key,
|
|
8
|
+
* `onUnknownComponent`, and `onUnknownVersion` (which also governs the meta's
|
|
9
|
+
* format version).
|
|
10
|
+
*
|
|
7
11
|
* SECURITY: same trust expectation as `attachWorld` — the sender of the
|
|
8
12
|
* `TransferableSnapshot` (typically a Web Worker) must be trusted. The
|
|
9
13
|
* function runs `validateMeta` and `deserializeWorld`, which enforce magic
|
|
@@ -12,10 +16,13 @@ declare function transferableSnapshot(world: World): TransferableSnapshot;
|
|
|
12
16
|
* components. For untrusted senders, use `aibridgejs` + `toJSON(world)` at
|
|
13
17
|
* the application boundary instead.
|
|
14
18
|
*/
|
|
15
|
-
declare function adoptSnapshot(snap: TransferableSnapshot): World;
|
|
19
|
+
declare function adoptSnapshot(snap: TransferableSnapshot, options?: DeserializeOptions): World;
|
|
16
20
|
/**
|
|
17
21
|
* Adopt a SharedArrayBuffer-backed snapshot produced by `transferableSnapshot`.
|
|
18
22
|
*
|
|
23
|
+
* `options` takes the `deserializeWorld` options plus `readOnly`, which makes
|
|
24
|
+
* every mutator (including `resetWorld`) throw `EcsError` on the result.
|
|
25
|
+
*
|
|
19
26
|
* SECURITY: the SAB sender must be trusted. `attachWorld` performs the same
|
|
20
27
|
* magic + version + length-bounds checks as `deserializeWorld`, but the JSON
|
|
21
28
|
* payload itself is deserialised into AoS components via `addComponent`. If
|
|
@@ -24,7 +31,7 @@ declare function adoptSnapshot(snap: TransferableSnapshot): World;
|
|
|
24
31
|
* pattern, which lets you validate the shape at the application boundary
|
|
25
32
|
* before constructing entities.
|
|
26
33
|
*/
|
|
27
|
-
declare function attachWorld(buffer: SharedArrayBuffer | ArrayBuffer, options?: {
|
|
34
|
+
declare function attachWorld(buffer: SharedArrayBuffer | ArrayBuffer, options?: DeserializeOptions & {
|
|
28
35
|
readOnly?: boolean;
|
|
29
36
|
}): World;
|
|
30
37
|
declare function detachWorld(world: World): void;
|
package/dist/worker.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {a,b as b$1}from'./chunk-
|
|
1
|
+
import {a,b as b$1}from'./chunk-GJ6U2TIU.js';import {i,k,m as m$1,a as a$1,f}from'./chunk-27JX7WHY.js';var c=1095320899;function S(e){let r=i(e),o=a(e),a$1=typeof SharedArrayBuffer>"u"?new ArrayBuffer(o.byteLength):new SharedArrayBuffer(o.byteLength);return new Uint8Array(a$1).set(o),{buffer:a$1,meta:p(r)}}function b(e,r){return m(e?.meta,r),b$1(new Uint8Array(e.buffer),r)}function B(e,r){let o=b$1(new Uint8Array(e),r);return r?.readOnly&&(i(o).readOnly=true),o}function g(e){k(e.id)&&m$1(e);}function p(e){let r=[];for(let o of e.componentInfoByBit)o&&r.push({id:o.id,kind:o.kind,schema:o.schema});return {magic:c,formatVersion:2,aiecsjsVersion:a$1,indexBits:e.options.indexBits,generationBits:e.options.generationBits,maxComponents:e.options.maxComponents,maskWordCount:e.options.maskWordCount,capacity:e.capacity,componentSchemas:r}}function m(e,r){if(e?.magic!==c)throw new f("aiecsjs: invalid snapshot meta (wrong magic)");if(e.formatVersion!==2&&r?.onUnknownVersion!=="best-effort")throw new f(`aiecsjs: format version ${e.formatVersion} not supported`)}export{b as adoptSnapshot,B as attachWorld,g as detachWorld,S as transferableSnapshot};//# sourceMappingURL=worker.js.map
|
|
2
2
|
//# sourceMappingURL=worker.js.map
|
package/dist/worker.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/worker.ts"],"names":["MAGIC","transferableSnapshot","world","state","getWorldState","bytes","serializeWorld","
|
|
1
|
+
{"version":3,"sources":["../src/worker.ts"],"names":["MAGIC","transferableSnapshot","world","state","getWorldState","bytes","serializeWorld","buffer","buildMeta","adoptSnapshot","snap","options","validateMeta","deserializeWorld","attachWorld","detachWorld","isWorldRegistered","destroyWorld","componentSchemas","info","VERSION","meta","EcsError"],"mappings":"uGAyBA,IAAMA,EAAQ,UAAA,CAEP,SAASC,EAAqBC,CAAAA,CAAoC,CACvE,IAAMC,CAAAA,CAAQC,CAAAA,CAAcF,CAAK,CAAA,CAC3BG,CAAAA,CAAQC,EAAeJ,CAAK,CAAA,CAK5BK,IACJ,OAAO,iBAAA,CAAsB,IACzB,IAAI,WAAA,CAAYF,EAAM,UAAU,CAAA,CAChC,IAAI,iBAAA,CAAkBA,CAAAA,CAAM,UAAU,CAAA,CAC5C,OAAA,IAAI,WAAWE,GAAM,CAAA,CAAE,IAAIF,CAAK,CAAA,CACzB,CAAE,MAAA,CAAAE,GAAAA,CAAQ,KAAMC,CAAAA,CAAUL,CAAK,CAAE,CAC1C,CAiBO,SAASM,CAAAA,CAAcC,CAAAA,CAA4BC,EAAqC,CAC7F,OAAAC,EAAaF,CAAAA,EAAM,IAAA,CAAMC,CAAO,CAAA,CACzBE,GAAAA,CAAiB,IAAI,UAAA,CAAWH,CAAAA,CAAK,MAAM,CAAA,CAAGC,CAAO,CAC9D,CAgBO,SAASG,EACdP,CAAAA,CACAI,CAAAA,CACO,CACP,IAAMT,CAAAA,CAAQW,IAAiB,IAAI,UAAA,CAAWN,CAAM,CAAA,CAAGI,CAAO,EAC9D,OAAIA,CAAAA,EAAS,WAAUP,CAAAA,CAAcF,CAAK,EAAE,QAAA,CAAW,IAAA,CAAA,CAChDA,CACT,CAEO,SAASa,EAAYb,CAAAA,CAAoB,CAC1Cc,EAAkBd,CAAAA,CAAM,EAAE,GAC5Be,GAAAA,CAAaf,CAAK,EAEtB,CAEA,SAASM,EAAUL,CAAAA,CAA8B,CAC/C,IAAMe,CAAAA,CAAkD,GACxD,IAAA,IAAWC,CAAAA,IAAQhB,EAAM,kBAAA,CAClBgB,CAAAA,EACLD,EAAiB,IAAA,CAAK,CAAE,GAAIC,CAAAA,CAAK,EAAA,CAAI,KAAMA,CAAAA,CAAK,IAAA,CAAM,OAAQA,CAAAA,CAAK,MAAO,CAAC,CAAA,CAE7E,OAAO,CACL,KAAA,CAAOnB,CAAAA,CACP,cAAe,CAAA,CACf,cAAA,CAAgBoB,IAChB,SAAA,CAAWjB,CAAAA,CAAM,QAAQ,SAAA,CACzB,cAAA,CAAgBA,EAAM,OAAA,CAAQ,cAAA,CAC9B,cAAeA,CAAAA,CAAM,OAAA,CAAQ,cAC7B,aAAA,CAAeA,CAAAA,CAAM,QAAQ,aAAA,CAC7B,QAAA,CAAUA,EAAM,QAAA,CAChB,gBAAA,CAAAe,CACF,CACF,CAEA,SAASN,CAAAA,CAAaS,CAAAA,CAAiBV,EAA+C,CACpF,GAAIU,GAAM,KAAA,GAAUrB,CAAAA,CAClB,MAAM,IAAIsB,CAAAA,CAAS,8CAA8C,CAAA,CAEnE,GAAID,EAAK,aAAA,GAAkB,CAAA,EAAKV,GAAS,gBAAA,GAAqB,aAAA,CAC5D,MAAM,IAAIW,CAAAA,CAAS,2BAA2BD,CAAAA,CAAK,aAAa,gBAAgB,CAEpF","file":"worker.js","sourcesContent":["// aiecsjs/worker — SharedArrayBuffer helpers (experimental, snapshot-copy).\n//\n// Note: 0.x implements SAB as a transferable snapshot pattern rather than true\n// shared-memory aliasing. The world is serialized into the SAB, and the worker\n// reconstructs a fresh world from those bytes via adoptSnapshot/attachWorld.\n// True shared-column memory is targeted for a future stable release.\n// The API matches the documented contract and survives postMessage cleanly.\n//\n// EntityRef is in-memory only — not preserved across worker boundaries.\n// Generation counters reset on adoptSnapshot/attachWorld. Pass `EntityRef.id`\n// (the packed EntityId) across the worker boundary only if you understand that\n// the generation portion will be stale after a round-trip snapshot.\n\nimport { EcsError } from './internal/errors.js'\nimport type {\n DeserializeOptions,\n TransferableSnapshot,\n World,\n WorldMeta,\n WorldState,\n} from './internal/types.js'\nimport { destroyWorld, getWorldState, isWorldRegistered } from './internal/world.js'\nimport { deserializeWorld, serializeWorld } from './serialize.js'\nimport { VERSION } from './version.js'\n\nconst MAGIC = 0x41494543 // 'AIEC' little-endian as uint32\n\nexport function transferableSnapshot(world: World): TransferableSnapshot {\n const state = getWorldState(world)\n const bytes = serializeWorld(world)\n // Fallback: a plain ArrayBuffer where SharedArrayBuffer is unavailable (no\n // cross-origin isolation). TransferableSnapshot.buffer is typed\n // `SharedArrayBuffer | ArrayBuffer`, so the type tells the truth instead of\n // pretending the fallback is a SAB.\n const buffer =\n typeof SharedArrayBuffer === 'undefined'\n ? new ArrayBuffer(bytes.byteLength)\n : new SharedArrayBuffer(bytes.byteLength)\n new Uint8Array(buffer).set(bytes)\n return { buffer, meta: buildMeta(state) }\n}\n\n/**\n * Adopt a snapshot previously produced by `transferableSnapshot`.\n *\n * `options` are the `deserializeWorld` options: component resolution by key,\n * `onUnknownComponent`, and `onUnknownVersion` (which also governs the meta's\n * format version).\n *\n * SECURITY: same trust expectation as `attachWorld` — the sender of the\n * `TransferableSnapshot` (typically a Web Worker) must be trusted. The\n * function runs `validateMeta` and `deserializeWorld`, which enforce magic\n * + format version + length bounds on the binary header; but the inner\n * JSON payload, once decoded, is fed to `addComponent` and reaches AoS\n * components. For untrusted senders, use `aibridgejs` + `toJSON(world)` at\n * the application boundary instead.\n */\nexport function adoptSnapshot(snap: TransferableSnapshot, options?: DeserializeOptions): World {\n validateMeta(snap?.meta, options)\n return deserializeWorld(new Uint8Array(snap.buffer), options)\n}\n\n/**\n * Adopt a SharedArrayBuffer-backed snapshot produced by `transferableSnapshot`.\n *\n * `options` takes the `deserializeWorld` options plus `readOnly`, which makes\n * every mutator (including `resetWorld`) throw `EcsError` on the result.\n *\n * SECURITY: the SAB sender must be trusted. `attachWorld` performs the same\n * magic + version + length-bounds checks as `deserializeWorld`, but the JSON\n * payload itself is deserialised into AoS components via `addComponent`. If\n * the sender is untrusted (e.g. a third-party Web Worker that you do not\n * audit), prefer the higher-level `aibridgejs` channel + `toJSON(world)`\n * pattern, which lets you validate the shape at the application boundary\n * before constructing entities.\n */\nexport function attachWorld(\n buffer: SharedArrayBuffer | ArrayBuffer,\n options?: DeserializeOptions & { readOnly?: boolean },\n): World {\n const world = deserializeWorld(new Uint8Array(buffer), options)\n if (options?.readOnly) getWorldState(world).readOnly = true\n return world\n}\n\nexport function detachWorld(world: World): void {\n if (isWorldRegistered(world.id)) {\n destroyWorld(world)\n }\n}\n\nfunction buildMeta(state: WorldState): WorldMeta {\n const componentSchemas: WorldMeta['componentSchemas'] = []\n for (const info of state.componentInfoByBit) {\n if (!info) continue\n componentSchemas.push({ id: info.id, kind: info.kind, schema: info.schema })\n }\n return {\n magic: MAGIC,\n formatVersion: 2,\n aiecsjsVersion: VERSION,\n indexBits: state.options.indexBits,\n generationBits: state.options.generationBits,\n maxComponents: state.options.maxComponents,\n maskWordCount: state.options.maskWordCount,\n capacity: state.capacity,\n componentSchemas,\n }\n}\n\nfunction validateMeta(meta: WorldMeta, options: DeserializeOptions | undefined): void {\n if (meta?.magic !== MAGIC) {\n throw new EcsError('aiecsjs: invalid snapshot meta (wrong magic)')\n }\n if (meta.formatVersion !== 2 && options?.onUnknownVersion !== 'best-effort') {\n throw new EcsError(`aiecsjs: format version ${meta.formatVersion} not supported`)\n }\n}\n"]}
|