mirror of
https://github.com/edubart/otclient.git
synced 2025-12-15 13:19:46 +01:00
reorganize modules
This commit is contained in:
43
modules/core_scripts/dispatcher.lua
Normal file
43
modules/core_scripts/dispatcher.lua
Normal file
@@ -0,0 +1,43 @@
|
||||
local eventId = 0
|
||||
local eventsTable = { }
|
||||
local orig = { scheduleEvent = scheduleEvent,
|
||||
addEvent = addEvent }
|
||||
|
||||
-- fix original scheduleEvent
|
||||
function scheduleEvent(func, delay)
|
||||
eventId = eventId + 1
|
||||
local id = eventId
|
||||
local function proxyFunc()
|
||||
if eventsTable[id] then
|
||||
func()
|
||||
eventsTable[id] = nil
|
||||
end
|
||||
end
|
||||
eventsTable[id] = proxyFunc
|
||||
orig.scheduleEvent(proxyFunc, delay)
|
||||
return id
|
||||
end
|
||||
|
||||
-- FIXME: the event function can be collected
|
||||
-- and the dispatcher would call an invalid function, generating an warning
|
||||
function removeEvent(id)
|
||||
if id and eventsTable[id] then
|
||||
eventsTable[id] = nil
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
-- fix original addEvent
|
||||
function addEvent(func)
|
||||
eventId = eventId + 1
|
||||
local id = eventId
|
||||
local function proxyFunc()
|
||||
if eventsTable[id] then
|
||||
func()
|
||||
eventsTable[id] = nil
|
||||
end
|
||||
end
|
||||
eventsTable[id] = proxyFunc
|
||||
orig.addEvent(proxyFunc)
|
||||
return id
|
||||
end
|
||||
Reference in New Issue
Block a user