@adobe/data 0.1.3 → 0.2.1

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.
Files changed (591) hide show
  1. package/.cursorrules +28 -0
  2. package/.eslintrc.cjs +48 -0
  3. package/.github/CONTRIBUTING.md +47 -0
  4. package/CODE_OF_CONDUCT.md +79 -0
  5. package/LICENSE +21 -0
  6. package/README.md +2 -2
  7. package/asconfig.json +22 -0
  8. package/assembly/index.ts +85 -0
  9. package/assembly/tsconfig.json +17 -0
  10. package/c/build_run.sh +2 -0
  11. package/c/vector_addition +0 -0
  12. package/c/vector_addition.c +125 -0
  13. package/{cache/functions/types.js → config/license.js} +0 -2
  14. package/docs/perftest.html +28 -0
  15. package/index.html +10 -0
  16. package/package.json +50 -28
  17. package/scripts/deploy-docs.sh +41 -0
  18. package/{assembly-test/assembly.test.js → src/assembly-test/assembly.test.ts} +4 -4
  19. package/src/cache/async-cache.ts +38 -0
  20. package/{cache/blob-store.test.js → src/cache/blob-store.test.ts} +41 -7
  21. package/src/cache/blob-store.ts +278 -0
  22. package/src/cache/data-cache.test.ts +61 -0
  23. package/src/cache/data-cache.ts +183 -0
  24. package/src/cache/expiring-data-cache.test.ts +81 -0
  25. package/src/cache/expiring-data-cache.ts +61 -0
  26. package/src/cache/fallback-async-cache.ts +49 -0
  27. package/src/cache/functions/async-data-function.ts +25 -0
  28. package/src/cache/functions/functions.test.ts +84 -0
  29. package/{cache/functions/get-cached.test.js → src/cache/functions/get-cached.test.ts} +24 -7
  30. package/{cache/functions/get-cached.js → src/cache/functions/get-cached.ts} +12 -6
  31. package/{cache/functions/hashing/blob-to-hash.js → src/cache/functions/hashing/blob-to-hash.ts} +20 -18
  32. package/{cache/functions/hashing/buffer-to-hash.js → src/cache/functions/hashing/buffer-to-hash.ts} +7 -7
  33. package/src/cache/functions/hashing/hashing.test.ts +95 -0
  34. package/{cache/functions/hashing/index.js → src/cache/functions/hashing/index.ts} +1 -1
  35. package/{cache/functions/hashing/json-to-hash.js → src/cache/functions/hashing/json-to-hash.ts} +3 -3
  36. package/{cache/functions/hashing/string-to-hash.js → src/cache/functions/hashing/string-to-hash.ts} +11 -10
  37. package/{cache/functions/index.js → src/cache/functions/index.ts} +2 -4
  38. package/{cache/functions/memoize.js → src/cache/functions/memoize.ts} +22 -15
  39. package/src/cache/functions/prevent-parallel-execution.ts +50 -0
  40. package/src/cache/get-persistent-cache.ts +62 -0
  41. package/{cache/index.js → src/cache/index.ts} +9 -3
  42. package/src/cache/managed-array.ts +243 -0
  43. package/{cache/managed-async-cache.browser.test.js → src/cache/managed-async-cache.browser.test.ts} +27 -24
  44. package/src/cache/managed-async-cache.ts +78 -0
  45. package/src/cache/memory-allocator.ts +176 -0
  46. package/src/cache/memory-async-cache.ts +51 -0
  47. package/src/data.ts +38 -0
  48. package/src/ecs/README.md +21 -0
  49. package/src/ecs/archetype/archetype.ts +39 -0
  50. package/src/ecs/archetype/create-archetype.test.ts +115 -0
  51. package/src/ecs/archetype/create-archetype.ts +52 -0
  52. package/src/ecs/archetype/delete-row.test.ts +110 -0
  53. package/src/ecs/archetype/delete-row.ts +37 -0
  54. package/src/ecs/archetype/index.ts +24 -0
  55. package/src/ecs/component-schemas.ts +24 -0
  56. package/src/ecs/core-components.ts +443 -0
  57. package/src/ecs/database/create-database.test.ts +745 -0
  58. package/src/ecs/database/create-database.ts +205 -0
  59. package/src/ecs/database/database.ts +81 -0
  60. package/src/ecs/database/index.ts +24 -0
  61. package/src/ecs/database/observe-dependent-value.test.ts +198 -0
  62. package/src/ecs/database/observe-dependent-value.ts +78 -0
  63. package/src/ecs/database/transactional-store/create-transactional-store.test.ts +250 -0
  64. package/src/ecs/database/transactional-store/create-transactional-store.ts +281 -0
  65. package/{ecs/ecs/ecs-types.js → src/ecs/database/transactional-store/index.ts} +1 -2
  66. package/src/ecs/database/transactional-store/transactional-store.ts +80 -0
  67. package/src/ecs/entity-location-table/create-entity-location-table.test.ts +170 -0
  68. package/src/ecs/entity-location-table/create-entity-location-table.ts +96 -0
  69. package/src/ecs/entity-location-table/entity-location-table.ts +30 -0
  70. package/{core/schema/schema.js → src/ecs/entity-location-table/entity-location.ts} +11 -11
  71. package/src/ecs/entity-location-table/index.ts +23 -0
  72. package/src/ecs/entity.ts +26 -0
  73. package/src/ecs/index.ts +26 -0
  74. package/src/ecs/store/core/core.ts +71 -0
  75. package/src/ecs/store/core/create-core.test.ts +440 -0
  76. package/src/ecs/store/core/create-core.ts +168 -0
  77. package/{cache/async-cache.js → src/ecs/store/core/index.ts} +1 -2
  78. package/src/ecs/store/create-store.test.ts +562 -0
  79. package/src/ecs/store/create-store.ts +97 -0
  80. package/src/ecs/store/index.ts +23 -0
  81. package/{types/types.js → src/ecs/store/resource-components.ts} +2 -6
  82. package/src/ecs/store/store.ts +55 -0
  83. package/src/equals-shallow.test.ts +133 -0
  84. package/src/equals-shallow.ts +37 -0
  85. package/src/equals.test.ts +175 -0
  86. package/src/equals.ts +70 -0
  87. package/src/index.ts +27 -0
  88. package/src/internal/array-buffer-like/copy.ts +469 -0
  89. package/src/internal/array-buffer-like/grow.ts +53 -0
  90. package/{core/index.js → src/internal/array-buffer-like/index.ts} +4 -4
  91. package/src/internal/array-buffer-like/is-array-buffer.ts +445 -0
  92. package/src/internal/array-buffer-like/is-shared-array-buffer.ts +445 -0
  93. package/{core/functions/is-async-generator.js → src/internal/async-generator/is-async-generator.ts} +2 -2
  94. package/{core/schema.js → src/internal/data-view-32/create-data-view-32.ts} +8 -11
  95. package/src/internal/data-view-32/data-view-32.ts +447 -0
  96. package/{ecs/action-ecs/index.js → src/internal/data-view-32/index.ts} +2 -2
  97. package/src/internal/function/memoize-factory.ts +12 -0
  98. package/src/internal/object/index.ts +23 -0
  99. package/{cache/functions/bind-functions.js → src/internal/object/map-entries.ts} +6 -11
  100. package/src/internal/promise/is-promise.ts +28 -0
  101. package/src/internal/typed-array/get-byte-size.ts +50 -0
  102. package/src/internal/typed-array/index.ts +24 -0
  103. package/src/internal/typed-array/typed-array-constructer.ts +32 -0
  104. package/src/internal/typed-array/typed-array.ts +25 -0
  105. package/src/is-data.ts +47 -0
  106. package/src/lit/decorators/apply-decorator.ts +24 -0
  107. package/src/lit/decorators/apply-service-decorators.ts +13 -0
  108. package/src/lit/decorators/index.ts +24 -0
  109. package/src/lit/decorators/require-service.ts +19 -0
  110. package/src/lit/elements/index.ts +3 -0
  111. package/src/lit/elements/service-application.ts +26 -0
  112. package/src/lit/elements/service-context.ts +3 -0
  113. package/src/lit/elements/service-element.ts +18 -0
  114. package/src/lit/hooks/component/component.ts +10 -0
  115. package/src/lit/hooks/component/stack.ts +17 -0
  116. package/src/lit/hooks/index.ts +13 -0
  117. package/src/lit/hooks/use-connected.ts +39 -0
  118. package/src/lit/hooks/use-effect.ts +17 -0
  119. package/src/lit/hooks/use-element.ts +8 -0
  120. package/src/lit/hooks/use-memo.ts +14 -0
  121. package/src/lit/hooks/use-observable-values.ts +9 -0
  122. package/src/lit/hooks/use-observable.ts +13 -0
  123. package/src/lit/hooks/use-ref.ts +6 -0
  124. package/src/lit/hooks/use-resize-observer.ts +27 -0
  125. package/src/lit/hooks/use-state.ts +17 -0
  126. package/src/lit/hooks/use-window-event.ts +14 -0
  127. package/src/lit/hooks/with-hooks.ts +20 -0
  128. package/src/lit/index.ts +3 -0
  129. package/src/mutable-clone.ts +29 -0
  130. package/{core/data.test.js → src/normalize.test.ts} +22 -22
  131. package/src/normalize.ts +37 -0
  132. package/src/observe/create-observable-event.ts +47 -0
  133. package/src/observe/create-observable-state.ts +51 -0
  134. package/src/observe/create-persisted-state.test.ts +143 -0
  135. package/src/observe/create-persisted-state.ts +60 -0
  136. package/src/observe/create-query-state.ts +70 -0
  137. package/{observe/from-array.js → src/observe/from-array.ts} +14 -3
  138. package/{service/service.js → src/observe/from-constant.ts} +13 -4
  139. package/{observe/from-element-id.js → src/observe/from-element-id.ts} +44 -39
  140. package/src/observe/from-element-properties-and-events.ts +47 -0
  141. package/{observe/from-element-property.js → src/observe/from-element-property.ts} +33 -25
  142. package/src/observe/from-promise-with-error.ts +51 -0
  143. package/src/observe/from-promise.ts +49 -0
  144. package/src/observe/from-properties.ts +67 -0
  145. package/{observe/index.js → src/observe/index.ts} +4 -1
  146. package/src/observe/observe.test.ts +467 -0
  147. package/src/observe/to-promise.ts +40 -0
  148. package/src/observe/to-properties.ts +41 -0
  149. package/src/observe/types.ts +40 -0
  150. package/src/observe/with-async-map.ts +37 -0
  151. package/src/observe/with-batch.test.ts +141 -0
  152. package/src/observe/with-batch.ts +67 -0
  153. package/{core/data.js → src/observe/with-cache.ts} +32 -26
  154. package/src/observe/with-copy.ts +32 -0
  155. package/src/observe/with-deduplicate-data.ts +43 -0
  156. package/src/observe/with-deduplicate.ts +41 -0
  157. package/src/observe/with-default.ts +48 -0
  158. package/src/observe/with-lazy.test.ts +68 -0
  159. package/{core/functions/is-promise.js → src/observe/with-lazy.ts} +17 -5
  160. package/{observe/with-map-data.js → src/observe/with-map-data.ts} +9 -3
  161. package/src/observe/with-map.ts +36 -0
  162. package/src/observe/with-optional.ts +47 -0
  163. package/src/observe/with-unwrap.ts +54 -0
  164. package/src/old-ecs/action-ecs/action-ecs.test.ts +420 -0
  165. package/src/old-ecs/action-ecs/action-ecs.ts +274 -0
  166. package/src/old-ecs/action-ecs/action-types.ts +178 -0
  167. package/src/old-ecs/action-ecs/index.ts +29 -0
  168. package/{service/sequential-action.js → src/old-ecs/action-ecs/sequential-action.ts} +19 -4
  169. package/src/old-ecs/core-ecs/core-ecs-serialization.test.ts +244 -0
  170. package/src/old-ecs/core-ecs/core-ecs-types.ts +183 -0
  171. package/src/old-ecs/core-ecs/core-ecs.test.ts +474 -0
  172. package/src/old-ecs/core-ecs/core-ecs.ts +640 -0
  173. package/{core/functions/index.js → src/old-ecs/core-ecs/index.ts} +9 -4
  174. package/src/old-ecs/ecs/ecs-types.ts +189 -0
  175. package/src/old-ecs/ecs/ecs-where-functions.ts +95 -0
  176. package/src/old-ecs/ecs/ecs.test.ts +461 -0
  177. package/src/old-ecs/ecs/ecs.ts +279 -0
  178. package/{ecs/core-ecs/core-ecs-types.js → src/old-ecs/ecs/index.ts} +2 -2
  179. package/src/old-ecs/entity.ts +26 -0
  180. package/{ecs/index.js → src/old-ecs/index.ts} +1 -1
  181. package/{ecs/transaction-ecs/index.js → src/old-ecs/transaction-ecs/index.ts} +0 -1
  182. package/src/old-ecs/transaction-ecs/transaction-ecs.test.ts +725 -0
  183. package/src/old-ecs/transaction-ecs/transaction-ecs.ts +283 -0
  184. package/src/old-ecs/transaction-ecs/transaction-types.ts +248 -0
  185. package/src/old-ecs/transaction-ecs/transactions.ts +243 -0
  186. package/src/perftest/ecs-perf.ts +255 -0
  187. package/src/perftest/helper-functions.ts +31 -0
  188. package/src/perftest/horizon-perf.ts +132 -0
  189. package/{perftest/index.js → src/perftest/index.ts} +9 -7
  190. package/src/perftest/perf-test.ts +193 -0
  191. package/src/perftest/perf.md +90 -0
  192. package/src/perftest/vanilla-perf.ts +136 -0
  193. package/src/schema/boolean.ts +5 -0
  194. package/src/schema/dynamic/deep-merge.test.ts +100 -0
  195. package/src/schema/dynamic/deep-merge.ts +67 -0
  196. package/{core/schema/dynamic/enumerate-patches.test.js → src/schema/dynamic/enumerate-patches.test.ts} +49 -23
  197. package/{core/schema/dynamic/enumerate-patches.js → src/schema/dynamic/enumerate-patches.ts} +44 -4
  198. package/{core/schema/dynamic/get-dynamic-schema.test.js → src/schema/dynamic/get-dynamic-schema.test.ts} +41 -6
  199. package/src/schema/dynamic/get-dynamic-schema.ts +48 -0
  200. package/src/schema/dynamic/index.ts +22 -0
  201. package/src/schema/f32.ts +30 -0
  202. package/src/schema/i32.ts +31 -0
  203. package/src/schema/index.ts +32 -0
  204. package/{cache/functions/omit.js → src/schema/nullable.ts} +10 -7
  205. package/src/schema/schema.ts +229 -0
  206. package/src/schema/time.ts +5 -0
  207. package/src/schema/true.ts +26 -0
  208. package/src/schema/tuple.ts +5 -0
  209. package/src/schema/u32.ts +31 -0
  210. package/{core/schema/validation/is-valid.test.js → src/schema/validation/is-valid.test.ts} +6 -3
  211. package/{core/functions/array-equals.js → src/schema/validation/is-valid.ts} +8 -10
  212. package/{core/schema/validation/validate.test.js → src/schema/validation/validate.test.ts} +18 -4
  213. package/{core/schema/validation/validate.js → src/schema/validation/validate.ts} +9 -6
  214. package/{core/functions/with-validation.test.js → src/schema/validation/with-validation.test.ts} +28 -12
  215. package/{core/functions/with-validation.js → src/schema/validation/with-validation.ts} +14 -10
  216. package/src/schema.test.ts +55 -0
  217. package/src/service/add-observable-actions.ts +207 -0
  218. package/{service/index.js → src/service/index.ts} +4 -4
  219. package/src/service/is-service.ts +7 -0
  220. package/src/service/progressive-result.ts +141 -0
  221. package/src/service/service.ts +27 -0
  222. package/src/table/add-row.ts +42 -0
  223. package/src/table/create-table.ts +36 -0
  224. package/src/table/delete-row.ts +42 -0
  225. package/src/table/ensure-capacity.ts +37 -0
  226. package/src/table/get-row-data.ts +31 -0
  227. package/src/table/index.ts +29 -0
  228. package/src/table/row-index.ts +26 -0
  229. package/src/table/table.ts +32 -0
  230. package/src/table/update-row.ts +35 -0
  231. package/src/tsconfig.json +6 -0
  232. package/src/typed-buffer/copy-to-gpu-buffer.ts +23 -0
  233. package/{cache/expiring-data-cache.js → src/typed-buffer/create-array-buffer.ts} +31 -25
  234. package/src/typed-buffer/create-number-buffer.ts +90 -0
  235. package/src/typed-buffer/create-struct-buffer.ts +93 -0
  236. package/src/typed-buffer/create-typed-buffer.ts +49 -0
  237. package/src/typed-buffer/index.ts +26 -0
  238. package/src/typed-buffer/structs/assert-struct.ts +37 -0
  239. package/src/typed-buffer/structs/create-read-struct.test.ts +202 -0
  240. package/src/typed-buffer/structs/create-read-struct.ts +77 -0
  241. package/src/typed-buffer/structs/create-write-struct.ts +73 -0
  242. package/src/typed-buffer/structs/get-field-offset.ts +36 -0
  243. package/src/typed-buffer/structs/get-struct-layout.test.ts +146 -0
  244. package/src/typed-buffer/structs/get-struct-layout.ts +222 -0
  245. package/src/typed-buffer/structs/index.ts +22 -0
  246. package/src/typed-buffer/structs/read-struct.ts +25 -0
  247. package/src/typed-buffer/structs/struct-layout.ts +465 -0
  248. package/src/typed-buffer/structs/write-struct.test.ts +195 -0
  249. package/src/typed-buffer/structs/write-struct.ts +25 -0
  250. package/src/typed-buffer/typed-buffer.test.ts +253 -0
  251. package/src/typed-buffer/typed-buffer.ts +41 -0
  252. package/src/types/assert.ts +22 -0
  253. package/src/types/equal.ts +24 -0
  254. package/{types/index.js → src/types/index.ts} +3 -1
  255. package/src/types/types.ts +166 -0
  256. package/tsconfig-base.json +25 -0
  257. package/tsconfig.json +8 -0
  258. package/typedoc.json +22 -0
  259. package/vite.config.js +16 -0
  260. package/assembly/index.d.ts +0 -30
  261. package/assembly/index.js +0 -18
  262. package/assembly/index.wasm +0 -0
  263. package/assembly/index.wasm.map +0 -1
  264. package/assembly-test/assembly.test.d.ts +0 -1
  265. package/assembly-test/assembly.test.js.map +0 -1
  266. package/cache/async-cache.d.ts +0 -15
  267. package/cache/async-cache.js.map +0 -1
  268. package/cache/blob-store.d.ts +0 -94
  269. package/cache/blob-store.js +0 -191
  270. package/cache/blob-store.js.map +0 -1
  271. package/cache/blob-store.test.d.ts +0 -1
  272. package/cache/blob-store.test.js.map +0 -1
  273. package/cache/data-cache.d.ts +0 -38
  274. package/cache/data-cache.js +0 -96
  275. package/cache/data-cache.js.map +0 -1
  276. package/cache/data-cache.test.d.ts +0 -1
  277. package/cache/data-cache.test.js +0 -50
  278. package/cache/data-cache.test.js.map +0 -1
  279. package/cache/expiring-data-cache.d.ts +0 -6
  280. package/cache/expiring-data-cache.js.map +0 -1
  281. package/cache/expiring-data-cache.test.d.ts +0 -1
  282. package/cache/expiring-data-cache.test.js +0 -62
  283. package/cache/expiring-data-cache.test.js.map +0 -1
  284. package/cache/fallback-async-cache.d.ts +0 -7
  285. package/cache/fallback-async-cache.js +0 -22
  286. package/cache/fallback-async-cache.js.map +0 -1
  287. package/cache/functions/bind-functions.d.ts +0 -6
  288. package/cache/functions/bind-functions.js.map +0 -1
  289. package/cache/functions/functions.test.d.ts +0 -1
  290. package/cache/functions/functions.test.js +0 -79
  291. package/cache/functions/functions.test.js.map +0 -1
  292. package/cache/functions/get-cached.d.ts +0 -11
  293. package/cache/functions/get-cached.js.map +0 -1
  294. package/cache/functions/get-cached.test.d.ts +0 -1
  295. package/cache/functions/get-cached.test.js.map +0 -1
  296. package/cache/functions/hashing/blob-to-hash.d.ts +0 -4
  297. package/cache/functions/hashing/blob-to-hash.js.map +0 -1
  298. package/cache/functions/hashing/buffer-to-hash.d.ts +0 -4
  299. package/cache/functions/hashing/buffer-to-hash.js.map +0 -1
  300. package/cache/functions/hashing/hashing.test.d.ts +0 -1
  301. package/cache/functions/hashing/hashing.test.js +0 -95
  302. package/cache/functions/hashing/hashing.test.js.map +0 -1
  303. package/cache/functions/hashing/index.d.ts +0 -4
  304. package/cache/functions/hashing/index.js.map +0 -1
  305. package/cache/functions/hashing/json-to-hash.d.ts +0 -4
  306. package/cache/functions/hashing/json-to-hash.js.map +0 -1
  307. package/cache/functions/hashing/string-to-hash.d.ts +0 -4
  308. package/cache/functions/hashing/string-to-hash.js.map +0 -1
  309. package/cache/functions/index.d.ts +0 -5
  310. package/cache/functions/index.js.map +0 -1
  311. package/cache/functions/memoize.d.ts +0 -12
  312. package/cache/functions/memoize.js.map +0 -1
  313. package/cache/functions/omit.d.ts +0 -1
  314. package/cache/functions/omit.js.map +0 -1
  315. package/cache/functions/prevent-parallel-execution.d.ts +0 -7
  316. package/cache/functions/prevent-parallel-execution.js +0 -25
  317. package/cache/functions/prevent-parallel-execution.js.map +0 -1
  318. package/cache/functions/types.d.ts +0 -1
  319. package/cache/functions/types.js.map +0 -1
  320. package/cache/get-persistent-cache.d.ts +0 -12
  321. package/cache/get-persistent-cache.js +0 -23
  322. package/cache/get-persistent-cache.js.map +0 -1
  323. package/cache/index.d.ts +0 -3
  324. package/cache/index.js.map +0 -1
  325. package/cache/managed-array.d.ts +0 -23
  326. package/cache/managed-array.js +0 -160
  327. package/cache/managed-array.js.map +0 -1
  328. package/cache/managed-async-cache.browser.test.d.ts +0 -1
  329. package/cache/managed-async-cache.browser.test.js.map +0 -1
  330. package/cache/managed-async-cache.d.ts +0 -4
  331. package/cache/managed-async-cache.js +0 -45
  332. package/cache/managed-async-cache.js.map +0 -1
  333. package/cache/memory-allocator.d.ts +0 -23
  334. package/cache/memory-allocator.js +0 -94
  335. package/cache/memory-allocator.js.map +0 -1
  336. package/cache/memory-async-cache.d.ts +0 -6
  337. package/cache/memory-async-cache.js +0 -23
  338. package/cache/memory-async-cache.js.map +0 -1
  339. package/core/data.d.ts +0 -22
  340. package/core/data.js.map +0 -1
  341. package/core/data.test.d.ts +0 -1
  342. package/core/data.test.js.map +0 -1
  343. package/core/functions/array-equals.d.ts +0 -1
  344. package/core/functions/array-equals.js.map +0 -1
  345. package/core/functions/deep-merge.d.ts +0 -32
  346. package/core/functions/deep-merge.js +0 -54
  347. package/core/functions/deep-merge.js.map +0 -1
  348. package/core/functions/deep-merge.test.d.ts +0 -1
  349. package/core/functions/deep-merge.test.js +0 -94
  350. package/core/functions/deep-merge.test.js.map +0 -1
  351. package/core/functions/index.d.ts +0 -3
  352. package/core/functions/index.js.map +0 -1
  353. package/core/functions/is-async-generator.d.ts +0 -1
  354. package/core/functions/is-async-generator.js.map +0 -1
  355. package/core/functions/is-promise.d.ts +0 -1
  356. package/core/functions/is-promise.js.map +0 -1
  357. package/core/functions/with-validation.d.ts +0 -5
  358. package/core/functions/with-validation.js.map +0 -1
  359. package/core/functions/with-validation.test.d.ts +0 -1
  360. package/core/functions/with-validation.test.js.map +0 -1
  361. package/core/index.d.ts +0 -3
  362. package/core/index.js.map +0 -1
  363. package/core/schema/dynamic/dynamic-schema.d.ts +0 -27
  364. package/core/schema/dynamic/dynamic-schema.js +0 -2
  365. package/core/schema/dynamic/dynamic-schema.js.map +0 -1
  366. package/core/schema/dynamic/enumerate-patches.d.ts +0 -9
  367. package/core/schema/dynamic/enumerate-patches.js.map +0 -1
  368. package/core/schema/dynamic/enumerate-patches.test.d.ts +0 -134
  369. package/core/schema/dynamic/enumerate-patches.test.js.map +0 -1
  370. package/core/schema/dynamic/get-dynamic-schema.d.ts +0 -5
  371. package/core/schema/dynamic/get-dynamic-schema.js +0 -26
  372. package/core/schema/dynamic/get-dynamic-schema.js.map +0 -1
  373. package/core/schema/dynamic/get-dynamic-schema.test.d.ts +0 -1
  374. package/core/schema/dynamic/get-dynamic-schema.test.js.map +0 -1
  375. package/core/schema/dynamic/index.d.ts +0 -1
  376. package/core/schema/dynamic/index.js +0 -2
  377. package/core/schema/dynamic/index.js.map +0 -1
  378. package/core/schema/dynamic/schema-path-value-path.d.ts +0 -5
  379. package/core/schema/dynamic/schema-path-value-path.js +0 -12
  380. package/core/schema/dynamic/schema-path-value-path.js.map +0 -1
  381. package/core/schema/dynamic-schema.d.ts +0 -27
  382. package/core/schema/dynamic-schema.js +0 -2
  383. package/core/schema/dynamic-schema.js.map +0 -1
  384. package/core/schema/index.d.ts +0 -4
  385. package/core/schema/index.js +0 -26
  386. package/core/schema/index.js.map +0 -1
  387. package/core/schema/schema.d.ts +0 -122
  388. package/core/schema/schema.js.map +0 -1
  389. package/core/schema/schemas.d.ts +0 -45
  390. package/core/schema/schemas.js +0 -39
  391. package/core/schema/schemas.js.map +0 -1
  392. package/core/schema/ui-schema.d.ts +0 -25
  393. package/core/schema/ui-schema.js +0 -2
  394. package/core/schema/ui-schema.js.map +0 -1
  395. package/core/schema/validation/is-valid.d.ts +0 -2
  396. package/core/schema/validation/is-valid.js +0 -14
  397. package/core/schema/validation/is-valid.js.map +0 -1
  398. package/core/schema/validation/is-valid.test.d.ts +0 -1
  399. package/core/schema/validation/is-valid.test.js.map +0 -1
  400. package/core/schema/validation/validate.d.ts +0 -2
  401. package/core/schema/validation/validate.js.map +0 -1
  402. package/core/schema/validation/validate.test.d.ts +0 -1
  403. package/core/schema/validation/validate.test.js.map +0 -1
  404. package/core/schema/validation/with-validation.d.ts +0 -5
  405. package/core/schema/validation/with-validation.js +0 -16
  406. package/core/schema/validation/with-validation.js.map +0 -1
  407. package/core/schema/validation/with-validation.test.d.ts +0 -1
  408. package/core/schema/validation/with-validation.test.js +0 -96
  409. package/core/schema/validation/with-validation.test.js.map +0 -1
  410. package/core/schema.d.ts +0 -86
  411. package/core/schema.js.map +0 -1
  412. package/core/schema.test.d.ts +0 -1
  413. package/core/schema.test.js +0 -16
  414. package/core/schema.test.js.map +0 -1
  415. package/core/schemas.d.ts +0 -45
  416. package/core/schemas.js +0 -39
  417. package/core/schemas.js.map +0 -1
  418. package/ecs/action-ecs/action-ecs.d.ts +0 -19
  419. package/ecs/action-ecs/action-ecs.js +0 -203
  420. package/ecs/action-ecs/action-ecs.js.map +0 -1
  421. package/ecs/action-ecs/action-ecs.test.d.ts +0 -1
  422. package/ecs/action-ecs/action-ecs.test.js +0 -362
  423. package/ecs/action-ecs/action-ecs.test.js.map +0 -1
  424. package/ecs/action-ecs/action-types.d.ts +0 -106
  425. package/ecs/action-ecs/action-types.js +0 -19
  426. package/ecs/action-ecs/action-types.js.map +0 -1
  427. package/ecs/action-ecs/index.d.ts +0 -2
  428. package/ecs/action-ecs/index.js.map +0 -1
  429. package/ecs/core-ecs/core-ecs-serialization.test.d.ts +0 -1
  430. package/ecs/core-ecs/core-ecs-serialization.test.js +0 -230
  431. package/ecs/core-ecs/core-ecs-serialization.test.js.map +0 -1
  432. package/ecs/core-ecs/core-ecs-types.d.ts +0 -141
  433. package/ecs/core-ecs/core-ecs-types.js.map +0 -1
  434. package/ecs/core-ecs/core-ecs.d.ts +0 -7
  435. package/ecs/core-ecs/core-ecs.js +0 -492
  436. package/ecs/core-ecs/core-ecs.js.map +0 -1
  437. package/ecs/core-ecs/core-ecs.test.d.ts +0 -1
  438. package/ecs/core-ecs/core-ecs.test.js +0 -425
  439. package/ecs/core-ecs/core-ecs.test.js.map +0 -1
  440. package/ecs/core-ecs/index.d.ts +0 -1
  441. package/ecs/core-ecs/index.js +0 -2
  442. package/ecs/core-ecs/index.js.map +0 -1
  443. package/ecs/ecs/ecs-types.d.ts +0 -132
  444. package/ecs/ecs/ecs-types.js.map +0 -1
  445. package/ecs/ecs/ecs-where-functions.d.ts +0 -6
  446. package/ecs/ecs/ecs-where-functions.js +0 -91
  447. package/ecs/ecs/ecs-where-functions.js.map +0 -1
  448. package/ecs/ecs/ecs.d.ts +0 -13
  449. package/ecs/ecs/ecs.js +0 -177
  450. package/ecs/ecs/ecs.js.map +0 -1
  451. package/ecs/ecs/ecs.test.d.ts +0 -1
  452. package/ecs/ecs/ecs.test.js +0 -399
  453. package/ecs/ecs/ecs.test.js.map +0 -1
  454. package/ecs/ecs/index.d.ts +0 -3
  455. package/ecs/ecs/index.js +0 -3
  456. package/ecs/ecs/index.js.map +0 -1
  457. package/ecs/index.d.ts +0 -4
  458. package/ecs/index.js.map +0 -1
  459. package/ecs/transaction-ecs/index.d.ts +0 -2
  460. package/ecs/transaction-ecs/index.js.map +0 -1
  461. package/ecs/transaction-ecs/transaction-ecs.d.ts +0 -11
  462. package/ecs/transaction-ecs/transaction-ecs.js +0 -184
  463. package/ecs/transaction-ecs/transaction-ecs.js.map +0 -1
  464. package/ecs/transaction-ecs/transaction-ecs.test.d.ts +0 -1
  465. package/ecs/transaction-ecs/transaction-ecs.test.js +0 -599
  466. package/ecs/transaction-ecs/transaction-ecs.test.js.map +0 -1
  467. package/ecs/transaction-ecs/transaction-types.d.ts +0 -135
  468. package/ecs/transaction-ecs/transaction-types.js +0 -2
  469. package/ecs/transaction-ecs/transaction-types.js.map +0 -1
  470. package/ecs/transaction-ecs/transactions.d.ts +0 -5
  471. package/ecs/transaction-ecs/transactions.js +0 -158
  472. package/ecs/transaction-ecs/transactions.js.map +0 -1
  473. package/index.d.ts +0 -1
  474. package/index.js +0 -23
  475. package/index.js.map +0 -1
  476. package/observe/create-observable-event.d.ts +0 -10
  477. package/observe/create-observable-event.js +0 -22
  478. package/observe/create-observable-event.js.map +0 -1
  479. package/observe/create-observable-state.d.ts +0 -7
  480. package/observe/create-observable-state.js +0 -27
  481. package/observe/create-observable-state.js.map +0 -1
  482. package/observe/create-persisted-state.d.ts +0 -11
  483. package/observe/create-persisted-state.js +0 -31
  484. package/observe/create-persisted-state.js.map +0 -1
  485. package/observe/create-persisted-state.test.d.ts +0 -1
  486. package/observe/create-persisted-state.test.js +0 -124
  487. package/observe/create-persisted-state.test.js.map +0 -1
  488. package/observe/from-array.d.ts +0 -5
  489. package/observe/from-array.js.map +0 -1
  490. package/observe/from-constant.d.ts +0 -5
  491. package/observe/from-constant.js +0 -12
  492. package/observe/from-constant.js.map +0 -1
  493. package/observe/from-element-id.d.ts +0 -7
  494. package/observe/from-element-id.js.map +0 -1
  495. package/observe/from-element-properties-and-events.d.ts +0 -2
  496. package/observe/from-element-properties-and-events.js +0 -18
  497. package/observe/from-element-properties-and-events.js.map +0 -1
  498. package/observe/from-element-property.d.ts +0 -11
  499. package/observe/from-element-property.js.map +0 -1
  500. package/observe/from-promise-with-error.d.ts +0 -7
  501. package/observe/from-promise-with-error.js +0 -27
  502. package/observe/from-promise-with-error.js.map +0 -1
  503. package/observe/from-promise.d.ts +0 -6
  504. package/observe/from-promise.js +0 -22
  505. package/observe/from-promise.js.map +0 -1
  506. package/observe/from-properties.d.ts +0 -10
  507. package/observe/from-properties.js +0 -33
  508. package/observe/from-properties.js.map +0 -1
  509. package/observe/index.d.ts +0 -27
  510. package/observe/index.js.map +0 -1
  511. package/observe/observe.test.d.ts +0 -7
  512. package/observe/observe.test.js +0 -417
  513. package/observe/observe.test.js.map +0 -1
  514. package/observe/to-promise.d.ts +0 -8
  515. package/observe/to-promise.js +0 -18
  516. package/observe/to-promise.js.map +0 -1
  517. package/observe/to-properties.d.ts +0 -11
  518. package/observe/to-properties.js +0 -9
  519. package/observe/to-properties.js.map +0 -1
  520. package/observe/types.d.ts +0 -17
  521. package/observe/types.js +0 -2
  522. package/observe/types.js.map +0 -1
  523. package/observe/with-async-map.d.ts +0 -6
  524. package/observe/with-async-map.js +0 -12
  525. package/observe/with-async-map.js.map +0 -1
  526. package/observe/with-cache.d.ts +0 -6
  527. package/observe/with-cache.js +0 -33
  528. package/observe/with-cache.js.map +0 -1
  529. package/observe/with-copy.d.ts +0 -5
  530. package/observe/with-copy.js +0 -10
  531. package/observe/with-copy.js.map +0 -1
  532. package/observe/with-deduplicate-data.d.ts +0 -7
  533. package/observe/with-deduplicate-data.js +0 -18
  534. package/observe/with-deduplicate-data.js.map +0 -1
  535. package/observe/with-deduplicate.d.ts +0 -6
  536. package/observe/with-deduplicate.js +0 -19
  537. package/observe/with-deduplicate.js.map +0 -1
  538. package/observe/with-default.d.ts +0 -6
  539. package/observe/with-default.js +0 -21
  540. package/observe/with-default.js.map +0 -1
  541. package/observe/with-map-data.d.ts +0 -7
  542. package/observe/with-map-data.js.map +0 -1
  543. package/observe/with-map.d.ts +0 -5
  544. package/observe/with-map.js +0 -11
  545. package/observe/with-map.js.map +0 -1
  546. package/observe/with-optional.d.ts +0 -5
  547. package/observe/with-optional.js +0 -20
  548. package/observe/with-optional.js.map +0 -1
  549. package/observe/with-unwrap.d.ts +0 -5
  550. package/observe/with-unwrap.js +0 -26
  551. package/observe/with-unwrap.js.map +0 -1
  552. package/perftest/ecs-perf.d.ts +0 -49
  553. package/perftest/ecs-perf.js +0 -230
  554. package/perftest/ecs-perf.js.map +0 -1
  555. package/perftest/helper-functions.d.ts +0 -1
  556. package/perftest/helper-functions.js +0 -31
  557. package/perftest/helper-functions.js.map +0 -1
  558. package/perftest/horizon-perf.d.ts +0 -22
  559. package/perftest/horizon-perf.js +0 -126
  560. package/perftest/horizon-perf.js.map +0 -1
  561. package/perftest/index.d.ts +0 -1
  562. package/perftest/index.js.map +0 -1
  563. package/perftest/perf-test.d.ts +0 -18
  564. package/perftest/perf-test.js +0 -124
  565. package/perftest/perf-test.js.map +0 -1
  566. package/perftest/vanilla-perf.d.ts +0 -38
  567. package/perftest/vanilla-perf.js +0 -128
  568. package/perftest/vanilla-perf.js.map +0 -1
  569. package/schemas/index.d.ts +0 -1
  570. package/schemas/index.js +0 -23
  571. package/schemas/index.js.map +0 -1
  572. package/schemas/schemas.d.ts +0 -45
  573. package/schemas/schemas.js +0 -39
  574. package/schemas/schemas.js.map +0 -1
  575. package/service/add-observable-actions.d.ts +0 -29
  576. package/service/add-observable-actions.js +0 -108
  577. package/service/add-observable-actions.js.map +0 -1
  578. package/service/index.d.ts +0 -4
  579. package/service/index.js.map +0 -1
  580. package/service/progressive-result.d.ts +0 -96
  581. package/service/progressive-result.js +0 -99
  582. package/service/progressive-result.js.map +0 -1
  583. package/service/sequential-action.d.ts +0 -18
  584. package/service/sequential-action.js.map +0 -1
  585. package/service/service.d.ts +0 -4
  586. package/service/service.js.map +0 -1
  587. package/tsconfig.tsbuildinfo +0 -1
  588. package/types/index.d.ts +0 -1
  589. package/types/index.js.map +0 -1
  590. package/types/types.d.ts +0 -61
  591. package/types/types.js.map +0 -1
@@ -0,0 +1,640 @@
1
+ /*MIT License
2
+
3
+ © Copyright 2025 Adobe. All rights reserved.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.*/
22
+
23
+ import {
24
+ Data,
25
+ equals,
26
+ normalize,
27
+ } from "../../index.js";
28
+ import {
29
+ FromSchema,
30
+ Schema,
31
+ } from "../../schema/index.js";
32
+ import {
33
+ ManagedArray,
34
+ createManagedArray,
35
+ } from "../../cache/managed-array.js";
36
+ import {
37
+ MemoryAllocator,
38
+ createSimpleMemoryAllocator,
39
+ } from "../../cache/memory-allocator.js";
40
+ import {
41
+ CoreArchetype,
42
+ CoreECS,
43
+ Table,
44
+ Entity,
45
+ EntityValues,
46
+ CoreComponents,
47
+ ECSJSON,
48
+ CoreResources,
49
+ } from "./core-ecs-types.js";
50
+ import { U32Schema } from "../../schema/u32.js";
51
+
52
+ // This is a sentinel value used to indicate a component should be deleted.
53
+ export const DELETE: unknown = "_@_DELETE_@_";
54
+
55
+ type CoreTableImpl<C extends CoreComponents> = Omit<Table<C>, "columns"> & {
56
+ columns: { [K in keyof C]: ManagedArray<C[K]> };
57
+ };
58
+
59
+ type CoreArchetypeImpl<C extends CoreComponents> = CoreArchetype<C> &
60
+ CoreTableImpl<C> & {
61
+ id: number;
62
+ rows: number;
63
+ components: (keyof C)[];
64
+ addComponent: Map<keyof C, CoreArchetypeImpl<C>>;
65
+ removeComponent: Map<keyof C, CoreArchetypeImpl<C>>;
66
+ createRow: (values?: C) => number;
67
+ deleteRow: (row: number) => void;
68
+ readRow: (row: number) => C;
69
+ };
70
+
71
+ export function createCoreECS<
72
+ C extends CoreComponents,
73
+ R extends CoreResources,
74
+ >(
75
+ options: {
76
+ data?: ECSJSON;
77
+ allocator?: MemoryAllocator;
78
+ } = {}
79
+ ): CoreECS<CoreComponents, {}> {
80
+ type Component = keyof C;
81
+ const ELEMENTS_PER_RECORD = 2;
82
+ const DELETED_ARCHETYPE = 0xff;
83
+
84
+ const componentSchemas = {
85
+ id: U32Schema,
86
+ } as { [K in keyof C]: Schema };
87
+ const getSchema = (name: Component) => {
88
+ const schema = componentSchemas[name];
89
+ if (!schema) {
90
+ throw new Error(`Component "${String(name)}" not found`);
91
+ }
92
+ return schema;
93
+ };
94
+
95
+ const resources = {} as R;
96
+
97
+ let recordCount = 0;
98
+ let recordCapacity = 1024;
99
+ let firstDeletedId = 0;
100
+ let records = new Uint32Array(recordCapacity * ELEMENTS_PER_RECORD);
101
+
102
+ const { allocator = createSimpleMemoryAllocator() } = options;
103
+ // What if we remove internal components?
104
+ // id, name, component, schema?
105
+ // We DO need id as a component since it gets it's own column.
106
+ // But do we need to track components internally?
107
+ // [x] This is safely derived as needed.
108
+ const archetypeLookup: Record<string, CoreArchetypeImpl<C>> = {};
109
+ // [ ] the order of these is relevant for serialization currently.
110
+ const archetables: CoreArchetypeImpl<C>[] = [];
111
+ // [x] This is created as archetypes are requested.
112
+ const componentsToArchetypes = new Map<keyof C, Set<CoreArchetypeImpl<C>>>();
113
+
114
+ /**
115
+ * Returns the component with the least number of archetypes containing it.
116
+ */
117
+ const getMostExclusiveComponent = (components: Component[]): Component => {
118
+ if (components.length === 0) {
119
+ throw new Error("Cannot call with zero components");
120
+ }
121
+ let mostExclusive = components[0];
122
+ let size = Number.POSITIVE_INFINITY;
123
+ for (const component of components) {
124
+ const archetypes = getComponentsToArchetypes(component)!;
125
+ if (archetypes.size < size) {
126
+ size = archetypes.size;
127
+ mostExclusive = component;
128
+ }
129
+ }
130
+ return mostExclusive;
131
+ };
132
+
133
+ const getAdjacentArchetype = (
134
+ archetable: CoreArchetypeImpl<C>,
135
+ component: Component,
136
+ addOrRemove: "addComponent" | "removeComponent"
137
+ ): CoreArchetypeImpl<C> => {
138
+ let newTable = archetable[addOrRemove].get(component);
139
+ if (!newTable) {
140
+ const add = addOrRemove === "addComponent";
141
+ const newComponents = (
142
+ add
143
+ ? [...archetable.components, component].sort()
144
+ : archetable.components.filter((c) => c !== component)
145
+ ) as Component[];
146
+ newTable = getArchetype(...newComponents);
147
+ // add the edge lookup to the new table
148
+ archetable[addOrRemove].set(component, newTable);
149
+ newTable[
150
+ addOrRemove === "addComponent" ? "removeComponent" : "addComponent"
151
+ ].set(component, archetable);
152
+ }
153
+ return newTable;
154
+ };
155
+
156
+ const getComponentValue = <Name extends Component>(
157
+ id: Entity,
158
+ component: Name
159
+ ): C[Name] | undefined => {
160
+ const index = id * ELEMENTS_PER_RECORD;
161
+ const archetable = archetables[records[index + 0]];
162
+ const row = records[index + 1];
163
+ const array = archetable.columns[component];
164
+ if (array === undefined) {
165
+ return undefined;
166
+ }
167
+ return array.get(row) as C[Name];
168
+ };
169
+
170
+ const setComponentValue = <Name extends Component>(
171
+ id: Entity,
172
+ component: Name,
173
+ value: C[Name]
174
+ ): void => {
175
+ if (value === undefined) {
176
+ value = DELETE as any;
177
+ }
178
+ const index = id * ELEMENTS_PER_RECORD;
179
+ const archetable = archetables[records[index + 0]];
180
+ const currentRow = records[index + 1];
181
+ const array = archetable.columns[component];
182
+ if (value !== DELETE) {
183
+ if (array === undefined) {
184
+ // add component
185
+ const newTable = getAdjacentArchetype(
186
+ archetable,
187
+ component,
188
+ "addComponent"
189
+ );
190
+ const newRow = newTable.createRow();
191
+ // manually move values over
192
+ for (const name of archetable.components as Component[]) {
193
+ const targetColumn = archetable.columns[name];
194
+ if (targetColumn !== undefined) {
195
+ newTable.columns[name].set(newRow, targetColumn.get(currentRow));
196
+ }
197
+ }
198
+ // set the new component value
199
+ newTable.columns[component].set(newRow, value);
200
+
201
+ archetable.deleteRow(currentRow);
202
+ records[index + 0] = newTable.id;
203
+ records[index + 1] = newRow;
204
+ } else {
205
+ // updating value
206
+ array.set(currentRow, value);
207
+ }
208
+ } else if (array !== undefined) {
209
+ // remove component
210
+ const newTable = getAdjacentArchetype(
211
+ archetable,
212
+ component,
213
+ "removeComponent"
214
+ );
215
+ const newRow = newTable.createRow();
216
+ // manually move values over, except for the removed one
217
+ for (const name of archetable.components as Component[]) {
218
+ if (name !== component) {
219
+ newTable.columns[name].set(
220
+ newRow,
221
+ archetable.columns[name].get(currentRow)
222
+ );
223
+ }
224
+ }
225
+ archetable.deleteRow(currentRow);
226
+ records[index + 0] = newTable.id;
227
+ records[index + 1] = newRow;
228
+ }
229
+ };
230
+
231
+ const getArchetype = (...components: Component[]): CoreArchetypeImpl<C> => {
232
+ components.sort();
233
+ const ids = new Set(components);
234
+ if (!ids.has("id")) {
235
+ ids.add("id");
236
+ components.unshift("id");
237
+ }
238
+ const key = components.join(",");
239
+ let archetype: CoreArchetypeImpl<C> = archetypeLookup[key];
240
+ if (!archetype) {
241
+ const columns = Object.fromEntries(
242
+ components.map((name) => [
243
+ name,
244
+ createManagedArray(getSchema(name), allocator),
245
+ ])
246
+ ) as CoreTableImpl<C>["columns"];
247
+
248
+ const id = archetables.length;
249
+ archetype =
250
+ archetables[id] =
251
+ archetypeLookup[key] =
252
+ {
253
+ components,
254
+ id,
255
+ rows: 0,
256
+ columns,
257
+ addComponent: new Map(),
258
+ removeComponent: new Map(),
259
+ readRow: (row: number) => {
260
+ const values: any = {};
261
+ for (const name of components) {
262
+ values[name] = columns[name].get(row);
263
+ }
264
+ return values;
265
+ },
266
+ createRow: (values: C | undefined) => {
267
+ const newRow = archetype.rows++;
268
+ for (const name of components) {
269
+ const column = columns[name] as ManagedArray<any>;
270
+ column.ensureCapacity(archetype.rows);
271
+ if (values) {
272
+ const value = values[name];
273
+ if (value === undefined) {
274
+ throw new Error(
275
+ `Missing value for column "${String(name)}"`
276
+ );
277
+ }
278
+ column.set(newRow, value);
279
+ }
280
+ }
281
+ return newRow;
282
+ },
283
+ deleteRow: (row: number) => {
284
+ archetype.rows--;
285
+ // the main ecs records row pointer is updated to the new row
286
+ if (archetype.rows > 0) {
287
+ const movedRowId = columns.id.native![archetype.rows];
288
+ const movedRowIndex = movedRowId * ELEMENTS_PER_RECORD;
289
+ records[movedRowIndex + 1] = row;
290
+ }
291
+ // then we remove each columns row, moving the last to replace it.
292
+ for (const name of components) {
293
+ const column = columns[name];
294
+ if (column !== undefined) {
295
+ if (archetype.rows > 0) {
296
+ // the last row (at size) is moved to replace the deleted row
297
+ column.move(archetype.rows, row);
298
+ }
299
+ }
300
+ }
301
+ },
302
+ };
303
+ for (const name of components) {
304
+ getComponentsToArchetypes(name).add(archetype);
305
+ }
306
+ }
307
+ return archetype;
308
+ };
309
+
310
+ const getEntityArchetype = (id: Entity): CoreArchetype => {
311
+ return archetables[records[id * ELEMENTS_PER_RECORD]] as any;
312
+ };
313
+
314
+ const getComponentsToArchetypes = (
315
+ name: keyof C
316
+ ): Set<CoreArchetypeImpl<C>> => {
317
+ let set = componentsToArchetypes.get(name);
318
+ if (!set) {
319
+ componentsToArchetypes.set(name, (set = new Set()));
320
+ }
321
+ return set;
322
+ };
323
+
324
+ const withComponents = <
325
+ S extends { [K: string]: Schema },
326
+ T = { [K in keyof S]: FromSchema<S[K]> },
327
+ >(
328
+ components: S
329
+ ) => {
330
+ Object.entries(components).forEach(([name, newSchema]) => {
331
+ const oldSchema = componentSchemas[name as keyof C];
332
+ // it's OK for a component to be defined more than once
333
+ // so long as the schema is the same.
334
+ if (oldSchema && !equals(normalize(oldSchema), normalize(newSchema))) {
335
+ console.warn(`Component "${name}" schema changed from ${JSON.stringify(oldSchema)} to ${JSON.stringify(newSchema)}`);
336
+ }
337
+ componentSchemas[name as keyof C] = newSchema;
338
+ });
339
+ return ecs as CoreECS<CoreComponents & T>;
340
+ };
341
+
342
+ const withResources = <T extends { readonly [K: string]: Data }>(
343
+ newResources: T
344
+ ): any => {
345
+ Object.entries(newResources).forEach(([name, value]) => {
346
+ // register the resource as a component.
347
+ withComponents({ [name]: {} });
348
+ const archetype = getArchetype("id", name as Component);
349
+ // resource row *may* already exist if loaded from disk.
350
+ const entityId = archetype.rows
351
+ ? archetype.columns.id.get(archetype.rows)
352
+ : createEntity(archetype, { [name]: value } as any);
353
+ const column = (archetype.columns as any)[name] as ManagedArray<Data>;
354
+ Object.defineProperty(resources, name, {
355
+ get: () => column.get(0),
356
+ set: (newValue) => {
357
+ updateEntity(entityId, { [name]: newValue });
358
+ },
359
+ });
360
+ });
361
+ return ecs as any;
362
+ };
363
+
364
+ function getTables(): CoreTableImpl<C>[];
365
+ function getTables(
366
+ archetype: CoreArchetype<C>,
367
+ options: { mode: "read" | "write"; exact?: boolean }
368
+ ): CoreTableImpl<C>[];
369
+ function getTables(
370
+ archetype?: CoreArchetype<C>,
371
+ options?: { mode: "read" | "write"; exact?: boolean }
372
+ ): CoreTableImpl<C>[] {
373
+ if (!archetype) {
374
+ return archetables.filter((a) => a.rows > 0);
375
+ }
376
+ if (options?.exact) {
377
+ return [archetype as CoreArchetypeImpl<C>];
378
+ }
379
+ const components = archetype.components as (keyof C)[];
380
+ const tables: CoreTableImpl<C>[] = [];
381
+ const baseSet = getComponentsToArchetypes(
382
+ getMostExclusiveComponent(components)
383
+ )!;
384
+ for (const archetable of baseSet) {
385
+ if (archetable.rows === 0) {
386
+ continue;
387
+ }
388
+ let hasAll = true;
389
+ for (const component of components) {
390
+ if (!archetable.columns[component]) {
391
+ hasAll = false;
392
+ break;
393
+ }
394
+ }
395
+ if (hasAll) {
396
+ tables.push(archetable);
397
+ }
398
+ }
399
+ return tables;
400
+ }
401
+ const countEntities = (archetype: CoreArchetype) => {
402
+ let count = 0;
403
+ for (const table of getTables(archetype, { mode: "read" })) {
404
+ count += table.rows;
405
+ }
406
+ return count;
407
+ };
408
+
409
+ const selectEntities = (archetype: CoreArchetype) => {
410
+ const tables = getTables(archetype, { mode: "read" });
411
+ const entities: number[] = [];
412
+ let offset = 0;
413
+ for (const table of tables) {
414
+ // this native array will probably be longer than the actual size.
415
+ const size = table.rows;
416
+ const nativeArray = table.columns.id.native!;
417
+ // copy from 0 .. size to entities
418
+ for (let i = 0; i < size; i++) {
419
+ entities[offset++] = nativeArray[i];
420
+ }
421
+ }
422
+ return entities;
423
+ };
424
+
425
+ const getEntityValues = (
426
+ id: Entity
427
+ ): EntityValues<CoreComponents> | undefined => {
428
+ const index = id * ELEMENTS_PER_RECORD;
429
+ const archetypeId = records[index + 0];
430
+ if (archetypeId === undefined || archetypeId === DELETED_ARCHETYPE) {
431
+ return undefined;
432
+ }
433
+ const archetable = archetables[records[index + 0]];
434
+ const row = records[index + 1];
435
+ const values: any = {};
436
+ for (const name of archetable.components) {
437
+ values[name] = archetable.columns[name].get(row);
438
+ }
439
+ return values;
440
+ };
441
+
442
+ const updateEntity = (
443
+ id: Entity,
444
+ values: { [K in keyof CoreComponents]?: CoreComponents[K] | undefined }
445
+ ) => {
446
+ for (const name in values) {
447
+ if (name === "id") {
448
+ throw new Error("Cannot update id");
449
+ }
450
+ let value = values[name as keyof CoreComponents] as any;
451
+ if (value === undefined) {
452
+ value = DELETE;
453
+ }
454
+ setComponentValue(id, name as keyof C, value);
455
+ }
456
+ };
457
+
458
+ function ensureRecordCapacity(capacity: number) {
459
+ if (recordCapacity < capacity) {
460
+ recordCapacity = Math.max(capacity, recordCapacity * 2);
461
+ const newRecords = new Uint32Array(recordCapacity * ELEMENTS_PER_RECORD);
462
+ newRecords.set(records);
463
+ records = newRecords;
464
+ }
465
+ }
466
+
467
+ const createEntity = <A extends CoreComponents>(
468
+ _archetype?: CoreArchetype<A>,
469
+ values?: Omit<A, "id">
470
+ ) => {
471
+ const archetype = (_archetype as CoreArchetypeImpl<C>) ?? emptyArchetype;
472
+ let id: number;
473
+ if (firstDeletedId > 0 && recordCount > 0) {
474
+ id = firstDeletedId;
475
+ const index = id * ELEMENTS_PER_RECORD;
476
+ // point to the next deleted id
477
+ firstDeletedId = records[index + 1];
478
+ } else {
479
+ // create new entity record
480
+ ensureRecordCapacity(recordCount + 1);
481
+ id = recordCount++;
482
+ }
483
+ // now put it directly into the archetype.
484
+ values = (values ? { ...values, id } : { id }) as any;
485
+ const row = archetype.createRow(values as any);
486
+
487
+ // now add the records entry
488
+ const index = id * ELEMENTS_PER_RECORD;
489
+
490
+ records[index + 0] = archetype.id;
491
+ records[index + 1] = row;
492
+
493
+ return id;
494
+ };
495
+
496
+ const createBatch = <T extends CoreComponents>(
497
+ archetype: CoreArchetype<T>,
498
+ count: number,
499
+ ) => {
500
+ const table = getTables(archetype as any, { mode: "write", exact: true })[0] as unknown as CoreArchetypeImpl<T>;
501
+ ensureRecordCapacity(recordCount + count);
502
+ for (const name in table.columns) {
503
+ const column = table.columns[name];
504
+ column.ensureCapacity(table.rows + count);
505
+ }
506
+ const ids = table.columns.id.native!;
507
+ // we are only going to rapidly create main record entries *and* the archetype id values.
508
+ let created = 0;
509
+ // first recycle any deleted ids
510
+ while (created < count && firstDeletedId > 0 && recordCount > 0) {
511
+ const id = firstDeletedId;
512
+ const index = id * ELEMENTS_PER_RECORD;
513
+ // point to the next deleted id
514
+ firstDeletedId = records[index + 1];
515
+ const row = table.rows + created++;
516
+ records[index + 0] = table.id;
517
+ records[index + 1] = row;
518
+ ids[row] = id;
519
+ }
520
+ recordCount += created;
521
+ // then quickly create the rest
522
+ for (; created < count; created++) {
523
+ const id = recordCount++;
524
+ const row = table.rows + created;
525
+ records[id * ELEMENTS_PER_RECORD + 0] = table.id;
526
+ records[id * ELEMENTS_PER_RECORD + 1] = row;
527
+ ids[row] = id;
528
+ }
529
+
530
+ table.rows += count;
531
+
532
+ return table as Table<T>;
533
+ };
534
+
535
+ const deleteEntity = (id: Entity) => {
536
+ const index = id * ELEMENTS_PER_RECORD;
537
+ const archetype = archetables[records[index + 0]];
538
+ const row = records[index + 1];
539
+ archetype.deleteRow(row);
540
+ const nextDeletedId = firstDeletedId;
541
+ firstDeletedId = id;
542
+ records[index + 0] = DELETED_ARCHETYPE;
543
+ records[index + 1] = nextDeletedId;
544
+ };
545
+
546
+ // this first archetype in the table must be the empty entity archetype which contains only the id.
547
+ const emptyArchetype = getArchetype("id");
548
+
549
+ // 1 = initial
550
+ // 2 = simplified format
551
+ // 3 = Numbers default to Float64Array instead of Float32Array
552
+ // 4 = bug fix for createRow not ensuring capacity
553
+ const SERIALIZATION_VERSION = 4;
554
+ const toJSON = () => {
555
+ return {
556
+ ecs: true,
557
+ version: SERIALIZATION_VERSION,
558
+ components: componentSchemas,
559
+ entities: [...records.slice(0, recordCount * 2)],
560
+ tables: archetables.map((table, index) => {
561
+ if (index !== table.id) {
562
+ throw new Error(`Table id mismatch: ${index} !== ${table.id}`);
563
+ }
564
+ const { rows, columns } = table;
565
+ return {
566
+ rows,
567
+ columns: Object.fromEntries(
568
+ Object.entries(columns).map(([name, column]) => {
569
+ const value = column.toJSON(rows, name !== "id");
570
+ return [name, value];
571
+ })
572
+ ),
573
+ };
574
+ }),
575
+ } as const;
576
+ };
577
+
578
+ const fromJSON = (json: any) => {
579
+ const { ecs, version, entities, tables } = json;
580
+ if (!ecs) {
581
+ throw new Error("Not an ECS");
582
+ }
583
+ if (version !== SERIALIZATION_VERSION) {
584
+ return false;
585
+ }
586
+
587
+ recordCount = entities.length / 2;
588
+ ensureRecordCapacity(recordCount);
589
+ records.set(entities);
590
+ Object.assign(componentSchemas, json.components);
591
+
592
+ for (let i = 0; i < tables.length; i++) {
593
+ const persistedTable = tables[i];
594
+ const { rows, columns } = persistedTable;
595
+ const components = Object.keys(columns) as Component[];
596
+ const archetype = getArchetype(...components);
597
+ const [archetable] = getTables(archetype, {
598
+ mode: "write",
599
+ exact: true,
600
+ }) as CoreArchetypeImpl<C>[];
601
+ if (archetable.id !== i) {
602
+ throw new Error(
603
+ "Expected archetable to have the same id as the persisted table index"
604
+ );
605
+ }
606
+ archetable.rows = rows;
607
+ for (const [name, columnData] of Object.entries(columns)) {
608
+ const column = (archetable.columns as any)[name];
609
+ column.fromJSON(columnData, rows);
610
+ }
611
+ }
612
+ return true;
613
+ };
614
+
615
+ const ecs: CoreECS = {
616
+ components: componentSchemas,
617
+ resources,
618
+ getComponentValue,
619
+ setComponentValue,
620
+ countEntities,
621
+ selectEntities,
622
+ getTables,
623
+ getArchetype: getArchetype as any,
624
+ getEntityArchetype,
625
+ withComponents,
626
+ withResources,
627
+ getEntityValues,
628
+ createBatch,
629
+ createEntity,
630
+ updateEntity,
631
+ deleteEntity,
632
+ toJSON,
633
+ };
634
+
635
+ if (options.data) {
636
+ fromJSON(options.data);
637
+ }
638
+
639
+ return ecs;
640
+ }
@@ -19,7 +19,12 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
19
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
20
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
21
  SOFTWARE.*/
22
- export * from './array-equals.js';
23
- export * from './is-async-generator.js';
24
- export * from './deep-merge.js';
25
- //# sourceMappingURL=index.js.map
22
+ export type {
23
+ ECSJSON,
24
+ Entity,
25
+ EntityValues,
26
+ EntityCreateValues,
27
+ EntityUpdateValues,
28
+ Table,
29
+ Column,
30
+ } from "./core-ecs-types.js";