Multex is an implementation of the Flux
Architecture by Facebook for the Multi Theft Auto modification.
You can learn more about how Flux
works here.
Create a store by calling the exported createStore function:
local store = loadstring(createStore())()
You can set the initial state of a store by calling setInitialState
on it:
store:setInitialState({counter = 0})
You can also subscribe to various events that happen on the store main ones being:
- update
- computed_update
Computed properties introduced in Vuex
are also available in Multex.
You can subscribe to a property change like so:
store:setComputedProperty("counter", function()
outputChatBox("Counter has changed. Let's do something.")
end)
Multex also offers you two very simple hooks you can use, those being beforeAction
and afterAction
.
You can use them like so:
store:setHook("beforeAction", function()
outputChatBox("An action is about to take place.")
end)
Multex uses the traditional action handlers with a simple dispatch method.
Here is a little practical example showing you the power of Multex
.
local store = loadstring(createStore())()
store:setInitialState({counter = 0})
-- You can subscribe to the change of a property.
store:setComputedProperty("counter", function()
outputChatBox("Counter has changed.")
end)
-- Let's handle some actions!
store:onAction(function(name, payload)
if name == "increment" then
outputChatBox("Increment action called with payload of " .. payload.value)
local state = store:getState()
store:setState({counter = state.counter + 1})
end
end)
-- The update event is emitted whenever the state changes via setState()
store:on("update", function()
local counter = store:getState().counter
outputChatBox("State updated. Counter is now " .. counter)
end)
-- Let's create some actions for our store
local actions = {
increment = function()
store:dispatch("increment", {value = 1})
end,
}
actions.increment()
actions.increment()
actions.increment()
actions.increment()
actions.increment()
-- counter is now 5
If this project was helpful to you, you can buy me a beer if you feel like doing so. ๐