1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2024-12-14 10:13:13 +02:00
RedMew/utils/global.lua

88 lines
1.9 KiB
Lua
Raw Normal View History

local Event = require 'utils.event_core'
2018-11-26 03:07:03 +02:00
local Token = require 'utils.token'
2018-06-09 12:43:37 +02:00
2018-07-04 17:22:26 +02:00
local Global = {}
2018-06-09 12:43:37 +02:00
function Global.register(tbl, callback)
2019-02-18 08:43:59 +02:00
if _LIFECYCLE ~= _STAGE.control then
2019-02-04 23:17:00 +02:00
error('can only be called during the control stage', 2)
end
2018-06-09 12:43:37 +02:00
local token = Token.register_global(tbl)
Event.on_load(
function()
callback(Token.get_global(token))
end
)
2019-05-26 21:32:44 +02:00
return token
2018-06-17 12:39:55 +02:00
end
function Global.register_init(tbl, init_handler, callback)
2019-02-18 08:43:59 +02:00
if _LIFECYCLE ~= _STAGE.control then
2019-02-04 23:17:00 +02:00
error('can only be called during the control stage', 2)
end
2018-06-17 12:39:55 +02:00
local token = Token.register_global(tbl)
2018-06-09 12:43:37 +02:00
Event.on_init(
function()
init_handler(tbl)
callback(tbl)
2018-06-17 12:39:55 +02:00
end
)
2018-06-17 12:39:55 +02:00
Event.on_load(
function()
callback(Token.get_global(token))
2018-06-09 12:43:37 +02:00
end
)
2019-05-26 21:32:44 +02:00
return token
end
2018-06-09 12:43:37 +02:00
2019-01-31 00:16:44 +02:00
if _DEBUG then
local concat = table.concat
local names = {}
Global.names = names
function Global.register(tbl, callback)
local filepath = debug.getinfo(2, 'S').source:match('^.+/currently%-playing/(.+)$'):sub(1, -5)
local token = Token.register_global(tbl)
names[token] = concat {token, ' - ', filepath}
Event.on_load(
function()
callback(Token.get_global(token))
end
)
2019-05-26 21:32:44 +02:00
return token
2019-01-31 00:16:44 +02:00
end
function Global.register_init(tbl, init_handler, callback)
local filepath = debug.getinfo(2, 'S').source:match('^.+/currently%-playing/(.+)$'):sub(1, -5)
local token = Token.register_global(tbl)
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
)
2019-05-26 21:32:44 +02:00
return token
2019-01-31 00:16:44 +02:00
end
end
2018-06-09 12:43:37 +02:00
return Global