Add functions to queue multiple values for sync
This commit is contained in:
parent
0fa6341bf2
commit
92b4b99083
@ -5,9 +5,8 @@ local logging=require((...):gsub("(.)$", "%1.") .. 'logging')
|
|||||||
local is_initialized, callback_value, set_value,
|
local is_initialized, callback_value, set_value,
|
||||||
get_value, resolve_key, resolve_index
|
get_value, resolve_key, resolve_index
|
||||||
|
|
||||||
-- list of "dirty" keys that are queued for sync. This table should be
|
--schema: similar to state_table, nested table only contains value
|
||||||
-- accessible to the entirety of the sharedstate library
|
local state_queue={}
|
||||||
local state_queued={}
|
|
||||||
|
|
||||||
-- we're protecting internal variables here, i'm *that* scared of writing bad
|
-- we're protecting internal variables here, i'm *that* scared of writing bad
|
||||||
-- code again.
|
-- code again.
|
||||||
@ -184,21 +183,46 @@ function sharedstate.set(key, value)
|
|||||||
-- pings.sharedstate_recv_named(key, value)
|
-- pings.sharedstate_recv_named(key, value)
|
||||||
end
|
end
|
||||||
|
|
||||||
---Send queued entries
|
---Queue entries for sending
|
||||||
--Send queued table entries to other hosts in one ping.
|
--Queues entries for sending over a single ping, as to prevent being rate
|
||||||
function sharedstate.send()
|
--limited. Values will not be accessible until sharedstate.commit() is run.
|
||||||
--schema: similar to state_table, nested table only contains value
|
---@param key string key
|
||||||
local send_queue={}
|
---@param value any value
|
||||||
-- this is intentional values in state_queued are key names
|
function sharedstate.queue(key, value)
|
||||||
for _, key in pairs(state_queued) do
|
local t={
|
||||||
local t={
|
["value"] = value
|
||||||
["value"]=get_value(key)
|
}
|
||||||
}
|
state_queue[key]=t
|
||||||
send_queue[key]=t
|
end
|
||||||
end
|
|
||||||
|
|
||||||
pings.sharedstate_unpack(send_queue)
|
---Commit queued entries
|
||||||
state_queued={}
|
--Commit queued table entries and send in one ping.
|
||||||
|
function sharedstate.commit()
|
||||||
|
--schema: similar to state_table, nested table only contains value
|
||||||
|
-- local send_queue={}
|
||||||
|
-- -- this is intentional values in state_queued are key names
|
||||||
|
-- for _, key in pairs(state_queued) do
|
||||||
|
-- local t={
|
||||||
|
-- ["value"]=get_value(key)
|
||||||
|
-- }
|
||||||
|
-- send_queue[key]=t
|
||||||
|
-- end
|
||||||
|
|
||||||
|
|
||||||
|
pings.sharedstate_unpack(state_queue)
|
||||||
|
state_queue={}
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Clear a value from the shared state queue
|
||||||
|
--Clears a value from the shared state queue. If no key is specified, clears
|
||||||
|
--all queued values.
|
||||||
|
---@param key? string Key to clear
|
||||||
|
function sharedstate.clear(key)
|
||||||
|
if key ~= nil then
|
||||||
|
state_queue[key] = nil
|
||||||
|
else
|
||||||
|
state_queue={}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- this can be copied directly from the internal functions as it is read only
|
-- this can be copied directly from the internal functions as it is read only
|
||||||
|
Loading…
x
Reference in New Issue
Block a user