1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2024-12-12 10:04:40 +02:00
RedMew/utils/global.lua
grilledham d1f2eb5686 Event (#565)
* First pass at removable events

* added documentation

* added is_closure function

* fixed return multiple values

* is_closure now ignores _ENV
2018-12-28 17:21:45 +01:00

47 lines
1.0 KiB
Lua

local Event = require 'utils.event_core'
local Token = require 'utils.token'
local Global = {}
local load_data = {}
local init_data = {}
function Global.register(tbl, callback)
local token = Token.register_global(tbl)
table.insert(load_data, {callback = callback, token = token})
end
function Global.register_init(tbl, init_handler, callback)
local token = Token.register_global(tbl)
table.insert(load_data, {callback = callback, token = token})
table.insert(init_data, {token = token, init_handler = init_handler, callback = callback})
end
Event.on_load(
function()
for _, d in ipairs(load_data) do
local tbl = Token.get_global(d.token)
d.callback(tbl)
end
load_data = nil
init_data = nil
end
)
Event.on_init(
function()
for _, d in ipairs(init_data) do
local tbl = Token.get_global(d.token)
d.init_handler(tbl)
d.callback(tbl)
end
load_data = nil
init_data = nil
end
)
return Global