gredux

package module
v0.0.0-...-77dfb72 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2017 License: MIT Imports: 1 Imported by: 0

README

gredux

Go Report Card Build Status codecov GoDoc

gredux is a golang implementation of a redux-esque state container. The aim is to provide a structure for writing applications which have consistent, predictable behaviour.

Example Usage


import (
	"github.com/avahowell/gredux"
)

// Create an initial state for the Store
type counterState struct {
	count int
}

// Instantiate a new store around this state
store := gredux.New(counterState{0})

// Create a reducer which increments "count" when it receives an "increment" 
// action, and decrements when it receives a "decrement" action.
store.Reducer(func(state gredux.State, action gredux.Action) gredux.State {
	switch action.ID {
	case "increment":
		return counterState{state.(counterState).count + action.Data.(int)}
	case "decrement":
		return counterState{state.(counterState).count - action.Data.(int)}
	default:
		return state
	}
})

store.Dispatch(Action{"increment", 5})
store.Dispatch(Action{"decrement", 2})

fmt.Println(store.State().(counterState).count) // prints 3

// Register a func to be called after each state update
store.AfterUpdate(func(state State) {
	fmt.Println(state.(counterState).count) // prints the count after every state update
})
store.Dispatch(Action{"decrement", 2})

License

The MIT License (MIT)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action struct {
	ID   string
	Data interface{}
}

Action defines a dispatchable data type that triggers updates in the Store.

type Reducer

type Reducer func(State, Action) State

Reducer is the func which receives actions dispatched using Store.Dispatch() and updates the internal state.

type State

type State interface{}

State is the state of the gredux Store.

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store defines an immutable store of state. The current state of the Store can be received by calling State() but the state can only be changed by a Reducer as the result of a Dispatch'd Action.

func New

func New(initialState State) *Store

New instantiates a new gredux Store. initialState should be the struct used to define the Store's state.

func (*Store) AfterUpdate

func (st *Store) AfterUpdate(update func(State))

AfterUpdate sets Store's update func. `update` is called after each dispatch with a copy of the new state.

func (*Store) Dispatch

func (st *Store) Dispatch(action Action)

Dispatch dispatches an Action into the Store.

func (*Store) Reducer

func (st *Store) Reducer(r Reducer)

Reducer sets the store's reducer function to the function `r`.

func (*Store) State

func (st *Store) State() State

State returns a copy of the current state.

Jump to

Keyboard shortcuts

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