local que = {}
local log = {}

function AddToQue(key, f)
    que[key] = que[key] or {}
    local list = que[key]

    local new_index = #list + 1
    list[new_index] = f

    table.insert(log, { key, new_index })

    return { key, new_index }
end

function RemoveFromQue(ticket)
    local list = que[ticket[1]]
    if list then
        table.remove(list, ticket[2])
    end
end

function RemoveMultipleFromQue(tickets)
    for _, t in pairs(tickets) do
        RemoveFromQue(t)
    end
end
-- function RemoveLastQues(I)
--     for i=1, I do
--         local l = table.remove(log, 1)
--         RemoveFromQue(l[1], l[2])
--     end
-- end

function KillQues()
    for key in pairs(que) do
        que[key] = nil
    end
end

function ProcessQue(key, ret_result)
    if not que[key] then return end

    for i, f in ipairs(que[key]) do
        local success, result = pcall(f)
        if not success then
            error('Bad call to ' .. key, 2)
        end
        if ret_result then
            return result
        end
    end
end
