annotate Libs/AceEvent-3.0/AceEvent-3.0.lua @ 17:3000eccbf1a0 v7.3.0.017

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