Originally, redux is a predictable state container for JavaScript apps. From now on, all the redux features are available on your Lua projects. Try it out! :-D
redux-lua can be installed using LuaRocks:
$ luarocks install redux-lua
Here is an example of profile updating. To handle redux state changes, it is recommended to use redux-props. To get more usages, please checkout examples.
--[[ actions/profile.lua--]]localactions= {}
functionactions.updateName(name)
return {
type="PROFILE_UPDATE_NAME",
name=name
}
endfunctionactions.updateAge(age)
return {
type="PROFILE_UPDATE_AGE",
age=age
}
endfunctionactions.remove()
return {
type="PROFILE_REMOVE"
}
endfunctionactions.thunkCall()
returnfunction (dispatch, state)
returndispatch(actions.remove())
endendreturnactions--[[ reducers/profile.lua--]]localassign=require'redux.helpers.assign'localNull=require'redux.null'localinitState= {
name='',
age=0
}
localhandlers= {
["PROFILE_UPDATE_NAME"] =function (state, action)
returnassign(initState, state, {
name=action.name
})
end,
["PROFILE_UPDATE_AGE"] =function (state, action)
returnassign(initState, state, {
age=action.age
})
end,
["PROFILE_REMOVE"] =function (state, action)
returnNullend,
}
returnfunction (state, action)
state=stateorNulllocalhandler=handlers[action.type]
ifhandlerthenreturnhandler(state, action)
endreturnstateend--[[ reducers/index.lua--]]localcombineReducers=require'redux.combineReducers'localprofile=require'reducers.profile'returncombineReducers({
profile=profile
})--[[ store.lua--]]localcreateStore=require'redux.createStore'localreducers=require'reducers.index'localstore=createStore(reducers)
returnstoreHere is an example about how to define a middleware.
--[[ middlewares/logger.lua--]]localLogger=require'redux.utils.logger'localfunctionlogger(store)
returnfunction (nextDispatch)
returnfunction (action)
Logger.info('WILL DISPATCH:', action)
localret=nextDispatch(action)
Logger.info('STATE AFTER DISPATCH:', store.getState())
returnretendendendreturnloggerCompose all defined middlewares to middlewares array.
--[[ middlewares/index.lua--]]locallogger=require'middlewares.logger'localthunk=require'middlewares.thunk'localmiddlewares= {
thunk,
logger,
}
returnmiddlewaresFinally, pass middlewares to applyMiddleware, which is provided as an enhancer to createStore, and create our store instance.
--[[ store.lua--]]localcreateStore=require'redux.createStore'localreducers=require'reducers.index'localapplyMiddleware=require'redux.applyMiddleware'localmiddlewares=require'middlewares.index'localstore=createStore(reducers, applyMiddleware(table.unpack(middlewares)))
returnstore--[[ main.lua--]]localProfileActions=require'actions.profile'localinspect=require'redux.helpers.inspect'localstore=require'store'localfunctioncallback()
print(inspect(store.getState()))
end-- subscribe dispatchinglocalunsubscribe=store.subscribe(callback)
-- dispatch actionsstore.dispatch(ProfileActions.updateName('Jack'))
store.dispatch(ProfileActions.updateAge(10))
store.dispatch(ProfileActions.thunkCall())
-- unsubscribeunsubscribe()redux-lua is on Debug mode by default. Messages with errors and warnings will be output when Debug mode is on. Use following code to turn it off.
localEnv=require'redux.env'Env.setDebug(false)nil is not allowed as a reducer result. If you want any reducer to hold no value, you can return Null instead of nil.
localNull=require'redux.null'