Compare commits

...

3 Commits

Author SHA1 Message Date
ad783d8a7e
sharedstate: don't send unchanged values 2023-06-25 00:46:22 -04:00
3c13cacfc9
sharedstate: higher log level for setting 2023-06-25 00:46:01 -04:00
f6451345cd
sharedstate: fix issue with falsy values
Setting a shared value to false would retain the old value since the
logic uses lua's fake "ternary"
2023-06-25 00:42:03 -04:00

View File

@ -8,6 +8,10 @@ get_value, resolve_key, resolve_index
--schema: similar to state_table, nested table only contains value
local state_queue={}
local function ternary(condition, val_if_true, val_if_false)
if condition then return val_if_true else return val_if_false end
end
-- we're protecting internal variables here, i'm *that* scared of writing bad
-- code again.
-- this section is purely for making sure the tables are in a consistent state
@ -81,7 +85,7 @@ do
---@param value any value
---@param callback? function callback function
function set_value(key, value, callback)
logging.trace("set_value", key, value, callback)
logging.debug("sharedstate: key " .. tostring(key) .. " set to " .. tostring(value))
local initialized=init_key(key)
local entry=state_table[key]
if initialized then
@ -90,7 +94,7 @@ do
entry["callback"]=callback
else
entry["value"]=value or entry["value"]
entry["value"]=ternary(value ~= nil, value, entry["value"])
callback_value(key)
entry["callback"]=callback or entry["callback"]
entry["old_value"]=value
@ -179,8 +183,11 @@ function sharedstate.set(key, value)
local errormsg="sharedstate: Key " .. key .. " has not been initialized."
error(errormsg)
end
pings.sharedstate_recv(resolve_index(key), value)
-- pings.sharedstate_recv_named(key, value)
-- don't bother sending unchanged values
if value ~= get_value(key) then
-- pings.sharedstate_recv(resolve_index(key), value)
pings.sharedstate_recv_named(key, value)
end
end
---Queue entries for sending