Skip to content

rookiecj/go-store

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-store

go-store is a state holder in Redux pattern. The State is read-only, the Changes are made with reducer in uni-directional way

The Redux pattern

Store holds a state. Action is delivered to the store to change the state. Reducer changes the state with Action. Store delivers the change to Subscriber

How To Use

It can be installed by:

go get github.com/rookiecj/go-store

how to use as follows:

import (
    "fmt"
    "time"

    "github.com/rookiecj/go-store/store"
)

type myState struct {
    id    int
    value string
}

type addAction struct {
    value string
}

func (c myState) StateInterface()     {}

func main() {
    
    initialState := myState{}
    reducer := func(state myState, action store.Action) (myState, error) {
        switch reified := action.(type) {
            case *addAction:
            return myState{
                id:    state.id,
                value: state.value + reified.value,
                }, nil
        }
        return state, nil
    }
    
    stateStore := store.NewStore[myState](initialState, reducer)
    
    stateStore.Subscribe(func(newState myState, oldState myState, action store.Action) {
        fmt.Println("subscriber1", newState)
    })
    
    stateStore.Subscribe(func(newState myState, oldState myState, action store.Action) {
        fmt.Println("subscriber2", newState)
    })
    
    stateStore.Dispatch(&addAction{
        value: "1",
	})
    stateStore.Dispatch(&addAction{
        value: "2",
	})
    stateStore.Dispatch(&addAction{
        value: "3",
	})

    stateStore.Stop()
    stateStore.WaitForStore()
}

TODO

  • make sure all subscribers notified
  • add Store callbacks like onFirstSubscribe
  • add SubscribeOn
  • add README
  • add doc
  • support Main/Background/Dispatch Scheduler(Experimental)
  • remove explicit scheduler start/stop
  • add AsyncAction for async work
  • add AddReducer
  • Disposer for unsubscribe from store
  • make getState public for subscribers not to save the state locally
  • add State history
  • make age precisely
  • add more testing