Skip to content

๐Ÿ—๏ธ Flux Implementation done in Multi Theft Auto

Notifications You must be signed in to change notification settings

molnarmark/multex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

9 Commits
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Multex

Open Source Love Open Source Love PRs Welcome

Introduction

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.

Getting Started

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})

Events

You can also subscribe to various events that happen on the store main ones being:

  • update
  • computed_update

Computed Properties

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)

Hooks

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)

Practical Example

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

Fancy buying me a beer?

If this project was helpful to you, you can buy me a beer if you feel like doing so. ๐Ÿ™‚

paypal

Releases

No releases published

Packages

No packages published

Languages