annotate Libs/AceEvent-3.0/AceEvent-3.0.lua @ 15:21cefa363c73 tip

Last modification to ready checking broke something ... Now its fixed and ready checking is working as intended.
author Xiiph
date Fri, 25 Feb 2011 01:21:13 +0100
parents 98c6f55e6619
children
rev   line source
Xiiph@0 1 --- AceEvent-3.0 provides event registration and secure dispatching.
Xiiph@0 2 -- All dispatching is done using **CallbackHandler-1.0**. AceEvent is a simple wrapper around
Xiiph@0 3 -- CallbackHandler, and dispatches all game events or addon message to the registrees.
Xiiph@0 4 --
Xiiph@0 5 -- **AceEvent-3.0** can be embeded into your addon, either explicitly by calling AceEvent:Embed(MyAddon) or by
Xiiph@0 6 -- specifying it as an embeded library in your AceAddon. All functions will be available on your addon object
Xiiph@0 7 -- and can be accessed directly, without having to explicitly call AceEvent itself.\\
Xiiph@0 8 -- It is recommended to embed AceEvent, otherwise you'll have to specify a custom `self` on all calls you
Xiiph@0 9 -- make into AceEvent.
Xiiph@0 10 -- @class file
Xiiph@0 11 -- @name AceEvent-3.0
Xiiph@0 12 -- @release $Id: AceEvent-3.0.lua 975 2010-10-23 11:26:18Z nevcairiel $
Xiiph@0 13 local MAJOR, MINOR = "AceEvent-3.0", 3
Xiiph@0 14 local AceEvent = LibStub:NewLibrary(MAJOR, MINOR)
Xiiph@0 15
Xiiph@0 16 if not AceEvent then return end
Xiiph@0 17
Xiiph@0 18 -- Lua APIs
Xiiph@0 19 local pairs = pairs
Xiiph@0 20
Xiiph@0 21 local CallbackHandler = LibStub:GetLibrary("CallbackHandler-1.0")
Xiiph@0 22
Xiiph@0 23 AceEvent.frame = AceEvent.frame or CreateFrame("Frame", "AceEvent30Frame") -- our event frame
Xiiph@0 24 AceEvent.embeds = AceEvent.embeds or {} -- what objects embed this lib
Xiiph@0 25
Xiiph@0 26 -- APIs and registry for blizzard events, using CallbackHandler lib
Xiiph@0 27 if not AceEvent.events then
Xiiph@0 28 AceEvent.events = CallbackHandler:New(AceEvent,
Xiiph@0 29 "RegisterEvent", "UnregisterEvent", "UnregisterAllEvents")
Xiiph@0 30 end
Xiiph@0 31
Xiiph@0 32 function AceEvent.events:OnUsed(target, eventname)
Xiiph@0 33 AceEvent.frame:RegisterEvent(eventname)
Xiiph@0 34 end
Xiiph@0 35
Xiiph@0 36 function AceEvent.events:OnUnused(target, eventname)
Xiiph@0 37 AceEvent.frame:UnregisterEvent(eventname)
Xiiph@0 38 end
Xiiph@0 39
Xiiph@0 40
Xiiph@0 41 -- APIs and registry for IPC messages, using CallbackHandler lib
Xiiph@0 42 if not AceEvent.messages then
Xiiph@0 43 AceEvent.messages = CallbackHandler:New(AceEvent,
Xiiph@0 44 "RegisterMessage", "UnregisterMessage", "UnregisterAllMessages"
Xiiph@0 45 )
Xiiph@0 46 AceEvent.SendMessage = AceEvent.messages.Fire
Xiiph@0 47 end
Xiiph@0 48
Xiiph@0 49 --- embedding and embed handling
Xiiph@0 50 local mixins = {
Xiiph@0 51 "RegisterEvent", "UnregisterEvent",
Xiiph@0 52 "RegisterMessage", "UnregisterMessage",
Xiiph@0 53 "SendMessage",
Xiiph@0 54 "UnregisterAllEvents", "UnregisterAllMessages",
Xiiph@0 55 }
Xiiph@0 56
Xiiph@0 57 --- Register for a Blizzard Event.
Xiiph@0 58 -- The callback will be called with the optional `arg` as the first argument (if supplied), and the event name as the second (or first, if no arg was supplied)
Xiiph@0 59 -- Any arguments to the event will be passed on after that.
Xiiph@0 60 -- @name AceEvent:RegisterEvent
Xiiph@0 61 -- @class function
Xiiph@0 62 -- @paramsig event[, callback [, arg]]
Xiiph@0 63 -- @param event The event to register for
Xiiph@0 64 -- @param callback The callback function to call when the event is triggered (funcref or method, defaults to a method with the event name)
Xiiph@0 65 -- @param arg An optional argument to pass to the callback function
Xiiph@0 66
Xiiph@0 67 --- Unregister an event.
Xiiph@0 68 -- @name AceEvent:UnregisterEvent
Xiiph@0 69 -- @class function
Xiiph@0 70 -- @paramsig event
Xiiph@0 71 -- @param event The event to unregister
Xiiph@0 72
Xiiph@0 73 --- Register for a custom AceEvent-internal message.
Xiiph@0 74 -- The callback will be called with the optional `arg` as the first argument (if supplied), and the event name as the second (or first, if no arg was supplied)
Xiiph@0 75 -- Any arguments to the event will be passed on after that.
Xiiph@0 76 -- @name AceEvent:RegisterMessage
Xiiph@0 77 -- @class function
Xiiph@0 78 -- @paramsig message[, callback [, arg]]
Xiiph@0 79 -- @param message The message to register for
Xiiph@0 80 -- @param callback The callback function to call when the message is triggered (funcref or method, defaults to a method with the event name)
Xiiph@0 81 -- @param arg An optional argument to pass to the callback function
Xiiph@0 82
Xiiph@0 83 --- Unregister a message
Xiiph@0 84 -- @name AceEvent:UnregisterMessage
Xiiph@0 85 -- @class function
Xiiph@0 86 -- @paramsig message
Xiiph@0 87 -- @param message The message to unregister
Xiiph@0 88
Xiiph@0 89 --- Send a message over the AceEvent-3.0 internal message system to other addons registered for this message.
Xiiph@0 90 -- @name AceEvent:SendMessage
Xiiph@0 91 -- @class function
Xiiph@0 92 -- @paramsig message, ...
Xiiph@0 93 -- @param message The message to send
Xiiph@0 94 -- @param ... Any arguments to the message
Xiiph@0 95
Xiiph@0 96
Xiiph@0 97 -- Embeds AceEvent into the target object making the functions from the mixins list available on target:..
Xiiph@0 98 -- @param target target object to embed AceEvent in
Xiiph@0 99 function AceEvent:Embed(target)
Xiiph@0 100 for k, v in pairs(mixins) do
Xiiph@0 101 target[v] = self[v]
Xiiph@0 102 end
Xiiph@0 103 self.embeds[target] = true
Xiiph@0 104 return target
Xiiph@0 105 end
Xiiph@0 106
Xiiph@0 107 -- AceEvent:OnEmbedDisable( target )
Xiiph@0 108 -- target (object) - target object that is being disabled
Xiiph@0 109 --
Xiiph@0 110 -- Unregister all events messages etc when the target disables.
Xiiph@0 111 -- this method should be called by the target manually or by an addon framework
Xiiph@0 112 function AceEvent:OnEmbedDisable(target)
Xiiph@0 113 target:UnregisterAllEvents()
Xiiph@0 114 target:UnregisterAllMessages()
Xiiph@0 115 end
Xiiph@0 116
Xiiph@0 117 -- Script to fire blizzard events into the event listeners
Xiiph@0 118 local events = AceEvent.events
Xiiph@0 119 AceEvent.frame:SetScript("OnEvent", function(this, event, ...)
Xiiph@0 120 events:Fire(event, ...)
Xiiph@0 121 end)
Xiiph@0 122
Xiiph@0 123 --- Finally: upgrade our old embeds
Xiiph@0 124 for target, v in pairs(AceEvent.embeds) do
Xiiph@0 125 AceEvent:Embed(target)
Xiiph@0 126 end