A-Frame Component Update Lifecycle: Execution Order for Simultaneous Property Changes
0 reputation · 07 Jan 2023, 14:03 UTC
0 reputation · 07 Jan 2023, 14:03 UTC
In A-Frame 1.x, the Entity-Component-System (ECS) architecture separates data from logic, utilizing the update handler to synchronize component state with the underlying Three.js scene graph.
When multiple components attached to a single entity undergo property changes within the same execution cycle, the framework triggers their respective update methods. However, there is no built-in transaction mechanism to coordinate these mutations or ensure a deterministic sequence of execution across different components.
This creates uncertainty when one component's update logic depends on the state of another component on the same entity, potentially leading to inconsistent frames or redundant calculations.
update handlers when multiple components on one entity are modified simultaneously?32775 reputation · 07 Jan 2023, 21:10 UTC
update() on a single entityWhen setAttribute is called on an entity that has several components, A‑Frame processes the changes component‑by‑component. For each component that receives a change, the framework does the following, in order:
setAttribute call. The arguments contain the new values and the previous values.update().The framework resolves the order of update() calls using one of two deterministic rules:
AFRAME.registerComponent.
setAttribute calls are made in the same JavaScript tick, the order of those calls overrides registration order.
A‑Frame does not expose a transaction system that guarantees atomic updates across components. The only way to influence order is via the rules above. If your component’s logic depends on another component’s state, you should either:
update() – the data will already be the latest value if the dependent component was updated earlier in the same cycle.tick() or play() to defer logic until the next frame, when all update() calls have finished.foo and bar, and log their init, update, and tick calls.
foo before bar.
setAttribute('foo', {a:1}) and setAttribute('bar', {b:2}).
foo.update runs before bar.update.
update calls flips accordingly.
These steps confirm the deterministic ordering and show that no duplicate updates occur when multiple properties change simultaneously.
Use comments to ask for clarification. Post a solution as an answer.
32,775 reputation · 07 Jan 2023, 16:29 UTC
When you change several attributes of the same A‑Frame component in a single JavaScript tick, the framework does not invoke update() multiple times. Instead it collects all modified properties, builds a single diff object, and calls update(data, oldData) once. The data argument contains only the keys that actually changed, and those keys are sorted alphabetically regardless of the order in which you called setAttribute or set them in HTML. This means you can safely read the new values for all changed properties in one go, but you cannot rely on the sequence of keys to infer which attribute was set first.