fix: forget remembered entities after a while

This commit is contained in:
cha0s 2019-05-02 23:34:07 -05:00
parent 19ff9e2293
commit 6db0c2bcdd
2 changed files with 21 additions and 3 deletions

View File

@ -3,6 +3,6 @@
- ✔ React UI
- ❌ Multiple rooms
- ❌ Send relevant entity URIs to client; cache and use instead of full transfer
- Forget remembered entities after a while
- Forget remembered entities after a while
- ❌ Optimize informed and Packer::pack: don't use .map on immutables
- ❌ Optimize Damaging::tick

View File

@ -1,3 +1,5 @@
import {performance} from 'perf_hooks';
import * as I from 'immutable';
import immutablediff from 'immutablediff';
@ -103,7 +105,9 @@ export class Informed extends decorate(Trait) {
if (!(uuid in this._rememberedEntities)) {
continue;
}
const rememberedEntity = this._rememberedEntities[uuid];
const rememberedEntity = this._rememberedEntities[uuid].entity;
// Reset remembrance timeout.
this._rememberedEntities[uuid].rememberFor = 60;
// Take a diff from what the client remembers to now.
const currentState = state.getIn(
['room', 'layers', layerId, 'entityList', uuid]
@ -163,7 +167,11 @@ export class Informed extends decorate(Trait) {
if (!remembered) {
return;
}
this._rememberedEntities[uuid] = remembered;
this._rememberedEntities[uuid] = {
entity: remembered,
// Remember entities for one minute.
rememeberFor: 60,
};
// Add overrides.
const overrides = this.entityOverrides(
removal.get('path'),
@ -255,4 +263,14 @@ export class Informed extends decorate(Trait) {
};
}
tick(elapsed) {
const rememberedEntities = this._rememberedEntities;
for (const uuid in rememberedEntities) {
const rememberFor = rememberedEntities[uuid].rememberFor -= elapsed;
if (rememberFor <= 0) {
delete rememberedEntities[uuid];
}
}
}
}