redux

package
v0.7.10 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package redux provides a simple implementation of a Redux-like state management library in Go.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Reduce

func Reduce[S State](curr S, reducers []Reducer[S], actions ...Action) iter.Seq[State]

Reduce is the type of the reducer of the store.

Types

type Action

type Action func() Update

Action is the type of the action of the store.

type QuitUpdate added in v0.7.7

type QuitUpdate struct{}

QuitUpdate is the type of the quit update of the store.

type Reducer

type Reducer[S State] func(curr S, update Update) S

Reducer is the type of the reducer of the store.

type State

type State interface{}

State is the type of the state of the store.

type StateChange added in v0.7.5

type StateChange[S State] interface {
	// Prev gets the previous state of the store.
	Prev() S
	// Curr gets the current state of the store.
	Curr() S
}

StateChange is the type of the state change of the store.

func NewStateChange added in v0.7.5

func NewStateChange[S State](prev, curr S) StateChange[S]

NewStateChange creates a new state change.

type Store

type Store[S State] interface {
	// Dispatch dispatches an event to the store.
	Dispatch(actions ...Action)
	// State gets the current state of the store.
	State(s ...S) S
	// Dispose disposes the store.
	Dispose()

	Subscription[S]
}

Store is the type of the store.

func New

func New[S State](ctx context.Context, initialState S, reducers ...Reducer[S]) Store[S]

New creates a new store.

Example
package main

import (
	"context"
	"fmt"

	"github.com/katallaxie/pkg/redux"
)

func main() {
	type noopState struct {
		Name string
	}

	r := func(_ noopState, _ redux.Update) noopState {
		return noopState{Name: "bar"}
	}

	a := func() redux.Update {
		return "foo"
	}

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	s := redux.New(ctx, noopState{Name: "foo"}, r)
	defer s.Dispose()

	sub := s.Subscribe()
	s.Dispatch(a)

	out := <-sub
	fmt.Println(out.Curr(), out.Prev())
}
Output:

{bar} {foo}

type Subscription

type Subscription[S State] interface {
	// Subscribe subscribes to the store.
	Subscribe() <-chan StateChange[S]
	// Cancel cancels the subscription.
	CancelSubscription(<-chan StateChange[S])
}

Subscription is the type of the subscription of the store.

type Update added in v0.7.7

type Update interface{}

Update is the type of the update of the store.

func Quit added in v0.7.7

func Quit() Update

QuitAction is the type of the quit action of the store.

Jump to

Keyboard shortcuts

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