Skip to content

Commit

Permalink
Centralize transmission in PerformSend function
Browse files Browse the repository at this point in the history
This means individual call sites trying to send messages out on the wire
don't each need to deal with duplicated details of state management
and correct processing of return values.

Also means some small docs can be added in-line, because someone's
going to look at the select trick and wonder what's going on in
a few years.
  • Loading branch information
Meorawr committed May 2, 2024
1 parent 74565d8 commit 21b4289
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions AceComm-3.0/ChatThrottleLib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,20 @@ local function WasMessageThrottled(result)
or result == SendAddonMessageResult.ChannelThrottle
end

local function PerformSend(sendFunction, ...)
-- The select(-1, true, ...) incantation grabs the last return value
-- from the called function, defaulting to true if it returned nothing.
--
-- The default case is required for SendChatMessage. The selection of
-- the last return value is required due to per-flavor API divergences.

bMyTraffic = true
local sendResult = select(-1, true, sendFunction(...))
bMyTraffic = false

return MapToSendResult(sendResult)
end

function ChatThrottleLib:Despool(Prio)
local ring = Prio.Ring
while ring.pos and Prio.avail > ring.pos[1].nSize do
Expand All @@ -359,9 +373,7 @@ function ChatThrottleLib:Despool(Prio)
-- do nothing
sendResult = SendAddonMessageResult.NotInGroup
else
bMyTraffic = true
sendResult = MapToSendResult(select(-1, true, msg.f(unpack(msg, 1, msg.n))))
bMyTraffic = false
sendResult = PerformSend(msg.f, unpack(msg, 1, msg.n))
end

if WasMessageThrottled(sendResult) then
Expand Down Expand Up @@ -509,9 +521,7 @@ function ChatThrottleLib:SendChatMessage(prio, prefix, text, chattype, languag
-- Check if there's room in the global available bandwidth gauge to send directly
if not self.bQueueing and nSize < self:UpdateAvail() then
self.avail = self.avail - nSize
bMyTraffic = true
_G.SendChatMessage(text, chattype, language, destination)
bMyTraffic = false
PerformSend(_G.SendChatMessage, text, chattype, language, destination)
self.Prio[prio].nTotalSent = self.Prio[prio].nTotalSent + nSize
if callbackFn then
-- We have no success/failure information here, and so may lie.
Expand Down Expand Up @@ -563,9 +573,7 @@ function ChatThrottleLib:SendAddonMessage(prio, prefix, text, chattype, target,
-- Check if there's room in the global available bandwidth gauge to send directly
if not self.bQueueing and nSize < self:UpdateAvail() then
self.avail = self.avail - nSize
bMyTraffic = true
local sendResult = MapToSendResult(select(-1, _G.C_ChatInfo.SendAddonMessage(prefix, text, chattype, target)))
bMyTraffic = false
local sendResult = PerformSend(_G.C_ChatInfo.SendAddonMessage, prefix, text, chattype, target)

if not WasMessageThrottled(sendResult) then
local didSend = (sendResult == SendAddonMessageResult.Success)
Expand Down

0 comments on commit 21b4289

Please sign in to comment.