fix: reverse order of notification in PojoEntityManager
@@ -552,6 +552,16 @@ protected void notifyComponentRemovalAndEntityDestruction(long entityId, EntityR
Preconditions.checkNotNull(component);
Optional<Component> oldComponent = getPool(entityId).map(pool -> pool.getComponentStore().put(entityId, component));
+ // notify internal users first to get the unobstructed views on the entity as it is at this moment.
+ if (!oldComponent.isPresent()) {
+ notifyComponentAdded(getEntity(entityId), component.getClass());
+ } else {
+ logger.error("Adding a component ({}) over an existing component for entity {}", component.getClass(), entityId);
+ notifyComponentChanged(getEntity(entityId), component.getClass());
+ }
+
+ // Send life cycle events for arbitrary systems to react on.
+ // Note: systems are free to remove the component that was just added, which might cause some trouble here...
if (eventSystem != null) {
EntityRef entityRef = getEntity(entityId);
if (!oldComponent.isPresent()) {
@@ -562,13 +572,6 @@ protected void notifyComponentRemovalAndEntityDestruction(long entityId, EntityR
}
}
- if (!oldComponent.isPresent()) {
- notifyComponentAdded(getEntity(entityId), component.getClass());
- } else {
- logger.error("Adding a component ({}) over an existing component for entity {}", component.getClass(), entityId);
- notifyComponentChanged(getEntity(entityId), component.getClass());
- }
-
return component;
}
