Skip to content

Instantly share code, notes, and snippets.

@nefftd
Created June 22, 2014 16:09
Show Gist options
  • Save nefftd/77197c075f0556edd724 to your computer and use it in GitHub Desktop.
Save nefftd/77197c075f0556edd724 to your computer and use it in GitHub Desktop.
Super simple bucket implementation for World of Warcraft addons, to deal with events which are too spammy for their own good.
-- Here's a super-simple implementation of something like bucket events. The
-- idea is, you want to call some code, but the event you're listening to is
-- very spammy (many times per second!), and it's just not necessary to update
-- that often. So when the event first occurs, we start a timer to call the
-- actual routines. Subsequent occurances of the event simply fail silently
-- until the timer is finished. So your code only runs at most every N seconds
-- see: BUCKET_DELAY. Caveat: even if the event only runs once, there will be a
-- delay of N seconds before your routines get called. Tweak BUCKET_DELAY to
-- your needs. Caveat 2: you can't collect arguments (can be implemented, but
-- not trivially).
local BUCKET_DELAY = 1.0 -- Fire at most once per second
local function my_bucket()
-- your routine
end
local throttle = 0
local tframe = CreateFrame('Frame')
tframe:SetScript('OnUpdate',function(self,elapsed)
throttle = throttle + elapsed
if throttle >= BUCKET_DELAY then
throttle = 0
self:Hide()
my_bucket() -- Do this last, so if there's an error, it doesn't prevent ending the timer
end
end)
local function SOME_EVENT_CALLBACK()
-- Your event callback.
tframe:Show() -- Start the timer if it's not already started
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment