thomasboyt / coquette-inspect

A Chrome DevTools extension for inspecting games made with the Coquette framework
29 stars 14 forks source link

Attach a unique ID to each entity #1

Closed thomasboyt closed 9 years ago

thomasboyt commented 9 years ago

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:

There's a couple options to implement UUIDs -

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