1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-01-10 00:43:27 +02:00
ComfyFactorio/utils/global.lua
2021-03-28 17:01:39 +02:00

54 lines
1.2 KiB
Lua

local Event = require 'utils.event_core'
local Token = require 'utils.token'
local Global = {
names = {}
}
local concat = table.concat
function Global.register(tbl, callback)
if _LIFECYCLE ~= _STAGE.control then
error('can only be called during the control stage', 2)
end
local filepath = debug.getinfo(2, 'S').source:match('^.+/currently%-playing/(.+)$'):sub(1, -5)
local token = Token.register_global(tbl)
Global.names[token] = concat {token, ' - ', filepath}
Event.on_load(
function()
callback(Token.get_global(token))
end
)
return token
end
function Global.register_init(tbl, init_handler, callback)
if _LIFECYCLE ~= _STAGE.control then
error('can only be called during the control stage', 2)
end
local filepath = debug.getinfo(2, 'S').source:match('^.+/currently%-playing/(.+)$'):sub(1, -5)
local token = Token.register_global(tbl)
Global.names[token] = concat {token, ' - ', filepath}
Event.on_init(
function()
init_handler(tbl)
callback(tbl)
end
)
Event.on_load(
function()
callback(Token.get_global(token))
end
)
return token
end
return Global