Fix warnings and improve doc
This commit is contained in:
parent
18046a9fcc
commit
f2e42fcb24
@ -1,5 +1,9 @@
|
|||||||
local sharedstate={}
|
local sharedstate={}
|
||||||
logging=require((...):gsub("(.)$", "%1.") .. 'logging')
|
local logging=require((...):gsub("(.)$", "%1.") .. 'logging')
|
||||||
|
|
||||||
|
-- function names
|
||||||
|
local is_initialized, callback_value, set_value,
|
||||||
|
get_value, resolve_key, resolve_index
|
||||||
|
|
||||||
-- 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.
|
||||||
@ -19,9 +23,9 @@ do
|
|||||||
local state_map={}
|
local state_map={}
|
||||||
|
|
||||||
--- Check if initialized
|
--- Check if initialized
|
||||||
-- Check if a key is initialized
|
---Check if a key is initialized
|
||||||
-- @param key key
|
---@param key string key
|
||||||
-- @return true if initialized, else false
|
---@return true if initialized, else false
|
||||||
function is_initialized(key)
|
function is_initialized(key)
|
||||||
logging.trace('is_initialized', key)
|
logging.trace('is_initialized', key)
|
||||||
return state_table[key] ~= nil
|
return state_table[key] ~= nil
|
||||||
@ -29,10 +33,10 @@ do
|
|||||||
|
|
||||||
|
|
||||||
--- Internal; ensure key is initialized
|
--- Internal; ensure key is initialized
|
||||||
--Properly initialize a key in all tables.
|
---Properly initialize a key in all tables.
|
||||||
--already initialized
|
---already initialized
|
||||||
--@param key key name
|
---@param key string key name
|
||||||
--@return true if key has been initialized, false if exists
|
---@return boolean if key has been initialized
|
||||||
local function init_key(key)
|
local function init_key(key)
|
||||||
logging.trace('init_key', key)
|
logging.trace('init_key', key)
|
||||||
if key == nil then
|
if key == nil then
|
||||||
@ -53,8 +57,8 @@ do
|
|||||||
end
|
end
|
||||||
|
|
||||||
--- Run callback function
|
--- Run callback function
|
||||||
-- Run the callback function of the given key
|
---Run the callback function of the given key
|
||||||
-- @param key key
|
---@param key string Key
|
||||||
function callback_value(key)
|
function callback_value(key)
|
||||||
logging.trace("callback_value", key)
|
logging.trace("callback_value", key)
|
||||||
local new_value=state_table[key]["value"]
|
local new_value=state_table[key]["value"]
|
||||||
@ -68,9 +72,9 @@ 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
|
||||||
-- @param key key
|
---@param key string key
|
||||||
-- @param value value
|
---@param value any value
|
||||||
-- @param callback callback function
|
---@param callback? function callback function
|
||||||
function set_value(key, value, callback)
|
function set_value(key, value, callback)
|
||||||
logging.trace("set_value", key, value, callback)
|
logging.trace("set_value", key, value, callback)
|
||||||
local initialized=init_key(key)
|
local initialized=init_key(key)
|
||||||
@ -89,9 +93,9 @@ do
|
|||||||
end
|
end
|
||||||
|
|
||||||
--- Get value
|
--- Get value
|
||||||
--Get a value from the state table
|
---Get a value from the state table
|
||||||
--@param key key
|
---@param key string key
|
||||||
--@return value from state table if set, else nil
|
---@return any value from state table if set, else nil
|
||||||
function get_value(key)
|
function get_value(key)
|
||||||
logging.trace("get_value", key)
|
logging.trace("get_value", key)
|
||||||
if not is_initialized(key) then return nil end
|
if not is_initialized(key) then return nil end
|
||||||
@ -99,50 +103,50 @@ do
|
|||||||
end
|
end
|
||||||
|
|
||||||
--- Resolve key
|
--- Resolve key
|
||||||
--Resolve key of given index
|
---Resolve key of given index
|
||||||
--@param index index
|
---@param index integer index
|
||||||
--@return key
|
---@return any key
|
||||||
function resolve_key(index)
|
function resolve_key(index)
|
||||||
logging.trace("resolve_key", index)
|
logging.trace("resolve_key", index)
|
||||||
return state_map[index]
|
return state_map[index]
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Resolve index
|
--- Resolve index
|
||||||
--Resolve index of given key
|
---Resolve index of given key
|
||||||
--@param key key
|
---@param key string key
|
||||||
--@return index
|
---@return integer index
|
||||||
function resolve_index(key)
|
function resolve_index(key)
|
||||||
logging.trace("resolve_index", key)
|
logging.trace("resolve_index", key)
|
||||||
return state_table[key]["index"]
|
return state_table[key]["index"]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Add an item to the shared state store.
|
---Add an item to the shared state store.
|
||||||
--Adds an item to the shared state store, as well as initializes it with a
|
---Adds an item to the shared state store, as well as initializes it with a
|
||||||
--value. This does not send a ping and should only be used during avatar
|
---value. This does not send a ping and should only be used during avatar
|
||||||
--initialization.
|
---initialization.
|
||||||
--@param key key name
|
---@param key string key name
|
||||||
--@param value initial value
|
---@param value any initial value
|
||||||
--@param callback Callback function to run on value change
|
---@param callback function Callback function to run on value change
|
||||||
function sharedstate.add(key, value, callback)
|
function sharedstate.add(key, value, callback)
|
||||||
logging.trace("sharedstate.add", key, value, callback)
|
logging.trace("sharedstate.add", key, value, callback)
|
||||||
set_value(key, value, callback)
|
set_value(key, value, callback)
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Internal; transfer value over network
|
--- Internal; transfer value over network
|
||||||
-- Ping used to transfer a value over the network, should not be called
|
---Ping used to transfer a value over the network, should not be called
|
||||||
-- directly
|
---directly
|
||||||
-- @param index Index of key
|
---@param index integer Index of key
|
||||||
-- @param value New value
|
---@param value any New value
|
||||||
function pings.sharedstate_transfer(index, value)
|
function pings.sharedstate_transfer(index, value)
|
||||||
logging.trace("pings.sharedstate_transfer", index, value)
|
logging.trace("pings.sharedstate_transfer", index, value)
|
||||||
set_value(resolve_key(index), value)
|
set_value(resolve_key(index), 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 key name
|
---@param key string key name
|
||||||
-- @param value value
|
---@param value any value
|
||||||
function sharedstate.set(key, value)
|
function sharedstate.set(key, value)
|
||||||
logging.trace("sharedstate.set", key, value)
|
logging.trace("sharedstate.set", key, value)
|
||||||
if not is_initialized(key) then
|
if not is_initialized(key) then
|
||||||
|
Loading…
x
Reference in New Issue
Block a user