State and actions

Share values between components with named states, and send named events with actions.

This page covers the two ways components in the app package talk to each other without holding references: states, which are named values any component can read and observe, and actions, which are named events carrying a value. This is go-app's state and action system, now part of WebGo's app package.

WebGo builds on both. app.Model , on Pages, layouts and state , is a typed wrapper over one state, and dynamic styles, on Applying , react to actions. The UI goroutine, Dispatch and Async are on Concurrency and the browser .

ยง1 State

                  
                    func
                     
                    (
                    ctx
                     
                    Context
                    )
                     
                    SetState
                    (
                    state
                     
                    string
                    ,
                     
                    v
                     
                    any
                    )
                     
                    State
                    

                    func
                     
                    (
                    ctx
                     
                    Context
                    )
                     
                    GetState
                    (
                    state
                     
                    string
                    ,
                     
                    recv
                     
                    any
                    )
                    

                    func
                     
                    (
                    ctx
                     
                    Context
                    )
                     
                    DelState
                    (
                    state
                     
                    string
                    )
                  
                

A state is a value stored under a string key. There is one store per running app, and any component can read or write any key. The store is safe for concurrent use, so these methods can be called from any goroutine.

SetState

  • SetState stores v under the key, replacing the previous value and its expiry.
  • For every component observing the key, it queues a delivery on the UI goroutine: the value is copied into the observer's receiver, its OnChange runs, and the component re-renders. Observers do not see the value during the SetState call, only when the UI goroutine gets to it.
  • It also posts an action named after the key, with v as its value, so an action handler or a dynamic style can follow the state.
  • It returns a State , whose methods persist, expire or broadcast the value just set. They are under Persisting and broadcasting .
  • A pointer is stored as the pointer. Readers get a copy of what it points to at the time they read, so changing the pointed-to value after SetState changes what later readers get without notifying observers. Call SetState again after a change.

GetState

  • GetState copies the current value into recv , which must be a pointer. If the stored value is a pointer, the value it points to is copied.
  • The type recv points to must be exactly the stored type. A state set with SetState("n", 1) can be read into an *int but not an *int64 . On a mismatch, or when recv is not a pointer, GetState logs the error through app.Log and leaves recv unchanged.
  • If the key was not set since the app started, GetState looks for a persisted copy in local storage and decodes it into recv . If there is none, recv is left as it was, so give it its default before the call.
  • If the state has expired, it is deleted from memory and local storage, and recv is left as it was.
  • A state stored as nil sets *recv to its zero value.

DelState

  • DelState removes the value from memory and from local storage.
  • It does not notify observers and does not remove them. They keep their last value until the next SetState on the key. To reset what observers show, set a zero value instead.
                  
                    type
                     
                    greeting
                     
                    struct
                     
                    {
                    
	
                    app
                    .
                    Compo
                    
	
                    name
                     
                    string
                    

                    }
                    


                    func
                     
                    (
                    g
                     
                    *
                    greeting
                    )
                     
                    OnMount
                    (
                    ctx
                     
                    app
                    .
                    Context
                    )
                     
                    {
                    
	
                    g
                    .
                    name
                     
                    =
                     
                    "guest"
                    
	
                    ctx
                    .
                    GetState
                    (
                    "user-name"
                    ,
                     
                    &
                    g
                    .
                    name
                    )
                    

                    }
                    


                    func
                     
                    (
                    g
                     
                    *
                    greeting
                    )
                     
                    Render
                    (
                    )
                     
                    app
                    .
                    UI
                     
                    {
                    
	
                    return
                     
                    app
                    .
                    Div
                    (
                    )
                    .
                    Body
                    (
                    
		
                    app
                    .
                    P
                    (
                    )
                    .
                    Text
                    (
                    "Hello, "
                     
                    +
                     
                    g
                    .
                    name
                    )
                    ,
                    
		
                    app
                    .
                    Input
                    (
                    )
                    .
                    Value
                    (
                    g
                    .
                    name
                    )
                    .
                    OnChange
                    (
                    g
                    .
                    rename
                    )
                    ,
                    
	
                    )
                    

                    }
                    


                    func
                     
                    (
                    g
                     
                    *
                    greeting
                    )
                     
                    rename
                    (
                    ctx
                     
                    app
                    .
                    Context
                    ,
                     
                    e
                     
                    app
                    .
                    Event
                    )
                     
                    {
                    
	
                    ctx
                    .
                    SetState
                    (
                    "user-name"
                    ,
                     
                    ctx
                    .
                    JSSrc
                    (
                    )
                    .
                    Get
                    (
                    "value"
                    )
                    .
                    String
                    (
                    )
                    )
                    

                    }
                  
                

This component reads the name once, when it mounts, so it does not see later changes made by other components. Observing state covers following a key.

01