thomasboyt / coquette-inspect

A Chrome DevTools extension for inspecting games made with the Coquette framework

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Attach a unique ID to each entity

thomasboyt opened this issue · comments

When entities are listed, their position in the entities array is currently used as the React key. However, this means that an entity being moved in position (i.e. if a prior entity is removed) will require the DOM for that entity to be entirely recomputed, which is a big overhead. It also makes it extremely hard to subscribe to the state of a specific entity.

So, there are two changes that need to happen:

  • Each entity needs to have a unique UUID
  • A method needs to exist for the agent to look up an entity by UUID

There's a couple options to implement UUIDs -

  • Monkey-patch entities.create to generate a UUID and store it in the entity, then iterate over the current entities list and backfill any that don't have UUIDs
  • Actually patch entities.create

entities.create is pretty simple; the easiest way to monkey-patch would be to simply wrap the original function:

var orig = this.__coquette__.entities.create;
this.__coquette__.entities.create = function(Constructor, settings) {
  var entity = orig.apply(this, Constructor, settings);
  entity._uuid = generateUUID();
  return entity;
};

Backfilling's also easy, e.g. this.__coquette__.entities.all().map((entity) => { entity._uuid = generateUUID() })

For looking up an entity by UUID, a filter should be good enough to start. Could cache a map of {uuid: entityObject}, but would have to make sure that the map entry is removed if the entity is deleted, which would require a monkey-patch around entities.remove