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.

ConcurrentState has been designed to be type safe, that is you cannot accidently call methods on your State object. The compiler forces all access to the state object to be made through the withState and getState methods.

A full example of a mutable const map class is as follows:

const class ConstMap {
  const ConcurrentState  conState  := ConcurrentState(ConstMapState#)
  
  ** Note that both 'key' and 'value' need to be immutable
  @Operator
  Obj get(Obj key) {
    getState {
      it.map[key]
    }
  }

  ** Note that both 'key' and 'value' need to be immutable
  @Operator
  Void set(Obj key, Obj 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 {
  Obj:Obj  map := [:]
}

As alternatives to ConcurrentState don't forget you also have AtomicBool, AtomicInt and AtomicRef

getState

Source

virtual Obj? getState(|Obj->Obj? f)

Use to return state

makeWithStateFactory

Source

new makeWithStateFactory(|->Obj stateFactory)

makeWithStateType

Source

new makeWithStateType(Type stateType)

The given state type must have a public no-args ctor as per sys::Type.make

withState

Source

virtual Future withState(|Obj->Obj? f)

Use to access state. Call get() on the returned Future to ensure any Errs are rethrown.