1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2025-01-18 03:21:47 +02:00
RedMew/utils/global.lua

88 lines
1.9 KiB
Lua
Raw Normal View History

local Event = require 'utils.event_core'
2018-11-25 20:07:03 -05:00
local Token = require 'utils.token'
2018-06-09 11:43:37 +01:00
2018-07-04 16:22:26 +01:00
local Global = {}
2018-06-09 11:43:37 +01:00
function Global.register(tbl, callback)
2019-02-18 01:43:59 -05:00
if _LIFECYCLE ~= _STAGE.control then
2019-02-04 16:17:00 -05:00
error('can only be called during the control stage', 2)
end
2018-06-09 11:43:37 +01:00
local token = Token.register_global(tbl)
Event.on_load(
function()
callback(Token.get_global(token))
end
)
2019-05-26 20:32:44 +01:00
return token
2018-06-17 11:39:55 +01:00
end
function Global.register_init(tbl, init_handler, callback)
2019-02-18 01:43:59 -05:00
if _LIFECYCLE ~= _STAGE.control then
2019-02-04 16:17:00 -05:00
error('can only be called during the control stage', 2)
end
2018-06-17 11:39:55 +01:00
local token = Token.register_global(tbl)
2018-06-09 11:43:37 +01:00
Event.on_init(
function()
init_handler(tbl)
callback(tbl)
2018-06-17 11:39:55 +01:00
end
)
2018-06-17 11:39:55 +01:00
Event.on_load(
function()
callback(Token.get_global(token))
2018-06-09 11:43:37 +01:00
end
)
2019-05-26 20:32:44 +01:00
return token
end
2018-06-09 11:43:37 +01:00
2019-01-30 22:16:44 +00: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 20:32:44 +01:00
return token
2019-01-30 22:16:44 +00: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 20:32:44 +01:00
return token
2019-01-30 22:16:44 +00:00
end
end
2018-06-09 11:43:37 +01:00
return Global