Skip to content

Latest commit

History

69 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

redux-lua

Build Status

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

Install

redux-lua can be installed using LuaRocks:

$ luarocks install redux-lua

Usage

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.

Define actions

--[[ 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

Define reducer

--[[ 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

Combine reducers

--[[ reducers/index.lua--]]localcombineReducers=require'redux.combineReducers'localprofile=require'reducers.profile'returncombineReducers({
profile=profile
})

Create store

--[[ store.lua--]]localcreateStore=require'redux.createStore'localreducers=require'reducers.index'localstore=createStore(reducers)
returnstore

Create store with middlewares

Here 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())
returnretendendendreturnlogger

Compose all defined middlewares to middlewares array.

--[[ middlewares/index.lua--]]locallogger=require'middlewares.logger'localthunk=require'middlewares.thunk'localmiddlewares= {
thunk,
logger,
}
returnmiddlewares

Finally, 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

Dispatch & Subscription

--[[ 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()

Debug mode

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)

Null vs. nil

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'

License

MIT License

About

Implement redux using Lua language.

Topics

Resources

Stars

33 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages