mirror of
https://github.com/MikaylaFischler/cc-mek-scada.git
synced 2024-08-30 18:22:34 +00:00
notify subscriber right away if there is already a value present
This commit is contained in:
parent
17954ef3d0
commit
350370a084
@ -13,17 +13,26 @@ function psil.create()
|
||||
-- allocate a new interconnect field
|
||||
---@key string data key
|
||||
local function alloc(key)
|
||||
self.ic[key] = { subscribers = {}, value = 0 }
|
||||
self.ic[key] = { subscribers = {}, value = nil }
|
||||
end
|
||||
|
||||
---@class psil
|
||||
local public = {}
|
||||
|
||||
-- subscribe to a data object in the interconnect
|
||||
--
|
||||
-- will call func() right away if a value is already avaliable
|
||||
---@param key string data key
|
||||
---@param func function function to call on change
|
||||
function public.subscribe(key, func)
|
||||
if self.ic[key] == nil then alloc(key) end
|
||||
-- allocate new key if not found or notify if value is found
|
||||
if self.ic[key] == nil then
|
||||
alloc(key)
|
||||
elseif self.ic[key].value ~= nil then
|
||||
func(self.ic[key].value)
|
||||
end
|
||||
|
||||
-- subscribe to key
|
||||
table.insert(self.ic[key].subscribers, { notify = func })
|
||||
end
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user