class 'EventDispatcher'
-------------------------------------------------------------------------------

EventDispatcher.lastId = -1

function EventDispatcher:__init()
    self.listeners = {}
end
-------------------------------------------------------------------------------

function EventDispatcher:destroy()
    self.listeners = nil
end
-------------------------------------------------------------------------------

function EventDispatcher:addEventListener(eventName, listener)
    if not self.listeners[eventName] then
        self.listeners[eventName] = {}
    end
    
    EventDispatcher.lastId = EventDispatcher.lastId + 1
    
    table.insert(self.listeners[eventName], {
                                                id = EventDispatcher.lastId,
                                                listener = listener
                                            })
    
    return EventDispatcher.lastId
end
-------------------------------------------------------------------------------

function EventDispatcher:removeEventListener(eventName, id)
    local eventListeners = self.listeners[eventName]
    
    if eventListeners then
        for i, listenerInfo in ipairs(eventListeners) do
            if listenerInfo.id == id then
                table.remove(eventListeners[i])
                return true
            end
        end
    end
    
    return false
end
-------------------------------------------------------------------------------

function EventDispatcher:dispatchEvent(event)
    for eventName, listeners in pairs(self.listeners) do
        if eventName == event.eventName then
            for i=1, #listeners do
                listeners[i].listener(event)
            end
            break
        end
    end
end
-------------------------------------------------------------------------------