Silo
A Silo is a state container, inspired by Redux slices and designed for Roblox developers.
Types
State<S>
type
State<S> =
{
[
string
]
:
any
}
Represents state.
Modifier<S>
type
Modifier<S> =
(
State
<
S
>
,
any
)
→
(
)
A function that modifies state.
Action<A>
interface
Action<A> {
Name:
string
Payload:
A
}
Actions are passed to Dispatch
. However, typically actions are
never constructed by hand. Use a silo's Actions table to generate
these actions.
Functions
new
Create a Silo.
local statsSilo = Silo.new({
-- Initial state:
Kills = 0,
Deaths = 0,
Points = 0,
}, {
-- Modifiers are functions that modify the state:
SetKills = function(state, kills)
state.Kills = kills
end,
AddPoints = function(state, points)
state.Points += points
end,
})
-- Use Actions to modify the state:
statsSilo:Dispatch(statsSilo.Actions.SetKills(10))
-- Use GetState to get the current state:
print("Kills", statsSilo:GetState().Kills)
From the above example, note how the modifier functions were transformed
into functions that can be called from Actions
with just the single
payload (no need to pass state). The SetKills
modifier is then used
as the SetKills
action to be dispatched.
combine
Constructs a new silo as a combination of other silos.
GetState
Silo:
GetState
(
) →
State
<
S
>
Get the current state.
local state = silo:GetState()
Dispatch
Silo:
Dispatch
(
action:
Action
<
A
>
) →
(
)
Dispatch an action.
silo:Dispatch(silo.Actions.DoSomething("something"))
Subscribe
Silo:
Subscribe
(
subscriber:
(
newState:
State
<
S
>
,
oldState:
State
<
S
>
)
→
(
)
) →
(
)
→
(
)
Subscribe a function to receive all state updates, including initial state (subscriber is called immediately).
Returns an unsubscribe function. Call the function to unsubscribe.
local unsubscribe = silo:Subscribe(function(newState, oldState)
-- Do something
end)
-- Later on, if desired, disconnect the subscription by calling unsubscribe:
unsubscribe()
Watch
Silo:
Watch
(
selector:
(
State
<
S
>
)
→
T
,
onChange:
(
T
)
→
(
)
) →
(
)
→
(
)
Watch a specific value within the state, which is selected by the
selector
function. The initial value, and any subsequent changes
grabbed by the selector, will be passed to the onChange
function.
Just like Subscribe
, a function is returned that can be used
to unsubscribe (i.e. stop watching).
local function SelectPoints(state)
return state.Statistics.Points
end
local unsubscribe = silo:Watch(SelectPoints, function(points)
print("Points", points)
end)
ResetToDefaultState
Silo:
ResetToDefaultState
(
) →
(
)
Reset the state to the default state that was given in the constructor.
local silo = Silo.new({
Points = 0,
}, {
SetPoints = function(state, points)
state.Points = points
end
})
silo:Dispatch(silo.Actions.SetPoints(10))
print(silo:GetState().Points) -- 10
silo:ResetToDefaultState()
print(silo:GetState().Points) -- 0