local DispatchRequestManager = BaseClass("DispatchRequestManager")
local __init = function(self)
  self.queue = {}
end
local __delete = function(self)
  self.queue = nil
  if self.executeTimer then
    self.executeTimer:Stop()
    self.executeTimer = nil
  end
end
local Append = function(self, func)
  table.insert(self.queue, func)
  self:TryStartExecuteTimer()
end
local Prepend = function(self, func)
  table.insert(self.queue, 1, func)
  self:TryStartExecuteTimer()
end
local Pop = function(self)
  if table.count(self.queue) == 0 then
    return nil
  end
  return table.remove(self.queue, 1)
end
local Execute = function(self)
  local element = self:Pop()
  if element then
    element()
    self.executeTimer:Stop()
    self.executeTimer = nil
    if table.count(self.queue) > 0 then
      self:TryStartExecuteTimer()
    end
  end
end
local TryStartExecuteTimer = function(self)
  if self.executeTimer == nil then
    local randomDelay = math.random()
    randomDelay = randomDelay * 2
    self.executeTimer = TimerManager:GetInstance():DelayInvoke(function()
      self:Execute()
    end, randomDelay)
    self.executeTimer:Start()
  end
end
DispatchRequestManager.__init = __init
DispatchRequestManager.__delete = __delete
DispatchRequestManager.Append = Append
DispatchRequestManager.Prepend = Prepend
DispatchRequestManager.Pop = Pop
DispatchRequestManager.Execute = Execute
DispatchRequestManager.TryStartExecuteTimer = TryStartExecuteTimer
return DispatchRequestManager
