redux

package module
v2.2.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 26, 2021 License: MIT Imports: 0 Imported by: 0

README

redux

version badges Build Status codecov go.dev reference GitHub license

This is a redux implementation in Go.

Origin version

Install

$ go get github.com/dannypsnl/redux

Usage

import (
    // must import, else go module would think following package is a module
    _ "github.com/dannypsnl/redux/v2"
    // v2 store
    "github.com/dannypsnl/redux/v2/store"
    // v2 rematch(optional)
    "github.com/dannypsnl/redux/v2/rematch"
)

Basic example

// As you can see, you don't need to checking nil or not now
// Now redux-go will initial the state with zero value of that type
func counter(state int, action string) int {
    switch action {
    case "INC":
        return state + 1
    case "DEC":
        return state - 1
    default:
        return state
    }
}

func main() {
    // redux/v2/store
    store := store.New(counter)
    store.Dispatch("INC")
    fmt.Printf("%d\n", store.StateOf(counter)) // should print out: 1
}

More stunning(lambda can work)

func main() {
    counter := func(state, payload int) int {
        return state + payload
    }
    store := store.New(counter)
    store.Dispatch(100)
    store.Dispatch(-50)
    fmt.Printf("%d\n", store.StateOf(counter)) // should print out: 50
}
Rematch

And more...

type CountingModel struct {
    rematch.Reducer
    State int
}

func (cm *CountingModel) Increase(s, payload int) int {
    return s + payload
}

func (cm *CountingModel) Decrease(s, payload int) int {
    return s - payload
}

func main() {
    c := &CountingModel{
        State: 0,
    }
    store := store.New(c)
    store.Dispatch(c.Action(c.Increase).With(100))
    store.Dispatch(c.Action(c.Decrease).With(50))

    fmt.Printf("result: %d\n", store.StateOf(c)) // expect: 50
}

Then let's have a party

type CountingModel struct {
    rematch.Reducer
    State int

    Increase *rematch.Action `action:"IncreaseImpl"`
}

func (c *CountingModel) IncreaseImpl(s, payload int) int {
    return s + payload
}

func main() {
    c := &CountingModel {
        State: 0,
    }
    store := store.New(c)
    store.Dispatch(c.Increase.With(30))
    store.Dispatch(c.Increase.With(20))

    fmt.Printf("result: %d\n", store.StateOf(c)) // expect: 50
}

Documentation

Overview

Package redux provides primitives for manage data by single way flow.

Directories

Path Synopsis
example should be empty, it's purpose is let example of doc can appear on godoc page
example should be empty, it's purpose is let example of doc can appear on godoc page
rematch provides structure helps user manage their reducers more under controlled.
rematch provides structure helps user manage their reducers more under controlled.
Package store is the dirty part of whole Redux system.
Package store is the dirty part of whole Redux system.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL