utils
Functions
batch
utils.batch(f: () → ...unknown) → ()Batches any state write operation up until the given function is done running.
local text = swan.state("Hey, how is it going?")
swan.effect(function()
print(text())
end)
utils.batch(function()
text("Hello, World!")
text("Bye, World!")
end) -- the previous effect will only print "Bye, World!"
utils.batch(function()
text.value = "Oops"
end) -- be careful! If the last write operation is done through .value, the effect will also trigger! -- "Oops"
branch
utils.branch(...: any--
The values and the branches.
) → ()Branching function to be used within effects.
Pass a condition to be checked, and after it a function to be run if it's successful. The last value, if a function, and if not after a condition, will be threated as the "else" branch.
local age = swan.state(16)
local role = "admin"
local bad_validator = swan.effect(function()
if role ~= "admin" then -- admins can bypass the age verification
assert(age() >= 18, "Your age is too low!")
end
end) -- won't detect `age` since in the first run, the condition was false, and age() wasn't called.
local validator = swan.effect(function()
utils.branch(
role ~= "admin", function()
assert(age() >= 18, "Your age is too low!")
end
)
end) -- will detect `age`
is_state
utils.is_state(v: T) → booleanReturns whether the given value is a state.
local not_a_state = "Hey"
print(utils.is_state(not_a_state)) -- false
local a_state = swan.state("Cool")
print(utils.is_state(a_state)) -- true
is_proxy
utils.is_proxy(v: T) → booleanReturns whether the given value is a proxy.
local not_a_proxy = "Hey"
print(utils.is_proxy(not_a_proxy)) -- false
local a_proxy_state = swan.proxy_state({
hello = "world"
})
print(utils.is_proxy(a_proxy_state())) -- true
is_proxy_state
utils.is_proxy_state(v: T) → booleanReturns whether the given value is a state holding a proxy.
local not_a_proxy_state = "Hey"
print(utils.is_proxy_state(not_a_proxy_state)) -- false
not_a_proxy_state = swan.state("Hey")
print(utils.is_proxy_state(not_a_proxy_state)) -- false
local a_proxy_state = swan.proxy_state({
hello = "world"
})
print(utils.is_proxy_state(a_proxy_state)) -- true
get_real
utils.get_real(pr: proxy_base_hack<T>) → TReturns the "real" value of a proxy.
local todos = swan.proxy_state {
"go outside",
"touch grass",
"write more todos"
}
print(todos(), todos()[1]) -- {}, "go outside"
print(swan.get_real(todos())) --[[ {
"go outside",
"touch grass",
"write more todos"
}
]]--
state_get_real
utils.state_get_real(proxy_state: state<proxy_base_hack<T>>) → TReturns the "real" value of a state's proxy.
local todos = swan.proxy_state {
"go outside",
"touch grass",
"write more todos"
}
print(todos(), todos()[1]) -- {}, "go outside"
print(swan.state_get_real(todos)) --[[ {
"go outside",
"touch grass",
"write more todos"
}
]]--
clear
utils.clear(state: state<T>?) → ()Disconnects all the effects from a specific state or in general.
local money = swan.state(10000)
local health = swan.state(100)
swan.effect(function()
print("Your money:", money())
print("Your health:", health())
end)
swan.effect(function()
print("Your money (again):", money())
end)
money(100) -- "Your money: 100", "Your health: 50", "Your money (again): 100"
health(50) -- "Your money: 100", "Your health: 50"
swan.clear(health)
health(200) -- *crickets*
money(50) -- "Your money: 50", "Your health: 200", "Your money (again): 50"
swan.clear()
money(10) -- *crickets*