Compare commits
2 Commits
b9975082d0
...
92b4b99083
Author | SHA1 | Date | |
---|---|---|---|
92b4b99083 | |||
0fa6341bf2 |
@ -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.
|
||||||
@ -76,7 +75,8 @@ do
|
|||||||
|
|
||||||
--- Set value and run callback
|
--- Set value and run callback
|
||||||
-- Sets a value in the state table, initializing it if neede, and runs
|
-- Sets a value in the state table, initializing it if neede, and runs
|
||||||
-- the callback if it has been previously set
|
-- the callback if it has been previously set. This function should
|
||||||
|
-- only be called within ping receiver functions
|
||||||
---@param key string key
|
---@param key string key
|
||||||
---@param value any value
|
---@param value any value
|
||||||
---@param callback? function callback function
|
---@param callback? function callback function
|
||||||
@ -145,8 +145,8 @@ sharedstate.add=sharedstate.init
|
|||||||
---directly
|
---directly
|
||||||
---@param index integer Index of key
|
---@param index integer Index of key
|
||||||
---@param value any New value
|
---@param value any New value
|
||||||
function pings.sharedstate_transfer(index, value)
|
function pings.sharedstate_recv(index, value)
|
||||||
logging.trace("pings.sharedstate_transfer", index, value)
|
logging.trace("pings.sharedstate_recv", index, value)
|
||||||
set_value(resolve_key(index), value)
|
set_value(resolve_key(index), value)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -155,11 +155,19 @@ end
|
|||||||
---directly
|
---directly
|
||||||
---@param key string Key
|
---@param key string Key
|
||||||
---@param value any New value
|
---@param value any New value
|
||||||
function pings.sharedstate_ktransfer(key, value)
|
function pings.sharedstate_recv_named(key, value)
|
||||||
logging.trace("pings.sharedstate_ktransfer", key, value)
|
logging.trace("pings.sharedstate_recv_named", key, value)
|
||||||
set_value(key, value)
|
set_value(key, value)
|
||||||
end
|
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
|
--- Set shared value
|
||||||
---Sets a shared value. This sends a ping to transfer it over the network.
|
---Sets a shared value. This sends a ping to transfer it over the network.
|
||||||
---@param key string key name
|
---@param key string key name
|
||||||
@ -171,25 +179,50 @@ function sharedstate.set(key, value)
|
|||||||
local errormsg="sharedstate: Key " .. key .. " has not been initialized."
|
local errormsg="sharedstate: Key " .. key .. " has not been initialized."
|
||||||
error(errormsg)
|
error(errormsg)
|
||||||
end
|
end
|
||||||
pings.sharedstate_transfer(resolve_index(key), value)
|
pings.sharedstate_recv(resolve_index(key), value)
|
||||||
-- pings.sharedstate_ktransfer(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