
When a KOS Model is instantiated it will progress through a defined lifecycle with the KOS framework calling into one or more optional lifecycle hooks that are exposed via the IKosModelLifecycle
interface which is inherited by IKosDataModel
.
Called immediately after the model’s constructor has been called and provides an opportunity to further initialize the model. The init
lifecycle hook is asynchronous which allows for initialization that is not typically available in the constructor.
Any registered dependent models are guaranteed to be initialized before the init lifecycle hook is called.
Called after the model (and its dependent models) have been initialized. The load
lifecycle hook is typically used to hydrate the model and load its initial state with calls to the backend.
Called after the model (and its dependents) have been loaded and the framework core has come online. The ready
lifecycle hooks are useful if there are activities that need to be performed after the model has been hydrated which should delay the subsequent loading of any models that might be dependent on the current model.
After this lifecycle hook completes, the model is considered ready to be used by other models or UI components and can safely start listening to event bus or WebSocket messages from the backend.
Once a model passes the ready
state, if the KOS Core transport comes online, the online
hook will be called on all KOS models. This provides a mechanism for models to optionally resynchronize with the backend/dispenser and resubscribe to any topics that may have been destroyed in cases where the WebSocket transport is interrupted.
Once a model passes the ready
state, if the KOS Core transport goes offline for any reason, the offline
hook will be called on all KOS models. When called the model will have the opportunity to remove any manually create listeners or subscriptions that need to be re-established when the core comes back online.
Called just before the model is removed from memory. The unload
is used to clean up any unused data structures or listeners before a model is destroyed.
The activate
lifecycle hook is intended to be used to enable deferred loading rather than relying on the load
hook such as when a component that consumes a model is mounted into the UI. This hook is particularly useful for models that are expected to defer data loading and hydration until the model is being used in the UI.
If a model is mounted into a component via the useKosModel
React hook the activate
and corresponding deactivate
hook will be called as the component is mounted and unmounted from the DOM.
Called whenever a UI component that consumes a model is unmounted from the DOM. The deactivate
lifecycle hook provides a mechanism to manually clean up a model, such as removing any additional listeners or freeing up high-memory data structures, when a component is unmounted and the model is not needed elsewhere.
This is similar in purpose to the unload hook with the primary difference being the frequency that the hook is called and is more tied to the UI lifecycle rather than the overall model lifecycle in the framework.