Redux on Lua
This module implements major redux features on Lua. You can create a store, dispatch actions and manage your state.
You can install this package using the OPM
opm install IgorMael/lux
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)
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)
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(...)
```
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))
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
This project is licensed under the MIT License - see the LICENSE file for details