annotate Libs/AceEvent-3.0/AceEvent-3.0.lua @ 0:169f5211fc7f

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