ObjectPoolMixin:Acquire()

From Warcraft Wiki
Jump to navigation Jump to search
These functions are implemented in :Acquire FrameXML/Pools.lua.

Furnishes a widget, or returns it to the object pool.

widget = pool:Acquire()
success = pool:Release(widget)
pool:ReleaseAll()

Arguments / Returns

widget
UIObject - Furnished by Acquire() or returned by Release().
success
boolean - Returned by Release() as true if returning the widget to the pool, or false otherwise (the widget is not even part of the pool, or was previously returned).

Details

  • Acquire() draws a widget from the pool, preferring to reuse a previously-released one but using the pool's creationFunc() if necessary.
    • Widgets are reused in reverse order to how they were released (LIFO).
  • All three functions apply the pool's resetterFunc() (unless it is nil) to affected widgets during each operation.
  • ReleaseAll() returns all widgets to the pool.

Example

-- initialize the pool
local function creationFunc()
	return CreateFrame("Frame")
end
local function resetterFunc(__, frame)
	frame:Hide()
end
local pool = CreateObjectPool(creationFunc, resetterFunc)

-- Request a frame
local frame = pool:Acquire()
frame:Show()

-- Return the frame when it is no longer required
pool:Release(frame)

-- Request a frame again
local frame = pool:Acquire()  -- could be a different frame if others were acquired/released in the interim
frame:Show() -- cancels Hide() in resetterFunc

Patch changes

  • Battle for Azeroth Patch 8.0.1 (2018-07-17): Release() now returns success and only acts on active widgets drawn from the pool.[1]
  • Legion Patch 7.2.0 (2017-03-28): Acquire(), Release() and ReleaseAll() added.[2]

References

 
  1. ^ 2018-07-16, Pools.lua, version 8.0.1.27101, near line 104, archived at Townlong-Yak
  2. ^ 2017-03-28, Pools.lua, version 7.3.0.23835, near line 75, archived at Townlong-Yak