godux

package module
v0.0.0-...-70bcb3c Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2020 License: MIT Imports: 1 Imported by: 0

README

godux

Go Report Card

State Management for Go Backend applications inspired by Redux.

Godux

╔═════════╗       ╔══════════╗       ╔═══════════╗       ╔═════════════════╗
║ Action  ║──────>║ Reducer  ║ ────> ║   Store   ║ ────> ║   Application   ║
╚═════════╝       ╚══════════╝       ╚═══════════╝       ╚═════════════════╝
     ^                                                            │
     └────────────────────────────────────────────────────────────┘

Install
  • Go: go get github.com/luisvinicius167/godux
Data Flow

godux gives go unidirectional data flow:

  • The Action returns a small map with specific directions that are dispatched to a Reducer.
  • The Reducer is a pure function (pure functions don't change original arguments) if relevant to it returns a new Value.
  • The Value becomes the new State of the Store.
Principles:
  • Global application state is held in the Store, as a single map.
  • State is ready-only (only change it only by replacing it with the Reducer).
  • Changes are made with pure functions - Actions/Reducers that do not change the actual object but make a changed copy.
Store:

A Store is basically a container that holds your application state.

    store := godux.NewStore()
	store.Setstate("count", 1)
    store.Setstate("Title", "I like godux!")
Action

Actions are just pure functions which pass on their inputs when they're dispatched. Actions are stored on the godux map as godux.Action.

    increment := func(number int) godux.Action {
		return godux.Action{
			Type:  "INCREMENT",
			Value: number,
		}
	}
Reducers

As in Redux:

"Actions describe the fact that something happened, but don’t specify how the application’s state changes in response. This is the job of a reducer".

Reducers are pure functions that take in actions and the state of the store as inputs and leave them all as they came in (aka. pure)-- especially the original state of the store must not be modified (it's accessed by store.GetState)).

    // reducer function
	reducer := func(action godux.Action) interface{} {
		switch action.Type {
		case "INCREMENT":
			return store.GetState("count").(int) + action.Value.(int)
		case "DECREMENT":
			return action.Value.(int) - store.GetState("count").(int)
		default:
			return store.GetAllState()
		}
	}
	// Add your reducer function to return new values basend on your state
	store.Reducer(reducer)
Dispatch

Dispatching an action is very easy.

    // Receive new value
	newCount := store.Dispatch(increment(1)) // return 2
API Reference
  • Store:
    • godux.newStore(): Create a single store with the state of your application (should only be used once).
    • godux.SetState(name string, value interface{}): Sets the state of the store.
    • godux.GetState(name string): Return a state's value.
    • godux.GetAllState(): Return the whole state as a map.
  • Reducer:
    • store.Reducer(func(action godux.Action)): Adding a reducer function to your Store.
  • Dispatch:
    • store.Dispatch(action godux.Action): Dispatching an action to your Reducer.
  • Action:
    • godux.Action( Type string, Value interface{}): Adding an easily available Action.
License

MIT License.

Documentation

Overview

Package godux implements a state management for your backend application. It's inspired in Redux, but with simplest concepts. - State: Your application state don't change. - Actions: Your action is used in reducers, to return new value based on State. - Reducers: Actions describe the fact that something happened, but don’t specify how the application’s state changes in response. This is the job of a reducer.

This library was inspired in Redux.

Example
package main

import (
	"fmt"

	"github.com/luisvinicius167/godux"
)

func main() {
	// Creating new Store
	store := godux.NewStore()
	// Set state
	store.SetState("count", 1)

	// Creating new Action

	increment := func(number int) godux.Action {
		return godux.Action{
			Type:  "INCREMENT",
			Value: number,
		}
	}

	decrement := func(number int) godux.Action {
		return godux.Action{
			Type:  "DECREMENT",
			Value: number,
		}
	}
	// reducer function
	reducer := func(action godux.Action) interface{} {
		switch action.Type {
		case "INCREMENT":
			return store.GetState("count").(int) + action.Value.(int)
		case "DECREMENT":
			return store.GetState("count").(int) - action.Value.(int)
		default:
			return store.GetAllState()
		}
	}
	// Add your reducer function to return new values based on your state
	store.Reducer(reducer)
	// Receive new value
	newCount := store.Dispatch(increment(10)) // 1+10=11
	subCount := store.Dispatch(decrement(10)) // 1-10=-9
	fmt.Printf("Your Store state is: %d. Your newCount is: %d. Your subCount is: %d\n", store.GetState("count"), newCount, subCount)
}
Output:

Your Store state is: 1. Your newCount is: 11. Your subCount is: -9

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action struct {
	Type  string
	Value interface{}
}

Action that you create to change the State

type Store

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

Store Your central store that has your application state

func NewStore

func NewStore() *Store

NewStore to create your Store Application

func (*Store) Dispatch

func (s *Store) Dispatch(actionType Action) interface{}

Dispatch trigger your action type

func (*Store) GetAllState

func (s *Store) GetAllState() interface{}

GetAllState return a full copy of the current state.

func (*Store) GetState

func (s *Store) GetState(name string) interface{}

GetState return the state of your store

func (*Store) Reducer

func (s *Store) Reducer(callback func(Action) interface{})

Reducer is a function that you use to return new value based on your storeState. Your state don't will be changed.

func (*Store) SetState

func (s *Store) SetState(name string, value interface{})

SetState is to sets the state store

Jump to

Keyboard shortcuts

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