2.8 KiB
2.8 KiB
BaseElement
This is the state plugin. It provides a state management system for UI elements with support for persistent states, computed states, and state sharing between elements.
Functions
| Method | Returns | Description |
|---|---|---|
| BaseElement:computed | BaseElement | Creates a computed state |
| BaseElement:getState | any | Gets a state value |
| BaseElement:initializeState | BaseElement | Initializes a new state |
| BaseElement:onStateChange | BaseElement | Watches for state changes |
| BaseElement:setState | BaseElement | Sets a state value |
| BaseElement:shareState | BaseElement | Shares state between elements |
BaseElement:computed(self, key, computeFn)
Creates a computed state that derives its value from other states
Parameters
selfBaseElementThe element to create computed state forkeystringThe name of the computed statecomputeFnfunctionFunction that computes the state value
Returns
BaseElementselfThe element instance
BaseElement:getState(self, name)
Gets the value of a state
Parameters
selfBaseElementThe element to get state fromnamestringThe name of the state
Returns
anyvalueThe current state value
BaseElement:initializeState(self, name, default, canTriggerRender?, persist?, path?)
Initializes a new state for this element
Parameters
selfBaseElementThe element to initialize state fornamestringThe name of the statedefaultanyThe default value of the statecanTriggerRender(optional)booleanWhether state changes trigger a renderpersist(optional)booleanWhether to persist the state to diskpath(optional)stringCustom file path for persistence
Returns
BaseElementselfThe element instance
BaseElement:onStateChange(self, stateName, callback)
Registers a callback for state changes
Parameters
selfBaseElementThe element to watchstateNamestringThe state to watchcallbackfunctionCalled with (element, newValue, oldValue)
Returns
BaseElementselfThe element instance
BaseElement:setState(self, name, value)
Sets the value of a state
Parameters
selfBaseElementThe element to set state fornamestringThe name of the statevalueanyThe new value for the state
Returns
BaseElementselfThe element instance
BaseElement:shareState(self, stateKey...)
Shares a state with other elements, keeping them in sync
Parameters
selfBaseElementThe source elementstateKeystringThe state to share...(vararg)BaseElementThe target elements to share with
Returns
BaseElementselfThe source element