redux

package module
v0.0.0-...-fb98daf Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2017 License: MIT Imports: 1 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Go Redux

GoDocBuild Status

Redux implementation in GoLang.

Motivation

Follow Redux pattern for sane state management.

Example

package main

import (
	"fmt"

	"github.com/rcliao/redux"
)

func main() {
	initialState := map[string]interface{}{
		"counter": 0,
	}
	reducers := []redux.Reducer{
		func(state redux.State, action redux.Action) redux.State {
			switch action.Type {
			case "INCREMENT":
				state["counter"] = state["counter"].(int) + 1
				return state
			case "DECREMENT":
				state["counter"] = state["counter"].(int) - 1
				return state
			default:
				return state
			}
		},
	}
	store := redux.NewStore(initialState, reducers)
	store.Subscribe(func(state redux.State) {
		fmt.Println("pubsub:", state)
	})
	store.Dispatch(redux.Action{Type: "INCREMENT"}) // 1
	store.Dispatch(redux.Action{Type: "INCREMENT"}) // 2
}

Documentation

Index

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 is sent to dispatcher to create new state

type Listener

type Listener func(State)

Listener function defines subscriber to store

type Reducer

type Reducer func(State, Action) State

Reducer takes an action and return updated state

type State

type State map[string]interface{}

State is simply a map of name to anything

type Store

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

Store is the central glue between everything – takes actions dispatch such action to all reducers and modify state accordingly

func NewStore

func NewStore(initialState State, reducers []Reducer) *Store

NewStore is a constructor pattern for creating a new store

func (*Store) Dispatch

func (s *Store) Dispatch(action Action) State

Dispatch sends action to store and returns updated state

func (*Store) GetState

func (s *Store) GetState() State

GetState returns the copy of the current state

func (*Store) SetReducers

func (s *Store) SetReducers(reducers []Reducer)

SetReducers updates the internal reducers

func (*Store) Subscribe

func (s *Store) Subscribe(listener Listener)

Subscribe allows consumer to listen for state changes

Directories

Path Synopsis
cmd
example command

Jump to

Keyboard shortcuts

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