Categorygithub.com/janmbaco/redux
repositorypackage
0.7.1
Repository: https://github.com/janmbaco/redux.git
Documentation: pkg.go.dev

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# README

redux

version badges Build Status Go Report Card Coverage Status GoDoc GitHub license
This is a redux implementation in Go.
I hope this project can help you manage the complex update flow in Go.
Origin version

Install

$ go get https://github.com/dannypsnl/redux/...

Usage

pkgs

redux/store

  • New Create New Store by reducers(at least one reducer)
  • Dispatch recieve then send action to every reducers to update state
    And you should not call Dispatch in Subscribetor, you will get dead lock.
  • Subscribe recieve a func without args will be invoked by every next Dispatch
    And you should not call Subscribe in Subscribetor, you will get a panic warning.
  • Marshal return state as JSON format string

redux/action

By this module, we can have a better

  • New recieve a string and return a pointer to Action for you
  • Action is a type contain Type & Args
    Type is just a string help reducer juage what should them do.
    Args is a map[string]interface{} contain a lot values, think about we Dispatch login Action
    We need user & password to do this State update, so we will put user & password's value in the Action::Args
    Again, only reducer should use Args, so cast is safety.
  • Arg help you append Argument onto Action with fluent API.

Example

Examples

Basic Example

Ignore other packages

import(
    "github.com/dannypsnl/redux/store"
    "github.com/dannypsnl/redux/action"
)

func counter(state interface{}, action action.Action) interface{} {
    // Initial State
    if state == nil {
        return 0
    }
    switch action.Type {
    case "INC":
        return state.(int)+1
    case "DEC":
        return state.(int)-1
    default:
        return state
    }
}

func main() {
    store := store.New(counter) // counter state be initial at here, it's 0
    // Subscribe's function will be invoke when Dispatch
    store.Subscribe(func() {
        fmt.Printf("Now state is %v\n", store.GetState("counter"))
        fmt.Printf("%s\n", store.Marshal()) // `{ counter: 1 }`, 1 should be current state, Let's print out the json format of our store
    })
    store.Dispatch(action.New("INC")) // state increase by action, now is 1
}