Lifecycle Handlers
Evolve exposes a number of lifecycle hooks that you can implement to customize the behavior of elements.
The syntax is when:hook_name => expression. when can be either before or `after. Hook names and descriptions are found below:
| Event Name | Description |
|---|---|
create | Called when the element is first created. At this point no parameters have been passed but state has been initialized |
enable | Called when the element was either first created or was re-enabled in a frame |
update | Called every frame, parameters have been set for this frame |
finish | Called every frame after all descendents have been updated |
disable | Called asynchronously when an element was disabled. This happens on the main thread at the start of the next frame |
destroy | Called asynchronously when an element was destroyed. This happens on the main thread at the start of the next frame |
Lifecycle can be layered like an onion if an element is decorated. The element itself will fire it's handlers 'in the center' and any decorators are called around it.
@TopLevelDecorator
template SomeElement { ... }
@Decorator1
@Decorator2
SomeElement(
before:update => Debug.Log("Before Inline"),
after:update => Debug.Log("After Inline")
);
// assuming we log each before/after update the output would be
Decorator1.BeforeUpdate
Decorator2.BeforeUpdate
Before Inline
TopLevelDecorator.BeforeUpdate
SomeElement.BeforeUpdate
// onion center, no actual method is called here which means
// that for element declarations, before:update and after:update are basically the same
SomeElement.AfterUpdate
TopLevelDecorator.AfterUpdate
After Inline
Decorator2.AfterUpdate
Decorator1.AfterUpdate
An event or life cycle can also be mapped from the companion or from top level state. As long as the signatures match
between the event you are mapping and the method on the companion or state instance, the mapping is valid.
There is also a shorthand for mapping life cycle handlers from a $companion. For lifecycle events
the return type is ignored and can be anything as long as your mapping method defines no parameters.
The name mapping table for lifecycle events is below:
| Event Name | Method Mapping Name |
|---|---|
before:create | OnBeforeCreate |
after:create | OnAfterCreate |
before:enable | OnBeforeEnable |
after:enable | OnAfterEnable |
before:update | OnBeforeUpdate |
after:update | OnAfterUpdate |
before:finish | OnBeforeFinish |
after:finish | OnAfterFinish |
before:disable | OnBeforeDisable |
after:disable | OnAfterDisable |
before:destroy | OnBeforeDestroy |
after:destroy | OnAfterDestroy |