--Debug inspection. aa to show at beginning of Zgoo
--Uncomment to ignore
MISSED_CALLS = {}

Promise = {}
setmetatable(Promise, {
    __index = function(self, key)
        self:__preSave(key)
        return (self.__save)
    end,
    __call = function (cls, ...)
        return cls.__new(...)
    end
})
function Promise:__new()
    local o = setmetatable({}, {
        __index = Promise,
        __call = function (cls, ...)
            return cls:__execute(...)
        end
    })
    o.callList = {}
    o.nextKey = nil

    return o
end

function Promise:__getMissedCalls()
    return missedCalls
end

function Promise:__preSave(key)
    self.nextKey = key
end

function Promise:__save(...)
    if self.nextKey == nil then return end

    table.insert(self.callList, {
        func = self.nextKey,
        narg = select('#', ...),
        varargs = {...}
    })
    self.nextKey = nil
end

function Promise:__execute(obj)
    for k,v in ipairs(self.callList) do
        --Add info for debug inspection
        if MISSED_CALLS then
            table.insert(aaMISSED_CALLS, k .. ":" .. v.func .. "(" .. table.concat(v.varargs,",") .. ")")
        end
        obj[v.func](v.func, unpack(v.varargs))
    end
end