From 6db0c2bcdd877ff08155dc9bfe4b0e454d82ed4c Mon Sep 17 00:00:00 2001 From: cha0s Date: Thu, 2 May 2019 23:34:07 -0500 Subject: [PATCH] fix: forget remembered entities after a while --- TODO.md | 2 +- server/traits/informed.trait.js | 22 ++++++++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/TODO.md b/TODO.md index 39e6a65..eafa69c 100644 --- a/TODO.md +++ b/TODO.md @@ -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 diff --git a/server/traits/informed.trait.js b/server/traits/informed.trait.js index f73c802..6afa8b1 100644 --- a/server/traits/informed.trait.js +++ b/server/traits/informed.trait.js @@ -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]; + } + } + } + }