factory.lua 1.57 KB
Newer Older
1 2 3 4 5 6
local parent, ns = ...
local oUF = ns.oUF
local Private = oUF.Private

local argcheck = Private.argcheck

Adrian L Lange's avatar
Adrian L Lange committed
7
local queue = {}
Rainrider's avatar
Rainrider committed
8
local factory = CreateFrame('Frame')
Adrian L Lange's avatar
Adrian L Lange committed
9
factory:SetScript('OnEvent', function(self, event, ...)
10 11 12
	return self[event](self, event, ...)
end)

Rainrider's avatar
Rainrider committed
13
factory:RegisterEvent('PLAYER_LOGIN')
Adrian L Lange's avatar
Adrian L Lange committed
14
factory.active = true
15

Adrian L Lange's avatar
Adrian L Lange committed
16
function factory:PLAYER_LOGIN()
17 18
	if(not self.active) then return end

Adrian L Lange's avatar
Adrian L Lange committed
19
	for _, func in next, queue do
20 21
		func(oUF)
	end
22 23

	-- Avoid creating dupes.
Adrian L Lange's avatar
Adrian L Lange committed
24
	wipe(queue)
25 26
end

Rainrider's avatar
Rainrider committed
27
--[[ Factory: oUF:Factory(func)
Rainrider's avatar
Rainrider committed
28 29 30 31
Used to call a function directly if the current character is logged in and the factory is active. Else the function is
queued up to be executed at a later time (upon PLAYER_LOGIN by default).

* self - the global oUF object
Rainrider's avatar
Rainrider committed
32
* func - function to be executed or delayed (function)
Rainrider's avatar
Rainrider committed
33
--]]
34 35 36
function oUF:Factory(func)
	argcheck(func, 2, 'function')

37
	-- Call the function directly if we're active and logged in.
Adrian L Lange's avatar
Adrian L Lange committed
38
	if(IsLoggedIn() and factory.active) then
39 40
		return func(self)
	else
Adrian L Lange's avatar
Adrian L Lange committed
41
		table.insert(queue, func)
42
	end
43 44
end

Rainrider's avatar
Rainrider committed
45
--[[ Factory: oUF:EnableFactory()
Rainrider's avatar
Rainrider committed
46 47 48 49
Used to enable the factory.

* self - the global oUF object
--]]
50
function oUF:EnableFactory()
Adrian L Lange's avatar
Adrian L Lange committed
51
	factory.active = true
52 53
end

Rainrider's avatar
Rainrider committed
54
--[[ Factory: oUF:DisableFactory()
Rainrider's avatar
Rainrider committed
55 56 57 58
Used to disable the factory.

* self - the global oUF object
--]]
59
function oUF:DisableFactory()
Adrian L Lange's avatar
Adrian L Lange committed
60
	factory.active = nil
61
end
62

Rainrider's avatar
Rainrider committed
63
--[[ Factory: oUF:RunFactoryQueue()
Rainrider's avatar
Rainrider committed
64 65 66 67 68
Used to try to execute queued up functions. The current player must be logged in and the factory must be active for
this to succeed.

* self - the global oUF object
--]]
69
function oUF:RunFactoryQueue()
Adrian L Lange's avatar
Adrian L Lange committed
70
	factory:PLAYER_LOGIN()
71
end