@daneren2005/shared-memory-ecs 1.10.4 → 1.10.5
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/dist/component-trigger-index.d.ts +17 -0
- package/dist/{create-system-worker-BU5CnyVh.js → create-system-worker-CkPwxmmr.js} +102 -70
- package/dist/create-system-worker-CkPwxmmr.js.map +1 -0
- package/dist/index.js +250 -248
- package/dist/index.js.map +1 -1
- package/dist/node/node-worker-adapter.d.ts +1 -1
- package/dist/node-worker-self.js +1 -2
- package/dist/node-worker-self.js.map +1 -1
- package/dist/node.js +3 -4
- package/dist/node.js.map +1 -1
- package/dist/systems/entity-worker-system.d.ts +2 -0
- package/dist/systems/workers/entity-system-worker-message.d.ts +12 -3
- package/dist/systems/workers/packed-membership.d.ts +16 -0
- package/dist/worker.js +1 -2
- package/dist/world.d.ts +2 -0
- package/package.json +9 -9
- package/dist/create-system-worker-BU5CnyVh.js.map +0 -1
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export interface ComponentTrigger {
|
|
2
|
+
name: string;
|
|
3
|
+
order: number;
|
|
4
|
+
}
|
|
5
|
+
interface TriggerDefinition {
|
|
6
|
+
loadProperties: Array<string>;
|
|
7
|
+
loadInFinishLoading?: boolean;
|
|
8
|
+
}
|
|
9
|
+
export default class ComponentTriggerIndex {
|
|
10
|
+
private readonly immediate;
|
|
11
|
+
private readonly deferred;
|
|
12
|
+
constructor(registry: Record<string, TriggerDefinition>);
|
|
13
|
+
forLoad(config: object): ReadonlyArray<ComponentTrigger>;
|
|
14
|
+
forFinishLoading(config: object): ReadonlyArray<ComponentTrigger>;
|
|
15
|
+
private find;
|
|
16
|
+
}
|
|
17
|
+
export {};
|
|
@@ -2,7 +2,7 @@ import e from "@daneren2005/shared-memory-objects/memory-heap";
|
|
|
2
2
|
import { getPointer as t } from "@daneren2005/shared-memory-objects/utils/pointer";
|
|
3
3
|
import n from "@daneren2005/shared-memory-objects/shared-pool";
|
|
4
4
|
import r from "@daneren2005/shared-memory-objects/constant-string";
|
|
5
|
-
|
|
5
|
+
// src/memory-component.ts
|
|
6
6
|
var i = class e {
|
|
7
7
|
heap;
|
|
8
8
|
pool;
|
|
@@ -91,7 +91,7 @@ var i = class e {
|
|
|
91
91
|
world;
|
|
92
92
|
entities = /* @__PURE__ */ new Map();
|
|
93
93
|
getWorld(e, t) {
|
|
94
|
-
return
|
|
94
|
+
return t && !t.first && this.world || (this.world = e), this.world;
|
|
95
95
|
}
|
|
96
96
|
getEntities(e, t, n) {
|
|
97
97
|
if (!n) return this.entities.clear(), e;
|
|
@@ -111,8 +111,7 @@ var i = class e {
|
|
|
111
111
|
this.world = void 0, this.entities.clear();
|
|
112
112
|
}
|
|
113
113
|
};
|
|
114
|
-
|
|
115
|
-
//#region src/actions/build-worker-entity.ts
|
|
114
|
+
// src/actions/build-worker-entity.ts
|
|
116
115
|
function c(e, t, n, r, i) {
|
|
117
116
|
let a = t[e.type] ?? {};
|
|
118
117
|
if (i && Object.prototype.hasOwnProperty.call(e, "class")) throw Error("Entity class comes from its type template and cannot be supplied by a worker config");
|
|
@@ -151,15 +150,55 @@ function l(e, t, n, r) {
|
|
|
151
150
|
index: r.allocateComponentBlock(e, i.toBlock(t))
|
|
152
151
|
};
|
|
153
152
|
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
153
|
+
// src/systems/workers/packed-membership.ts
|
|
154
|
+
var u = -1;
|
|
155
|
+
function d(e, t) {
|
|
156
|
+
let n = new Uint32Array(e.size), r = t.map(() => new Int32Array(e.size)), i = 0;
|
|
157
|
+
for (let a of e.values()) {
|
|
158
|
+
n[i] = a.eid;
|
|
159
|
+
for (let e = 0; e < r.length; e++) {
|
|
160
|
+
let n = a.components[t[e]];
|
|
161
|
+
r[e][i] = n?.block ? n.index : u;
|
|
162
|
+
}
|
|
163
|
+
i++;
|
|
164
|
+
}
|
|
165
|
+
let a = {};
|
|
166
|
+
return t.forEach((e, t) => {
|
|
167
|
+
a[e] = r[t];
|
|
168
|
+
}), {
|
|
169
|
+
entityIds: n,
|
|
170
|
+
components: a
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
function f(e) {
|
|
174
|
+
return [e.entityIds.buffer, ...Object.values(e.components).map((e) => e.buffer)];
|
|
175
|
+
}
|
|
176
|
+
function p(e, t) {
|
|
177
|
+
let n = Object.keys(e.components), r = n.map((t) => e.components[t]), i = n.map((e) => t[e]), a = [];
|
|
178
|
+
for (let t = 0; t < e.entityIds.length; t++) {
|
|
179
|
+
let o = {};
|
|
180
|
+
for (let e = 0; e < n.length; e++) {
|
|
181
|
+
let a = r[e][t];
|
|
182
|
+
a !== u && (o[n[e]] = i[e].getBlock(a));
|
|
183
|
+
}
|
|
184
|
+
a.push({
|
|
185
|
+
entityId: e.entityIds[t],
|
|
186
|
+
components: o
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
return a;
|
|
190
|
+
}
|
|
191
|
+
function m(e, t) {
|
|
192
|
+
e.packedAdded && (e.added = p(e.packedAdded, t));
|
|
193
|
+
}
|
|
194
|
+
// src/component.ts
|
|
195
|
+
var h = class {
|
|
157
196
|
index;
|
|
158
197
|
block;
|
|
159
198
|
constructor(e, t) {
|
|
160
199
|
this.block = e, this.index = t;
|
|
161
200
|
}
|
|
162
|
-
},
|
|
201
|
+
}, g = 0, _ = 1, v = 2, y = 3, b = class extends h {
|
|
163
202
|
cache;
|
|
164
203
|
constructor(e, t, n) {
|
|
165
204
|
super(e, t), this.cache = n;
|
|
@@ -182,7 +221,7 @@ var u = class {
|
|
|
182
221
|
set isStatic(e) {
|
|
183
222
|
this.block[1] = +!!e;
|
|
184
223
|
}
|
|
185
|
-
},
|
|
224
|
+
}, x = {
|
|
186
225
|
type: Uint32Array,
|
|
187
226
|
size: 4,
|
|
188
227
|
loadProperties: ["type", "isStatic"],
|
|
@@ -196,42 +235,37 @@ var u = class {
|
|
|
196
235
|
];
|
|
197
236
|
},
|
|
198
237
|
attach(e, t, n) {
|
|
199
|
-
return new
|
|
238
|
+
return new b(t.getBlock(n), n, e.world.constantStrings);
|
|
200
239
|
},
|
|
201
240
|
save(e) {
|
|
202
241
|
let t = {};
|
|
203
242
|
return e.type && (t.type = e.type), e.dead && (t.dead = !0), t;
|
|
204
243
|
}
|
|
205
244
|
};
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
function _(e, t, n) {
|
|
245
|
+
// src/actions/kill-entity-worker.ts
|
|
246
|
+
function S(e, t, n) {
|
|
209
247
|
let r = t.entity;
|
|
210
248
|
r && (r[0] = 1), n.entityDied(e);
|
|
211
249
|
}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
function v(e, t, n) {
|
|
250
|
+
// src/actions/create-entity-worker.ts
|
|
251
|
+
function C(e, t, n) {
|
|
215
252
|
if (!e.buildEntityDescriptor) throw Error("createEntityWorker requires the system to be registered with createsEntities: true and the component registry passed to createEntitySystemWorker");
|
|
216
253
|
n.createEntity(e.buildEntityDescriptor(t));
|
|
217
254
|
}
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
function y(e, t, n, r, i) {
|
|
255
|
+
// src/actions/add-component-worker.ts
|
|
256
|
+
function w(e, t, n, r, i) {
|
|
221
257
|
if (!e.buildComponentDescriptor) throw Error("addComponentWorker requires addsComponents: true and the component registry passed to createEntitySystemWorker");
|
|
222
258
|
i.addComponent(t, e.buildComponentDescriptor(String(n), r));
|
|
223
259
|
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
function b(e, t, n) {
|
|
260
|
+
// src/actions/remove-component-worker.ts
|
|
261
|
+
function T(e, t, n) {
|
|
227
262
|
n.removeComponent(e, t);
|
|
228
263
|
}
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
function x(t, n, r) {
|
|
264
|
+
// src/systems/workers/create-entity-system-worker.ts
|
|
265
|
+
function E(t, n, r) {
|
|
232
266
|
t.addEventListener?.("unhandledrejection", (e) => {
|
|
233
267
|
let n = e.reason, r = Error(n instanceof Error ? n.message : String(n));
|
|
234
|
-
n instanceof Error && (r.name = n.name, r.stack = n.stack),
|
|
268
|
+
n instanceof Error && (r.name = n.name, r.stack = n.stack), D(t, {
|
|
235
269
|
type: "unhandled-rejection",
|
|
236
270
|
error: r
|
|
237
271
|
});
|
|
@@ -243,41 +277,41 @@ function x(t, n, r) {
|
|
|
243
277
|
e.apply(r), t[n] = e.entries;
|
|
244
278
|
}
|
|
245
279
|
return t;
|
|
246
|
-
},
|
|
280
|
+
}, h, g, _, v = {}, y, b, x, S = !1, C = (e) => _?.getString(e) ?? "";
|
|
247
281
|
t.onmessage = function(o) {
|
|
248
282
|
let s = o.data;
|
|
249
|
-
if (s.type === "init")
|
|
283
|
+
if (s.type === "init") D(t, { type: "init-complete" });
|
|
250
284
|
else if (s.type === "load") {
|
|
251
|
-
if (s.heap && (
|
|
285
|
+
if (s.heap && (g = new e(s.heap), _ = new a(g), g.addOnGrowBufferHandlers((e) => D(t, {
|
|
252
286
|
type: "grow-buffer-from-worker",
|
|
253
287
|
buffer: e
|
|
254
|
-
}))),
|
|
255
|
-
|
|
256
|
-
for (let e of Object.keys(s.sharedMemory.components))
|
|
257
|
-
|
|
288
|
+
}))), g && s.sharedMemory) {
|
|
289
|
+
v = {};
|
|
290
|
+
for (let e of Object.keys(s.sharedMemory.components)) v[e] = i.fromSharedMemory(g, s.sharedMemory.components[e]);
|
|
291
|
+
y = g.getSharedAlloc(s.sharedMemory.eidCounter);
|
|
258
292
|
}
|
|
259
|
-
|
|
293
|
+
b = s.factoryConfigs, x = s.factoryClasses, S = !!s.addsComponents, m(s.entities, v), Object.values(s.queries).forEach((e) => m(e, v)), u.apply(s.entities);
|
|
260
294
|
let r = p(s.queries);
|
|
261
|
-
|
|
295
|
+
h = n.init?.({
|
|
262
296
|
gameTime: s.gameTime,
|
|
263
297
|
getString: C,
|
|
264
|
-
heap:
|
|
265
|
-
}, s.data, u.entries, r) ?? void 0,
|
|
266
|
-
} else if (s.type === "grow-buffer")
|
|
298
|
+
heap: g
|
|
299
|
+
}, s.data, u.entries, r) ?? void 0, D(t, { type: "loaded" });
|
|
300
|
+
} else if (s.type === "grow-buffer") g && g.buffers[s.buffer.bufferPosition] === void 0 && g.addSharedBuffer(s.buffer);
|
|
267
301
|
else if (s.type === "reset") {
|
|
268
302
|
d.clear(), u.clear();
|
|
269
303
|
for (let e of Object.keys(f)) delete f[e];
|
|
270
|
-
|
|
304
|
+
h = void 0;
|
|
271
305
|
} else if (s.type === "run") {
|
|
272
|
-
s.world = d.getWorld(s.world, s.workBatch),
|
|
306
|
+
s.world = d.getWorld(s.world, s.workBatch), h && Object.assign(s.world, h), s.world.getString = C, s.world.heap = g;
|
|
273
307
|
let e = {
|
|
274
|
-
allocateEid: () =>
|
|
275
|
-
allocateComponentBlock: (e, t) =>
|
|
308
|
+
allocateEid: () => y ? Atomics.add(y.data, 0, 1) + 1 : 0,
|
|
309
|
+
allocateComponentBlock: (e, t) => v[e].create(t)
|
|
276
310
|
};
|
|
277
|
-
s.world.allocate = e,
|
|
278
|
-
let i = performance.now(), a = [], o = {}, f = [],
|
|
311
|
+
s.world.allocate = e, b && r && (s.world.buildEntityDescriptor = (t) => c(t, b, r, e, x)), S && r && (s.world.buildComponentDescriptor = (t, n) => l(t, n, r, e));
|
|
312
|
+
let i = performance.now(), a = [], o = {}, f = [], m = [], _ = [];
|
|
279
313
|
u.apply(s.entities);
|
|
280
|
-
let
|
|
314
|
+
let w = d.getEntities(u.entries, s.entities, s.workBatch), T = p(s.queries), E = {
|
|
281
315
|
entityComponentChanged(e, t, n, r) {
|
|
282
316
|
a.push({
|
|
283
317
|
entityId: e,
|
|
@@ -310,14 +344,14 @@ function x(t, n, r) {
|
|
|
310
344
|
f.push(e);
|
|
311
345
|
},
|
|
312
346
|
addComponent(e, t) {
|
|
313
|
-
|
|
347
|
+
m.push({
|
|
314
348
|
type: "add",
|
|
315
349
|
entityId: e,
|
|
316
350
|
component: t
|
|
317
351
|
});
|
|
318
352
|
},
|
|
319
353
|
removeComponent(e, t) {
|
|
320
|
-
|
|
354
|
+
m.push({
|
|
321
355
|
type: "remove",
|
|
322
356
|
entityId: e,
|
|
323
357
|
componentName: String(t)
|
|
@@ -326,9 +360,9 @@ function x(t, n, r) {
|
|
|
326
360
|
};
|
|
327
361
|
if (n.queryChanged) {
|
|
328
362
|
for (let [e, t] of Object.entries(s.queries)) if (t.added.length || t.removed.length) try {
|
|
329
|
-
n.queryChanged(s.world, e, t,
|
|
363
|
+
n.queryChanged(s.world, e, t, E);
|
|
330
364
|
} catch (e) {
|
|
331
|
-
|
|
365
|
+
_.push({
|
|
332
366
|
error: e,
|
|
333
367
|
phase: "queryChanged"
|
|
334
368
|
});
|
|
@@ -336,26 +370,26 @@ function x(t, n, r) {
|
|
|
336
370
|
}
|
|
337
371
|
let O = !1;
|
|
338
372
|
if (n.preRun && (!s.workBatch || s.workBatch.first)) try {
|
|
339
|
-
n.preRun(s.world, u.entries,
|
|
373
|
+
n.preRun(s.world, u.entries, T, E);
|
|
340
374
|
} catch (e) {
|
|
341
|
-
O = !0,
|
|
375
|
+
O = !0, _.push({
|
|
342
376
|
error: e,
|
|
343
377
|
phase: "preRun"
|
|
344
378
|
});
|
|
345
379
|
}
|
|
346
380
|
if (!O && n.preBatch) try {
|
|
347
|
-
n.preBatch(s.world, T, E
|
|
381
|
+
n.preBatch(s.world, w, T, E);
|
|
348
382
|
} catch (e) {
|
|
349
|
-
O = !0,
|
|
383
|
+
O = !0, _.push({
|
|
350
384
|
error: e,
|
|
351
385
|
phase: "preBatch"
|
|
352
386
|
});
|
|
353
387
|
}
|
|
354
|
-
O ||
|
|
388
|
+
O || w.forEach((e) => {
|
|
355
389
|
try {
|
|
356
|
-
n(s.world, e.entityId, e.components,
|
|
390
|
+
n(s.world, e.entityId, e.components, T, E);
|
|
357
391
|
} catch (t) {
|
|
358
|
-
|
|
392
|
+
_.push({
|
|
359
393
|
error: t,
|
|
360
394
|
phase: "update",
|
|
361
395
|
entityId: e.entityId
|
|
@@ -363,9 +397,9 @@ function x(t, n, r) {
|
|
|
363
397
|
}
|
|
364
398
|
}), n.entityRemoved && s.entities.removed.forEach((e) => {
|
|
365
399
|
try {
|
|
366
|
-
n.entityRemoved(s.world, e,
|
|
400
|
+
n.entityRemoved(s.world, e, E);
|
|
367
401
|
} catch (t) {
|
|
368
|
-
|
|
402
|
+
_.push({
|
|
369
403
|
error: t,
|
|
370
404
|
phase: "entityRemoved",
|
|
371
405
|
entityId: e
|
|
@@ -373,35 +407,33 @@ function x(t, n, r) {
|
|
|
373
407
|
}
|
|
374
408
|
});
|
|
375
409
|
let k = performance.now() - i;
|
|
376
|
-
d.finish(s.workBatch, O),
|
|
410
|
+
d.finish(s.workBatch, O), D(t, {
|
|
377
411
|
type: "run-complete",
|
|
378
412
|
generation: s.generation,
|
|
379
413
|
runTime: k,
|
|
380
414
|
events: a,
|
|
381
415
|
systemEvents: o,
|
|
382
416
|
created: f,
|
|
383
|
-
componentChanges:
|
|
384
|
-
errors:
|
|
417
|
+
componentChanges: m,
|
|
418
|
+
errors: _,
|
|
385
419
|
workFailed: O
|
|
386
420
|
});
|
|
387
421
|
}
|
|
388
422
|
};
|
|
389
423
|
}
|
|
390
|
-
function
|
|
424
|
+
function D(e, t) {
|
|
391
425
|
e.postMessage(t);
|
|
392
426
|
}
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
function C(e) {
|
|
427
|
+
// src/systems/workers/create-system-worker.ts
|
|
428
|
+
function O(e) {
|
|
396
429
|
let t = (() => {});
|
|
397
430
|
t.preRun = (t, n, r, i) => e(t, r, i);
|
|
398
431
|
let n = e.init;
|
|
399
432
|
return n && (t.init = (e, t, r, i) => n(e, t, i)), e.queryChanged && (t.queryChanged = e.queryChanged), e.entityRemoved && (t.entityRemoved = e.entityRemoved), t;
|
|
400
433
|
}
|
|
401
|
-
function
|
|
402
|
-
|
|
434
|
+
function k(e, t, n) {
|
|
435
|
+
E(e, O(t), n);
|
|
403
436
|
}
|
|
404
|
-
|
|
405
|
-
export { o as _, y as a, m as c, p as d, g as f, s as g, c as h, b as i, d as l, l as m, C as n, v as o, u as p, x as r, _ as s, w as t, f as u, a as v, i as y };
|
|
437
|
+
export { c as _, w as a, a as b, y as c, v as d, x as f, l as g, d as h, T as i, g as l, f as m, O as n, C as o, h as p, E as r, S as s, k as t, _ as u, s as v, i as x, o as y };
|
|
406
438
|
|
|
407
|
-
//# sourceMappingURL=create-system-worker-
|
|
439
|
+
//# sourceMappingURL=create-system-worker-CkPwxmmr.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-system-worker-CkPwxmmr.js","names":["MemoryHeap","SharedPool","ConstantString","MemoryComponent","heap","memory","values","index","dataIndex","array","value","ConstantStringCache","existing","string","pointer","cached","getPointer","QueryMembership","entityId","delta","last","entity","EntityWorkBatchState","batch","world","entities","eid","selected","failed","buildWorkerEntity","template","factoryConfigs","config","classes","merged","entityClass","componentNames","registry","definition","allocator","components","name","prop","buildWorkerComponent","ABSENT_COMPONENT","packEntities","entityIds","members","columns","row","column","component","getPackedTransferables","packed","unpackEntities","names","columnPools","pools","expandPackedAdded","Component","block","DEAD_INDEX","STATIC_INDEX","TYPE_INDEX","CLASS_INDEX","EntityComponentImpl","cache","entityDefinition","typePointer","classPointer","killEntityWorker","callbacks","createEntityWorker","addComponentWorker","componentName","removeComponentWorker","createEntitySystemWorker","reason","event","error","postMessageTyped","scope","workState","queryEntities","applyQueryDeltas","queries","queryKey","deltas","membership","worldExtension","stringCache","eidCounter","factoryClasses","addsComponents","getString","message","e","updateFunction","key","definitions","start","entityEvents","systemEvents","createdEntities","componentChanges","errors","runEntities","args","queryName","err","preRunFailed","runTime","toEntityUpdateFunction","run","init","data","createSystemWorker","runFunction"],"sources":["../src/memory-component.ts","../src/constant-string-cache.ts","../src/systems/workers/query-membership.ts","../src/systems/workers/entity-work-batch.ts","../src/actions/build-worker-entity.ts","../src/systems/workers/packed-membership.ts","../src/component.ts","../src/entity-component.ts","../src/actions/kill-entity-worker.ts","../src/actions/create-entity-worker.ts","../src/actions/add-component-worker.ts","../src/actions/remove-component-worker.ts","../src/systems/workers/create-entity-system-worker.ts","../src/systems/workers/create-system-worker.ts"],"sourcesContent":["import SharedPool from '@daneren2005/shared-memory-objects/shared-pool';\nimport type { SharedPoolMemory } from '@daneren2005/shared-memory-objects/shared-pool';\nimport type MemoryHeap from '@daneren2005/shared-memory-objects/memory-heap';\nimport type { TypedArrayConstructor } from '@daneren2005/shared-memory-objects/interfaces/typed-array-constructor';\n\nexport type ComponentTypedArray = Uint32Array | Int32Array | Float32Array | Float64Array;\n\n// Backed by a SharedPool: all bookkeeping lives in the shared heap, so a worker can reconstruct a handle over\n// the same pool (pass the owner's getSharedMemory() as `memory`) and allocate/read blocks off-thread.\nexport default class MemoryComponent<T extends ComponentTypedArray = ComponentTypedArray> {\n\theap: MemoryHeap;\n\tpool: SharedPool<T>;\n\n\tconstructor(heap: MemoryHeap, type: TypedArrayConstructor<T>, dataLength: number, memory?: SharedPoolMemory) {\n\t\tthis.heap = heap;\n\t\tthis.pool = memory\n\t\t\t? new SharedPool<T>(heap, memory)\n\t\t\t: new SharedPool<T>(heap, {\n\t\t\t\ttype,\n\t\t\t\tdataLength,\n\t\t\t});\n\t}\n\n\t// Reconstructs a handle over an existing pool (from the owner's getSharedMemory()) — used in a worker, where the\n\t// pool's type/dataLength are already recorded in the heap header, so no definition is needed.\n\tstatic fromSharedMemory<T extends ComponentTypedArray = ComponentTypedArray>(heap: MemoryHeap, memory: SharedPoolMemory): MemoryComponent<T> {\n\t\treturn new MemoryComponent<T>(heap, Uint32Array as unknown as TypedArrayConstructor<T>, 1, memory);\n\t}\n\n\tgetSharedMemory(): SharedPoolMemory {\n\t\treturn this.pool.getSharedMemory();\n\t}\n\n\tget length() {\n\t\treturn this.pool.length;\n\t}\n\tget rawLength() {\n\t\treturn this.pool.bufferLength;\n\t}\n\n\tcreate(values: Array<number>): number {\n\t\treturn this.pool.push(values);\n\t}\n\n\tgetBlock(index: number): T {\n\t\treturn this.pool.at(index);\n\t}\n\tget(index: number, dataIndex: number): number {\n\t\treturn this.pool.get(index, dataIndex);\n\t}\n\tset(index: number, dataIndex: number, value: number) {\n\t\tlet array = this.pool.at(index);\n\t\tarray[dataIndex] = value;\n\t}\n\n\tdelete(index: number) {\n\t\tthis.pool.deleteIndex(index);\n\t}\n\tclear() {\n\t\tthis.pool.clear();\n\t}\n}\n","import ConstantString from '@daneren2005/shared-memory-objects/constant-string';\nimport { getPointer } from '@daneren2005/shared-memory-objects/utils/pointer';\nimport type MemoryHeap from '@daneren2005/shared-memory-objects/memory-heap';\n\n// Interns immutable strings in the heap and resolves them back from a pointer. Every distinct value is allocated\n// once as a single ConstantString shared by all users (15 \"Space Ship\" entities point at one allocation), and a\n// pointer resolves to its string through a Map hit before ever rebuilding it from memory. Both sides of the\n// worker boundary hold one: the main thread creates + interns; a worker (with a heap reconstructed from the same\n// SharedArrayBuffers) only ever resolves pointers and caches the results.\nexport default class ConstantStringCache {\n\tprivate heap: MemoryHeap;\n\t// value -> the one interned allocation. Only the creating (main) thread populates this; it owns the memory.\n\tprivate byValue = new Map<string, ConstantString>();\n\t// pointer -> value: the fast lookup every thread checks before touching memory.\n\tprivate byPointer = new Map<number, string>();\n\n\tconstructor(heap: MemoryHeap) {\n\t\tthis.heap = heap;\n\t}\n\n\t// Dedupes on value, so repeated types share a single ConstantString. Main-thread only.\n\tgetOrCreate(value: string): ConstantString {\n\t\tlet existing = this.byValue.get(value);\n\t\tif(existing) {\n\t\t\treturn existing;\n\t\t}\n\n\t\tconst string = new ConstantString(this.heap, value);\n\t\tthis.byValue.set(value, string);\n\t\tthis.byPointer.set(string.pointer, value);\n\n\t\treturn string;\n\t}\n\n\t// pointer -> string, checking the cache before rebuilding from memory. A pointer of 0 is the empty string; a\n\t// pointer into a buffer that has not synced to this thread yet returns undefined.\n\tgetString(pointer: number): string | undefined {\n\t\tif(pointer === 0) {\n\t\t\treturn '';\n\t\t}\n\n\t\tlet cached = this.byPointer.get(pointer);\n\t\tif(cached !== undefined) {\n\t\t\treturn cached;\n\t\t}\n\n\t\tconst memory = getPointer(pointer, this.heap.positionBits);\n\t\tif(this.heap.buffers[memory.bufferPosition] === undefined) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\tconst value = new ConstantString(this.heap, memory).value;\n\t\tthis.byPointer.set(pointer, value);\n\n\t\treturn value;\n\t}\n\n\t// Frees every allocation this cache owns and empties the lookups. A worker's cache owns nothing (it only ever\n\t// resolved views), so this just drops its pointer lookups.\n\tclear() {\n\t\tthis.byValue.forEach(string => string.free());\n\t\tthis.byValue.clear();\n\t\tthis.byPointer.clear();\n\t}\n}\n","import type { EntityUpdateComponents, QueryDelta, UpdateEntityConfigObject } from '../entity-worker-system';\n\n// A query's persistent member list on the worker side, kept from each run's delta. The eid index lives across runs so\n// a delta costs only its own size: removal swaps the last member into the gap, so order is not kept across removals.\n// `entries` is the same array for the membership's whole life; update functions are handed it directly.\nexport default class QueryMembership<T extends EntityUpdateComponents> {\n\treadonly entries: Array<UpdateEntityConfigObject<T>> = [];\n\tprivate readonly indexByEid = new Map<number, number>();\n\n\t// Removals first, so a remove+add of the same eid in one delta keeps the fresh entry.\n\tapply(delta: QueryDelta<T>): void {\n\t\tfor(const entityId of delta.removed) {\n\t\t\tconst index = this.indexByEid.get(entityId);\n\t\t\tif(index === undefined) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tthis.indexByEid.delete(entityId);\n\t\t\tconst last = this.entries.pop()!;\n\t\t\tif(index < this.entries.length) {\n\t\t\t\tthis.entries[index] = last;\n\t\t\t\tthis.indexByEid.set(last.entityId, index);\n\t\t\t}\n\t\t}\n\t\tfor(const entity of delta.added) {\n\t\t\tconst existing = this.indexByEid.get(entity.entityId);\n\t\t\tif(existing !== undefined) {\n\t\t\t\t// Upsert: a re-added entity brings fresh block views.\n\t\t\t\tthis.entries[existing] = entity;\n\t\t\t} else {\n\t\t\t\tthis.indexByEid.set(entity.entityId, this.entries.length);\n\t\t\t\tthis.entries.push(entity);\n\t\t\t}\n\t\t}\n\t}\n\n\tclear(): void {\n\t\tthis.entries.length = 0;\n\t\tthis.indexByEid.clear();\n\t}\n}\n","import type { EntityUpdateComponents, EntityWorkerSystemWorld, QueryDelta, UpdateEntityConfigObject } from '../entity-worker-system';\nimport type { EntityWorkBatch } from './entity-system-worker-message';\n\nexport default class EntityWorkBatchState<T extends EntityUpdateComponents, W extends EntityWorkerSystemWorld> {\n\tprivate world: W | undefined;\n\tprivate entities = new Map<number, UpdateEntityConfigObject<T>>();\n\n\tgetWorld(world: W, batch: EntityWorkBatch | undefined): W {\n\t\tif(!batch || batch.first || !this.world) {\n\t\t\tthis.world = world;\n\t\t}\n\t\treturn this.world;\n\t}\n\tgetEntities(entities: Array<UpdateEntityConfigObject<T>>, delta: QueryDelta<T>, batch: EntityWorkBatch | undefined): Array<UpdateEntityConfigObject<T>> {\n\t\tif(!batch) {\n\t\t\tthis.entities.clear();\n\t\t\treturn entities;\n\t\t}\n\t\tif(!this.entities.size) {\n\t\t\tfor(const entity of entities) {\n\t\t\t\tthis.entities.set(entity.entityId, entity);\n\t\t\t}\n\t\t}\n\t\tdelta.removed.forEach(eid => this.entities.delete(eid));\n\t\tdelta.added.forEach(entity => this.entities.set(entity.entityId, entity));\n\t\tconst selected: Array<UpdateEntityConfigObject<T>> = [];\n\t\tfor(const eid of batch.entityIds) {\n\t\t\tconst entity = this.entities.get(eid);\n\t\t\tif(entity) {\n\t\t\t\tselected.push(entity);\n\t\t\t}\n\t\t}\n\t\treturn selected;\n\t}\n\tfinish(batch: EntityWorkBatch | undefined, failed: boolean): void {\n\t\tif(!batch || batch.last || failed) {\n\t\t\tthis.world = undefined;\n\t\t}\n\t}\n\tclear(): void {\n\t\tthis.world = undefined;\n\t\tthis.entities.clear();\n\t}\n}\n","import type { WorkerAllocator, WorkerCreateEntityConfig, WorkerCreatedComponent, WorkerCreatedEntity } from '../systems/entity-worker-system';\nimport type { WorkerEntityClassRegistry } from '../entity-class';\n\n// The slice of a component definition worker-side creation needs. Both the game's ComponentDefinitionMap and the\n// world's RegisteredComponentRegistry satisfy it.\nexport interface WorkerCreatableComponent {\n\tloadProperties: Array<string>\n\tloadInFinishLoading?: boolean\n\ttoBlock(config: Record<string, unknown>): Array<number>\n}\nexport type WorkerCreateRegistry = { [name: string]: WorkerCreatableComponent };\nexport type FactoryConfigs = { [type: string]: Record<string, unknown> };\n\n// Turns a factory-style config ({ type, ...overrides }) into a WorkerCreatedEntity, off-thread: layers the type's\n// factory template under the overrides, mints an id, and allocates + writes each triggered component's block via its\n// toBlock(). The always-present `entity` component is NOT built here - the main thread builds it on adopt (interning\n// the type is main-thread-only), reading `type`/`isStatic` off this descriptor. Shared by the real worker and the\n// main-thread fallback so both behave identically.\nexport function buildWorkerEntity(\n\tconfig: WorkerCreateEntityConfig,\n\tfactoryConfigs: FactoryConfigs,\n\tregistry: WorkerCreateRegistry,\n\tallocator: WorkerAllocator,\n\tclasses?: WorkerEntityClassRegistry,\n): WorkerCreatedEntity {\n\tconst template = factoryConfigs[config.type] ?? {};\n\tif(classes && Object.prototype.hasOwnProperty.call(config, 'class')) {\n\t\tthrow new Error('Entity class comes from its type template and cannot be supplied by a worker config');\n\t}\n\tconst merged: Record<string, unknown> = { ...template, ...config };\n\tconst entityClass = typeof template.class === 'string' ? template.class : undefined;\n\tlet componentNames = Object.keys(registry);\n\tif(classes) {\n\t\tif(template.type !== config.type) {\n\t\t\tthrow new Error(`Entity template ${config.type} must declare the same type`);\n\t\t}\n\t\tconst definition = entityClass ? classes[entityClass] : undefined;\n\t\tif(!definition) {\n\t\t\tthrow new Error(`Entity type ${config.type} has unknown class ${entityClass ?? ''}`);\n\t\t}\n\t\tcomponentNames = definition.components;\n\t}\n\n\tconst eid = allocator.allocateEid();\n\tconst components: { [name: string]: number } = {};\n\tfor(let name of componentNames) {\n\t\t// The entity component is always built on the main thread when the descriptor is adopted (interning the type),\n\t\t// never off-thread - skip it here whether or not the registry includes it.\n\t\tif(name === 'entity') {\n\t\t\tcontinue;\n\t\t}\n\t\tconst definition = registry[name];\n\t\tif(!definition) {\n\t\t\tthrow new Error(`Entity class ${entityClass ?? ''} contains unknown component ${name}`);\n\t\t}\n\t\t// Deferred components need the whole world (other entities) that a worker doesn't have; skip them.\n\t\tif(definition.loadInFinishLoading) {\n\t\t\tcontinue;\n\t\t}\n\t\tif(definition.loadProperties.some(prop => prop in merged)) {\n\t\t\tcomponents[name] = allocator.allocateComponentBlock(name, definition.toBlock(merged));\n\t\t}\n\t}\n\n\treturn {\n\t\teid,\n\t\ttype: config.type,\n\t\tclass: entityClass,\n\t\tisStatic: merged.isStatic as boolean | undefined,\n\t\tcomponents,\n\t};\n}\n\nexport function buildWorkerComponent(\n\tname: string,\n\tconfig: Record<string, unknown>,\n\tregistry: WorkerCreateRegistry,\n\tallocator: WorkerAllocator,\n): WorkerCreatedComponent {\n\tif(name === 'entity') {\n\t\tthrow new Error('The entity component cannot be added at runtime');\n\t}\n\tconst definition = registry[name];\n\tif(!definition) {\n\t\tthrow new Error(`Unknown component: ${name}`);\n\t}\n\tif(definition.loadInFinishLoading) {\n\t\tthrow new Error(`Component ${name} requires main-thread finishLoading and cannot be added in a worker`);\n\t}\n\n\treturn {\n\t\tname,\n\t\tindex: allocator.allocateComponentBlock(name, definition.toBlock(config)),\n\t};\n}\n","import type BaseEntity from '../../entity';\nimport type { ComponentMap } from '../../component-definition';\nimport type MemoryComponent from '../../memory-component';\nimport type { EntityUpdateComponents, UpdateEntityConfigObject } from '../entity-worker-system';\nimport type { LoadQueryDelta, PackedEntities } from './entity-system-worker-message';\n\nconst ABSENT_COMPONENT = -1;\n\nexport function packEntities<C extends ComponentMap>(\n\tmembers: { size: number, values(): Iterable<BaseEntity<C>> },\n\tcomponentNames: ReadonlyArray<keyof C>,\n): PackedEntities {\n\tconst entityIds = new Uint32Array(members.size);\n\tconst columns = componentNames.map(() => new Int32Array(members.size));\n\tlet row = 0;\n\tfor(const entity of members.values()) {\n\t\tentityIds[row] = entity.eid;\n\t\tfor(let column = 0; column < columns.length; column++) {\n\t\t\tconst component = entity.components[componentNames[column]];\n\t\t\tcolumns[column][row] = component?.block ? component.index : ABSENT_COMPONENT;\n\t\t}\n\t\trow++;\n\t}\n\n\tconst components: PackedEntities['components'] = {};\n\tcomponentNames.forEach((name, column) => {\n\t\tcomponents[name as string] = columns[column];\n\t});\n\treturn { entityIds, components };\n}\n\nexport function getPackedTransferables(packed: PackedEntities): Array<Transferable> {\n\treturn [packed.entityIds.buffer, ...Object.values(packed.components).map(column => column.buffer)];\n}\n\nexport function unpackEntities<T extends EntityUpdateComponents>(\n\tpacked: PackedEntities,\n\tpools: { [name: string]: MemoryComponent },\n): Array<UpdateEntityConfigObject<T>> {\n\tconst names = Object.keys(packed.components) as Array<keyof T & string>;\n\tconst columns = names.map(name => packed.components[name]);\n\tconst columnPools = names.map(name => pools[name]);\n\tconst entities: Array<UpdateEntityConfigObject<T>> = [];\n\tfor(let row = 0; row < packed.entityIds.length; row++) {\n\t\tconst components = {} as T;\n\t\tfor(let column = 0; column < names.length; column++) {\n\t\t\tconst index = columns[column][row];\n\t\t\tif(index !== ABSENT_COMPONENT) {\n\t\t\t\tcomponents[names[column]] = columnPools[column].getBlock(index) as T[keyof T & string];\n\t\t\t}\n\t\t}\n\t\tentities.push({ entityId: packed.entityIds[row], components });\n\t}\n\treturn entities;\n}\n\nexport function expandPackedAdded(delta: LoadQueryDelta, pools: { [name: string]: MemoryComponent }): void {\n\tif(delta.packedAdded) {\n\t\tdelta.added = unpackEntities(delta.packedAdded, pools);\n\t}\n}\n","import type { ComponentTypedArray } from './memory-component';\nimport type { BaseComponent } from './component-definition';\n\n// Base class for a memory-backed component accessor. Subclass it, declare prototype get/set accessors over\n// `this.block` indexed by the component's exported *_INDEX constants, and return `new YourComponent(block, index)`\n// from `attach`. Because the accessors live on one shared prototype - not fresh closures captured per entity -\n// reads off thousands of entities every frame stay monomorphic and inline, and constructing a component allocates\n// just the instance instead of a closure per accessor. Components that own no extra memory need nothing else; a\n// subclass that does can take more constructor args (the entity, another pool) and store them as fields.\nexport default abstract class Component<T extends ComponentTypedArray = ComponentTypedArray> implements BaseComponent {\n\treadonly index: number;\n\treadonly block: T;\n\n\tconstructor(block: T, index: number) {\n\t\tthis.block = block;\n\t\tthis.index = index;\n\t}\n}\n","import type { ComponentDefinition } from './component-definition';\nimport Component from './component';\nimport type ConstantStringCache from './constant-string-cache';\n\nexport interface EntityComponent {\n\tindex: number\n\treadonly type: string\n\treadonly class: string\n\tdead: boolean\n\tisStatic: boolean\n}\n\nexport interface EntityComponentConfig {\n\ttype: string\n\tisStatic?: boolean\n}\n\nexport interface EntityClassConfig {\n\treadonly class: string\n}\n\nexport interface EntityComponentSerialization {\n\ttype?: string\n\tdead?: boolean\n}\n\nexport const DEAD_INDEX = 0;\nexport const STATIC_INDEX = 1;\nexport const TYPE_INDEX = 2;\nexport const CLASS_INDEX = 3;\n\n// Identity strings are interned, so the accessor needs the world's cache to resolve their pointers.\nclass EntityComponentImpl extends Component<Uint32Array> implements EntityComponent {\n\tprivate cache: ConstantStringCache;\n\n\tconstructor(block: Uint32Array, index: number, cache: ConstantStringCache) {\n\t\tsuper(block, index);\n\t\tthis.cache = cache;\n\t}\n\n\tget type() {\n\t\treturn this.cache.getString(this.block[TYPE_INDEX]) ?? '';\n\t}\n\tget class() {\n\t\treturn this.cache.getString(this.block[CLASS_INDEX]) ?? '';\n\t}\n\tget dead() {\n\t\treturn this.block[DEAD_INDEX] === 1;\n\t}\n\tset dead(value: boolean) {\n\t\tthis.block[DEAD_INDEX] = value ? 1 : 0;\n\t}\n\tget isStatic() {\n\t\treturn this.block[STATIC_INDEX] === 1;\n\t}\n\tset isStatic(value: boolean) {\n\t\tthis.block[STATIC_INDEX] = value ? 1 : 0;\n\t}\n}\n\nexport const entityDefinition: ComponentDefinition<EntityComponent, Uint32Array, EntityComponentConfig & Partial<EntityClassConfig>, EntityComponentSerialization> = {\n\ttype: Uint32Array,\n\tsize: 4,\n\tloadProperties: ['type', 'isStatic'],\n\t// The only component that reads the entity (never worker-created): it interns its identity strings through the heap and\n\t// stores the pointer. `entity` is always passed here since loadComponent runs on the main thread.\n\ttoBlock(config, entity) {\n\t\tconst cache = entity!.world.constantStrings;\n\t\tconst typePointer = config.type ? cache.getOrCreate(config.type).pointer : 0;\n\t\tconst classPointer = config.class ? cache.getOrCreate(config.class).pointer : 0;\n\t\treturn [config.dead ? 1 : 0, config.isStatic ? 1 : 0, typePointer, classPointer];\n\t},\n\tattach(entity, memory, index) {\n\t\treturn new EntityComponentImpl(memory.getBlock(index), index, entity.world.constantStrings);\n\t},\n\tsave(component) {\n\t\tconst config: EntityComponentSerialization = {};\n\t\tif(component.type) {\n\t\t\tconfig.type = component.type;\n\t\t}\n\t\tif(component.dead) {\n\t\t\tconfig.dead = true;\n\t\t}\n\n\t\treturn config;\n\t},\n};\n","import type { ComponentTypedArray } from '../memory-component';\nimport type { EntityWorkerSystemCallbacks, EntityUpdateComponents } from '../systems/entity-worker-system';\nimport { DEAD_INDEX } from '../entity-component';\n\n// Worker-side killEntity: flags the entity dead in its block and reports the death back via callbacks. The\n// entity component must be in the system's query for its block to be available here.\nexport default function killEntityWorker(entityId: number, components: EntityUpdateComponents, callbacks: EntityWorkerSystemCallbacks): void {\n\tconst block = (components as { entity?: ComponentTypedArray }).entity;\n\tif(block) {\n\t\tblock[DEAD_INDEX] = 1;\n\t}\n\n\tcallbacks.entityDied(entityId);\n}\n","import type { EntityWorkerSystemCallbacks, EntityWorkerSystemWorld, WorkerCreateEntityConfig } from '../systems/entity-worker-system';\n\n// Worker-side entity creation from a factory config. The worker merges the selected type template, resolves its\n// optional class, mints a unique id, and allocates + writes every allowed, triggered component block\n// directly into the shared pools (off-thread) via each component's toBlock(). It reports the descriptor back; the main\n// thread adopts it on run-complete (world.adoptEntity), building the `entity` component and class wrapper there\n// worker-written blocks, so the entity first exists on the following frame.\n//\n// Requires the system to be registered with `createsEntities: true` and its worker entry to pass the component\n// registry to createEntitySystemWorker (so the worker has each component's toBlock).\nexport default function createEntityWorker(world: EntityWorkerSystemWorld, config: WorkerCreateEntityConfig, callbacks: EntityWorkerSystemCallbacks): void {\n\tif(!world.buildEntityDescriptor) {\n\t\tthrow new Error('createEntityWorker requires the system to be registered with createsEntities: true and the component registry passed to createEntitySystemWorker');\n\t}\n\n\tcallbacks.createEntity(world.buildEntityDescriptor(config));\n}\n","import type { ComponentMap } from '../component-definition';\nimport type { EntityWorkerSystemCallbacks, EntityWorkerSystemWorld } from '../systems/entity-worker-system';\n\n// Allocates a component block in the worker and asks the main thread to attach it after the run. The entity's\n// current query snapshot is intentionally unchanged until the next run.\nexport default function addComponentWorker<C extends ComponentMap, K extends keyof C>(\n\tworld: EntityWorkerSystemWorld,\n\tentityId: number,\n\tcomponentName: K,\n\tconfig: Record<string, unknown>,\n\tcallbacks: EntityWorkerSystemCallbacks<C>,\n): void {\n\tif(!world.buildComponentDescriptor) {\n\t\tthrow new Error('addComponentWorker requires addsComponents: true and the component registry passed to createEntitySystemWorker');\n\t}\n\n\tcallbacks.addComponent(entityId, world.buildComponentDescriptor(String(componentName), config));\n}\n","import type { ComponentMap } from '../component-definition';\nimport type { EntityWorkerSystemCallbacks } from '../systems/entity-worker-system';\n\n// Requests a main-thread detach after the worker run. Shared memory is freed only after every system that could\n// still hold the old block has completed a run.\nexport default function removeComponentWorker<C extends ComponentMap, K extends keyof C>(\n\tentityId: number,\n\tcomponentName: K,\n\tcallbacks: EntityWorkerSystemCallbacks<C>,\n): void {\n\tcallbacks.removeComponent(entityId, componentName);\n}\n","import MemoryHeap from '@daneren2005/shared-memory-objects/memory-heap';\nimport type AllocatedMemory from '@daneren2005/shared-memory-objects/allocated-memory';\nimport type EntitySystemWorkerMessage from './entity-system-worker-message';\nimport type { EntityEvent, SystemEvents, WorkerRunError } from './entity-system-worker-message';\nimport ConstantStringCache from '../../constant-string-cache';\nimport MemoryComponent from '../../memory-component';\nimport type { ComponentDefinitionMap, ComponentMap } from '../../component-definition';\nimport type {\n\tEntityWorkerSystemCallbacks, EntityWorkerSystemWorld, EntityQueryComponents, EntityUpdateComponents,\n\tEntityUpdateFunction, QueryDelta, WorkerComponentChange, WorkerCreatedEntity,\n} from '../entity-worker-system';\nimport { buildWorkerComponent, buildWorkerEntity, type FactoryConfigs } from '../../actions/build-worker-entity';\nimport type { WorkerEntityClassRegistry } from '../../entity-class';\nimport QueryMembership from './query-membership';\nimport EntityWorkBatchState from './entity-work-batch';\nimport { expandPackedAdded } from './packed-membership';\n\n// The slice of the worker global scope createEntitySystemWorker touches. Passing `self` explicitly (rather than\n// using the global) lets runners like @vitest/web-worker, which inject `self` as a module local, drive it.\nexport interface EntitySystemWorkerScope {\n\tonmessage: ((e: MessageEvent) => void) | null\n\tpostMessage(message: EntitySystemWorkerMessage): void\n\taddEventListener?(type: 'unhandledrejection', listener: (event: PromiseRejectionEvent) => void): void\n}\n\nexport default function createEntitySystemWorker<\n\tC extends ComponentMap,\n\tT extends EntityUpdateComponents<C>,\n\tW extends EntityWorkerSystemWorld = EntityWorkerSystemWorld,\n\tD = unknown,\n>(scope: EntitySystemWorkerScope, updateFunction: EntityUpdateFunction<C, T, W, D>, definitions?: ComponentDefinitionMap) {\n\tscope.addEventListener?.('unhandledrejection', event => {\n\t\tconst reason: unknown = event.reason;\n\t\t// Copy error details without a potentially uncloneable rejection reason or cause.\n\t\tconst error = new Error(reason instanceof Error ? reason.message : String(reason));\n\t\tif(reason instanceof Error) {\n\t\t\terror.name = reason.name;\n\t\t\terror.stack = reason.stack;\n\t\t}\n\t\tpostMessageTyped(scope, { type: 'unhandled-rejection', error });\n\t});\n\n\t// Persistent lists, carried across runs and mutated by each run's delta.\n\tconst entities = new QueryMembership<T>();\n\tconst workState = new EntityWorkBatchState<T, W>();\n\tconst queryEntities: { [key: string]: QueryMembership<T> } = {};\n\tconst applyQueryDeltas = (deltas: { [key: string]: QueryDelta }): EntityQueryComponents<C> => {\n\t\tconst queries: EntityQueryComponents<C> = {};\n\t\tfor(const [queryKey, delta] of Object.entries(deltas)) {\n\t\t\tconst membership = queryEntities[queryKey] ??= new QueryMembership<T>();\n\t\t\tmembership.apply(delta as QueryDelta<T>);\n\t\t\tqueries[queryKey] = membership.entries;\n\t\t}\n\t\treturn queries;\n\t};\n\t// What updateFunction.init returned: persistent state (e.g. a seeded RNG) merged onto `world` each run.\n\tlet worldExtension: Partial<W> | undefined;\n\tlet heap: MemoryHeap | undefined;\n\tlet stringCache: ConstantStringCache | undefined;\n\t// Reconstructed pool handles over the world's shared state, plus the factory templates - for off-thread entity\n\t// allocation. `definitions` (passed by the game's worker entry) supplies each component's toBlock/loadProperties.\n\tlet pools: { [name: string]: MemoryComponent } = {};\n\tlet eidCounter: AllocatedMemory | undefined;\n\tlet factoryConfigs: FactoryConfigs | undefined;\n\tlet factoryClasses: WorkerEntityClassRegistry | undefined;\n\tlet addsComponents = false;\n\tconst getString = (pointer: number): string => stringCache?.getString(pointer) ?? '';\n\n\tscope.onmessage = function(e) {\n\t\tconst message = e.data as EntitySystemWorkerMessage<W, D>;\n\n\t\tif(message.type === 'init') {\n\t\t\tpostMessageTyped(scope, {\n\t\t\t\ttype: 'init-complete',\n\t\t\t});\n\t\t} else if(message.type === 'load') {\n\t\t\tif(message.heap) {\n\t\t\t\theap = new MemoryHeap(message.heap);\n\t\t\t\tstringCache = new ConstantStringCache(heap);\n\t\t\t\t// Report any buffer this worker grows (while allocating off-thread) back to the main thread, which adopts\n\t\t\t\t// it and fans it out to sibling workers.\n\t\t\t\theap.addOnGrowBufferHandlers(buffer => postMessageTyped(scope, { type: 'grow-buffer-from-worker', buffer }));\n\t\t\t}\n\t\t\tif(heap && message.sharedMemory) {\n\t\t\t\tpools = {};\n\t\t\t\tfor(const name of Object.keys(message.sharedMemory.components)) {\n\t\t\t\t\tpools[name] = MemoryComponent.fromSharedMemory(heap, message.sharedMemory.components[name]);\n\t\t\t\t}\n\t\t\t\teidCounter = heap.getSharedAlloc(message.sharedMemory.eidCounter);\n\t\t\t}\n\t\t\tfactoryConfigs = message.factoryConfigs;\n\t\t\tfactoryClasses = message.factoryClasses;\n\t\t\taddsComponents = !!message.addsComponents;\n\t\t\texpandPackedAdded(message.entities, pools);\n\t\t\tObject.values(message.queries).forEach(delta => expandPackedAdded(delta, pools));\n\t\t\tentities.apply(message.entities as QueryDelta<T>);\n\t\t\tconst queries = applyQueryDeltas(message.queries);\n\t\t\tworldExtension = updateFunction.init?.({ gameTime: message.gameTime, getString, heap }, message.data, entities.entries, queries) ?? undefined;\n\t\t\tpostMessageTyped(scope, {\n\t\t\t\ttype: 'loaded',\n\t\t\t});\n\t\t} else if(message.type === 'grow-buffer') {\n\t\t\t// Never replace a buffer this worker already holds: the main thread fans a worker-grown buffer back out to\n\t\t\t// every worker (including the one that grew it), and overwriting our own live MemoryBuffer would discard its\n\t\t\t// allocation bookkeeping. The SharedArrayBuffer at that position is already the same one.\n\t\t\tif(heap && heap.buffers[message.buffer.bufferPosition] === undefined) {\n\t\t\t\theap.addSharedBuffer(message.buffer);\n\t\t\t}\n\t\t} else if(message.type === 'reset') {\n\t\t\tworkState.clear();\n\t\t\t// Drop the persistent lists so a reused world starts empty; worldExtension is refreshed by the next load.\n\t\t\tentities.clear();\n\t\t\tfor(const key of Object.keys(queryEntities)) {\n\t\t\t\tdelete queryEntities[key];\n\t\t\t}\n\t\t\tworldExtension = undefined;\n\t\t} else if(message.type === 'run') {\n\t\t\tmessage.world = workState.getWorld(message.world, message.workBatch);\n\t\t\tif(worldExtension) {\n\t\t\t\tObject.assign(message.world, worldExtension);\n\t\t\t}\n\t\t\tmessage.world.getString = getString;\n\t\t\tmessage.world.heap = heap;\n\t\t\tconst allocator = {\n\t\t\t\tallocateEid: () => eidCounter ? Atomics.add(eidCounter.data, 0, 1) + 1 : 0,\n\t\t\t\tallocateComponentBlock: (name: string, values: Array<number>) => pools[name].create(values),\n\t\t\t};\n\t\t\tmessage.world.allocate = allocator;\n\t\t\t// Only a system registered with createsEntities (factoryConfigs shipped) whose worker entry passed the\n\t\t\t// component definitions can create entities from a config.\n\t\t\tif(factoryConfigs && definitions) {\n\t\t\t\tmessage.world.buildEntityDescriptor = config => buildWorkerEntity(config, factoryConfigs!, definitions, allocator, factoryClasses);\n\t\t\t}\n\t\t\tif(addsComponents && definitions) {\n\t\t\t\tmessage.world.buildComponentDescriptor = (name, config) => buildWorkerComponent(name, config, definitions, allocator);\n\t\t\t}\n\t\t\tconst start = performance.now();\n\t\t\tlet entityEvents: Array<EntityEvent> = [];\n\t\t\tlet systemEvents: SystemEvents = {};\n\t\t\tlet createdEntities: Array<WorkerCreatedEntity> = [];\n\t\t\tlet componentChanges: Array<WorkerComponentChange> = [];\n\t\t\tlet errors: Array<WorkerRunError> = [];\n\n\t\t\tentities.apply(message.entities as QueryDelta<T>);\n\t\t\tconst runEntities = workState.getEntities(entities.entries, message.entities as QueryDelta<T>, message.workBatch);\n\t\t\tconst queries = applyQueryDeltas(message.queries);\n\n\t\t\tlet callbacks: EntityWorkerSystemCallbacks<C> = {\n\t\t\t\tentityComponentChanged<K extends keyof C, P extends keyof C[K]>(entityId: number, componentName: K, prop: P, value: C[K][P]) {\n\t\t\t\t\tentityEvents.push({\n\t\t\t\t\t\tentityId,\n\t\t\t\t\t\tevent: 'component-property-updated',\n\t\t\t\t\t\targs: [componentName, prop, value],\n\t\t\t\t\t});\n\t\t\t\t},\n\t\t\t\temitEntityEvent(entityId: number, event: string, ...args: Array<unknown>) {\n\t\t\t\t\tentityEvents.push({\n\t\t\t\t\t\tentityId,\n\t\t\t\t\t\tevent,\n\t\t\t\t\t\targs,\n\t\t\t\t\t});\n\t\t\t\t},\n\t\t\t\temitSystemEvent(event: string, entityId: number) {\n\t\t\t\t\t(systemEvents[event] ?? (systemEvents[event] = [])).push(entityId);\n\t\t\t\t},\n\t\t\t\tentityDied(entityId: number) {\n\t\t\t\t\tentityEvents.push({\n\t\t\t\t\t\tentityId,\n\t\t\t\t\t\tevent: 'death',\n\t\t\t\t\t\targs: [],\n\t\t\t\t\t});\n\t\t\t\t},\n\t\t\t\tcreateEntity(entity: WorkerCreatedEntity) {\n\t\t\t\t\tcreatedEntities.push(entity);\n\t\t\t\t},\n\t\t\t\taddComponent(entityId, component) {\n\t\t\t\t\tcomponentChanges.push({ type: 'add', entityId, component });\n\t\t\t\t},\n\t\t\t\tremoveComponent(entityId, componentName) {\n\t\t\t\t\tcomponentChanges.push({ type: 'remove', entityId, componentName: String(componentName) });\n\t\t\t\t},\n\t\t\t};\n\t\t\tif(updateFunction.queryChanged) {\n\t\t\t\tfor(const [queryName, delta] of Object.entries(message.queries)) {\n\t\t\t\t\tif(!delta.added.length && !delta.removed.length) {\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t\ttry {\n\t\t\t\t\t\tupdateFunction.queryChanged(message.world, queryName, delta, callbacks);\n\t\t\t\t\t} catch(err) {\n\t\t\t\t\t\terrors.push({ error: err as Error, phase: 'queryChanged' });\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tlet preRunFailed = false;\n\t\t\tif(updateFunction.preRun && (!message.workBatch || message.workBatch.first)) {\n\t\t\t\ttry {\n\t\t\t\t\tupdateFunction.preRun(message.world, entities.entries, queries, callbacks);\n\t\t\t\t} catch(err) {\n\t\t\t\t\tpreRunFailed = true;\n\t\t\t\t\terrors.push({ error: err as Error, phase: 'preRun' });\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// preRun sets up the run's state; if it threw, skip the entity loop rather than run over half-prepared data.\n\t\t\tif(!preRunFailed && updateFunction.preBatch) {\n\t\t\t\ttry {\n\t\t\t\t\tupdateFunction.preBatch(message.world, runEntities, queries, callbacks);\n\t\t\t\t} catch(err) {\n\t\t\t\t\tpreRunFailed = true;\n\t\t\t\t\terrors.push({ error: err as Error, phase: 'preBatch' });\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(!preRunFailed) {\n\t\t\t\trunEntities.forEach(entity => {\n\t\t\t\t\ttry {\n\t\t\t\t\t\tupdateFunction(message.world, entity.entityId, entity.components, queries, callbacks);\n\t\t\t\t\t} catch(err) {\n\t\t\t\t\t\t// One entity failing must not stop the rest of the run.\n\t\t\t\t\t\terrors.push({ error: err as Error, phase: 'update', entityId: entity.entityId });\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tif(updateFunction.entityRemoved) {\n\t\t\t\tmessage.entities.removed.forEach(entityId => {\n\t\t\t\t\ttry {\n\t\t\t\t\t\tupdateFunction.entityRemoved!(message.world, entityId, callbacks);\n\t\t\t\t\t} catch(err) {\n\t\t\t\t\t\terrors.push({ error: err as Error, phase: 'entityRemoved', entityId });\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t}\n\t\t\tconst runTime = performance.now() - start;\n\t\t\tworkState.finish(message.workBatch, preRunFailed);\n\n\t\t\tpostMessageTyped(scope, {\n\t\t\t\ttype: 'run-complete',\n\t\t\t\tgeneration: message.generation,\n\t\t\t\trunTime,\n\t\t\t\tevents: entityEvents,\n\t\t\t\tsystemEvents,\n\t\t\t\tcreated: createdEntities,\n\t\t\t\tcomponentChanges,\n\t\t\t\terrors,\n\t\t\t\tworkFailed: preRunFailed,\n\t\t\t});\n\t\t}\n\t};\n}\n\nfunction postMessageTyped(scope: EntitySystemWorkerScope, message: EntitySystemWorkerMessage) {\n\tscope.postMessage(message);\n}\n","import createEntitySystemWorker, { type EntitySystemWorkerScope } from './create-entity-system-worker';\nimport type { ComponentDefinitionMap, ComponentMap } from '../../component-definition';\nimport type {\n\tEntityWorkerSystemCallbacks, EntityWorkerSystemInitWorld, EntityWorkerSystemWorld, EntityQueryComponents, EntityUpdateComponents,\n\tEntityUpdateFunction, EntityRemovedFunction, EntityQueryChangedFunction,\n} from '../entity-worker-system';\n\n// A WorkerSystem's single per-run function: called once per run over the named sub-queries, not once per entity.\n// Optional `init`/`queryChanged`/`entityRemoved` mirror EntityUpdateFunction's hooks.\nexport type WorkerSystemRunFunction<\n\tC extends ComponentMap,\n\tW extends EntityWorkerSystemWorld = EntityWorkerSystemWorld,\n\tD = unknown,\n> = ((world: W, queries: EntityQueryComponents<C>, callbacks: EntityWorkerSystemCallbacks<C>) => void) & {\n\tinit?: WorkerSystemInitFunction<C, W, D>\n\tqueryChanged?: EntityQueryChangedFunction<C, W>\n\tentityRemoved?: EntityRemovedFunction<C, W>\n};\n// No entities argument: a WorkerSystem has no main query.\nexport type WorkerSystemInitFunction<\n\tC extends ComponentMap,\n\tW extends EntityWorkerSystemWorld = EntityWorkerSystemWorld,\n\tD = unknown,\n> = (world: EntityWorkerSystemInitWorld, data: D | undefined, queries: EntityQueryComponents<C>) => Partial<W> | void;\n\n// A WorkerSystem reuses the EntityWorkerSystem worker machinery: the single run function becomes `preRun` (which runs\n// once with query access) and the per-entity body is a no-op, since a WorkerSystem never populates its main query.\nexport function toEntityUpdateFunction<\n\tC extends ComponentMap,\n\tW extends EntityWorkerSystemWorld = EntityWorkerSystemWorld,\n\tD = unknown,\n>(run: WorkerSystemRunFunction<C, W, D>): EntityUpdateFunction<C, EntityUpdateComponents<C>, W, D> {\n\tconst updateFunction = (() => {}) as EntityUpdateFunction<C, EntityUpdateComponents<C>, W, D>;\n\tupdateFunction.preRun = (world, _entities, queries, callbacks) => run(world, queries, callbacks);\n\tconst init = run.init;\n\tif(init) {\n\t\tupdateFunction.init = (world, data, _entities, queries) => init(world, data, queries);\n\t}\n\tif(run.queryChanged) {\n\t\tupdateFunction.queryChanged = run.queryChanged;\n\t}\n\tif(run.entityRemoved) {\n\t\tupdateFunction.entityRemoved = run.entityRemoved;\n\t}\n\n\treturn updateFunction;\n}\n\n// Worker entry helper for a WorkerSystem (mirrors createEntitySystemWorker). Pass the component registry only for a\n// worker that creates entities.\nexport default function createSystemWorker<\n\tC extends ComponentMap,\n\tW extends EntityWorkerSystemWorld = EntityWorkerSystemWorld,\n\tD = unknown,\n>(scope: EntitySystemWorkerScope, runFunction: WorkerSystemRunFunction<C, W, D>, definitions?: Compon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