const classafIoc::ConcurrentState
sys::Obj afIoc::ConcurrentState
A helper class used to store, access and retrieve mutable state within a const
class. For IoC this means your services can be declared as perApplication
or singleton scope.
ConcurrentState
wraps a state object in an Actor, and provides access to it via the withState
and getState
methods. Note that by their nature, these methods are immutable boundaries. Meaning that while all data in the State object can be mutable, but data passed in and out of the methods can not be.
A full example of a mutable const map class is as follows:
const class ConstMap { const ConcurrentState conState := ConcurrentState(ConstMapState#) Str get(Str key) { getState { it.map[key] } } Void put(Str key, Str value) { withState { it.map[key] = value } } ** helper method used to narrow the state type private Void withState(|ConstMapState| state) { conState.withState(state) } ** helper method used to narrow the state type private Obj? getState(|ConstMapState -> Obj| state) { conState.getState(state) } } class ConstMapState { Str:Str map := [:] }
- getState
virtual Obj? getState(|Obj->Obj? f)
Use to return state
- makeWithStateFactory
new makeWithStateFactory(|->Obj stateFactory)
- makeWithStateType
new makeWithStateType(Type stateType)
The given state type must have a public no-args ctor as per sys::Type.make
- withState
virtual Void withState(|Obj f, Bool waitForErr := true)
Use to access state