Compare commits
No commits in common. "92b4b99083293c45aa026ee19435be624f9c03f7" and "b9975082d0a59515328a5b72e5704a7a54b8f12d" have entirely different histories.
92b4b99083
...
b9975082d0
@ -5,8 +5,9 @@ local logging=require((...):gsub("(.)$", "%1.") .. 'logging')
|
||||
local is_initialized, callback_value, set_value,
|
||||
get_value, resolve_key, resolve_index
|
||||
|
||||
--schema: similar to state_table, nested table only contains value
|
||||
local state_queue={}
|
||||
-- list of "dirty" keys that are queued for sync. This table should be
|
||||
-- accessible to the entirety of the sharedstate library
|
||||
local state_queued={}
|
||||
|
||||
-- we're protecting internal variables here, i'm *that* scared of writing bad
|
||||
-- code again.
|
||||
@ -75,8 +76,7 @@ do
|
||||
|
||||
--- Set value and run callback
|
||||
-- Sets a value in the state table, initializing it if neede, and runs
|
||||
-- the callback if it has been previously set. This function should
|
||||
-- only be called within ping receiver functions
|
||||
-- the callback if it has been previously set
|
||||
---@param key string key
|
||||
---@param value any value
|
||||
---@param callback? function callback function
|
||||
@ -145,8 +145,8 @@ sharedstate.add=sharedstate.init
|
||||
---directly
|
||||
---@param index integer Index of key
|
||||
---@param value any New value
|
||||
function pings.sharedstate_recv(index, value)
|
||||
logging.trace("pings.sharedstate_recv", index, value)
|
||||
function pings.sharedstate_transfer(index, value)
|
||||
logging.trace("pings.sharedstate_transfer", index, value)
|
||||
set_value(resolve_key(index), value)
|
||||
end
|
||||
|
||||
@ -155,19 +155,11 @@ end
|
||||
---directly
|
||||
---@param key string Key
|
||||
---@param value any New value
|
||||
function pings.sharedstate_recv_named(key, value)
|
||||
logging.trace("pings.sharedstate_recv_named", key, value)
|
||||
function pings.sharedstate_ktransfer(key, value)
|
||||
logging.trace("pings.sharedstate_ktransfer", key, value)
|
||||
set_value(key, value)
|
||||
end
|
||||
|
||||
--- Internal; unpack a table of state into the local state store
|
||||
---@param tbl table Table to unpack
|
||||
function pings.sharedstate_recv_table(tbl)
|
||||
for k, v in pairs(tbl) do
|
||||
set_value(k, v["value"])
|
||||
end
|
||||
end
|
||||
|
||||
--- Set shared value
|
||||
---Sets a shared value. This sends a ping to transfer it over the network.
|
||||
---@param key string key name
|
||||
@ -179,50 +171,25 @@ 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)
|
||||
pings.sharedstate_transfer(resolve_index(key), value)
|
||||
-- pings.sharedstate_ktransfer(key, value)
|
||||
end
|
||||
|
||||
---Queue entries for sending
|
||||
--Queues entries for sending over a single ping, as to prevent being rate
|
||||
--limited. Values will not be accessible until sharedstate.commit() is run.
|
||||
---@param key string key
|
||||
---@param value any value
|
||||
function sharedstate.queue(key, value)
|
||||
local t={
|
||||
["value"] = value
|
||||
}
|
||||
state_queue[key]=t
|
||||
end
|
||||
|
||||
---Commit queued entries
|
||||
--Commit queued table entries and send in one ping.
|
||||
function sharedstate.commit()
|
||||
---Send queued entries
|
||||
--Send queued table entries to other hosts in one ping.
|
||||
function sharedstate.send()
|
||||
--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={}
|
||||
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(send_queue)
|
||||
state_queued={}
|
||||
end
|
||||
|
||||
-- this can be copied directly from the internal functions as it is read only
|
||||
|
Loading…
x
Reference in New Issue
Block a user