fix: memory

This commit is contained in:
cha0s 2024-08-01 14:31:54 -05:00
parent 38d76791f7
commit f5d092efaf

View File

@ -17,19 +17,41 @@ addEventListener('message', (particle) => {
.onEnd(() => {}); .onEnd(() => {});
}); });
const memory = new Set();
let last = performance.now(); let last = performance.now();
function tick(now) { function tick(now) {
const elapsed = (now - last) / 1000; const elapsed = (now - last) / 1000;
last = now; last = now;
if (ecs.get(1)) { requestAnimationFrame(tick);
if (!ecs.get(1)) {
return;
}
ecs.tick(elapsed); ecs.tick(elapsed);
emitter.tick(elapsed); emitter.tick(elapsed);
if ('1' in ecs.diff) { const update = {};
delete ecs.diff['1']; const lastNearby = new Set(memory.values());
for (const id in ecs.$$entities) {
const entity = ecs.$$entities[id];
lastNearby.delete(id);
if (!memory.has(id)) {
update[id] = entity.toJSON();
}
else if (ecs.diff[id]) {
update[id] = ecs.diff[id];
}
memory.add(id);
}
for (const id of lastNearby) {
memory.delete(id);
update[id] = false;
}
if ('1' in update) {
delete update['1'];
}
if (Object.keys(update).length > 0) {
postMessage(update);
} }
postMessage(ecs.diff);
ecs.setClean(); ecs.setClean();
}
requestAnimationFrame(tick);
} }
requestAnimationFrame(tick); requestAnimationFrame(tick);