Created
June 30, 2011 01:21
-
-
Save mexitek/1055436 to your computer and use it in GitHub Desktop.
Lua SubPub Utility (or PubSub, whatevs)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- | |
-- subpub.lua | |
-- | |
-- Created by Arlo Carreon on 2011-06-29. | |
-- MIT License: 2011 Corillo Apps. | |
-- | |
-- SUBPUB.lua : A hub for application events. Class can publish an event, | |
-- all methods subscribed to said event will be executed. | |
-- | |
-- Try out the test below!! | |
-- | |
-- | |
-- ================================================== | |
-- = Quick SubPub Test: paste this in your main.lua = | |
-- ================================================== | |
--local subpub = require( "PATH_TO_UTILS/subpub" ); | |
--subpub.subscribe("test",function(show_text) native.showAlert( "test", show_text ) end); | |
--subpub.publish("test","Woohoo! SubPub works!!"); | |
-- ================================================== | |
-- ** make file into a package ** -- | |
module( ..., package.seeall ); | |
-- ** Local Vars ** -- | |
local event_queues = {}; | |
-- =================================================================================== | |
-- = subscribe() : Method takes an event_name and a function that wants to subscribe = | |
-- =================================================================================== | |
function subscribe( event_name, method_to_subscribe ) | |
-- ** Need to authenticate params here ** -- | |
-- ** Check for event queue ** -- | |
if not event_queues[event_name] then | |
event_queues[event_name] = {}; | |
end | |
-- ** Add method to it's queue ** -- | |
table.insert( event_queues[event_name], method_to_subscribe ); | |
end | |
-- ===================================================================================== | |
-- = Publish() : Method takes an event_name and will call all methods in event's queue = | |
-- ===================================================================================== | |
function publish( event_name, info ) | |
-- ** cache event_queue ** -- | |
local q = event_queues[event_name]; | |
-- ** Check to make sure event exists ** -- | |
if not q then | |
return nil; | |
end | |
-- ** Iterate through the event_queue and call all methods ** -- | |
for i=1,table.getn(q) do | |
q[i](info); | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment