Skip to content
A Lua Redux implementation
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Docs/Examples/Random
src
.gitignore
COPYRIGHT
README.md
dist.ini

README.md

Lux

Redux on Lua

What it does?

This module implements major redux features on Lua. You can create a store, dispatch actions and manage your state.

Installation and Usage

Installing

You can install this package using the OPM

opm install IgorMael/lux

Creating a store

To create a store you should call create_store function, it takes a reducer (to use multiple reducers you need combine them with combine_reducers), an optional initial_state and an optional store enhancer

create_store = function(reducer, initial_state, enhancer)

An Example

local lux = require 'lux'
local reducer = require 'Reducer'

local store = lux.create_store(reducer)

Combining reducers

In most of the real world applications you will want to use more than one reducer. To do so you need call the combine_reducers function

    combine_reducers = function(reducer_list)

An example

    local lux = require 'lux'

    local reducers = lux.combine_reducers({item = reducers.item_reducer, monster = reducers.monster_reducer})
    local store = lux.create_store(reducers)

Aplying a store enhancer

apply_middleware is an example of store enhancer, it modifies the dispatch function with middlewares. An example of middleware will soon be disponibilized,
```
     apply_middleware = function(...)
```

Dispatching actions

An action must be a lua table with the type field and optional payload

store:dispatch(action)

And an example

local lux = require 'lux'
local store = lux.create_store(reducer)

local create_foo = function(name, bar_level)
    return {
        type = "CREATE_FOO",
        payload = {
            bar_level = bar_level,
            name = name
        }
    }
end

store:dispatch(create_foo("FOOBAR", 12))

Subscribing

You can subscribe to the store

    store:subscribe(callback)

Example:

    local lux = require 'lux'
local store = lux.create_store(reducer)

local create_foo = function(name, bar_level)
    return {
        type = "CREATE_FOO",
        payload = {
            bar_level = bar_level,
            name = name
        }
    }
end

store:subscribe(function() print("What a dispatch! Beautiful!")) -- callback will be called after ever dispatch

License

This project is licensed under the MIT License - see the LICENSE file for details

You can’t perform that action at this time.