Skip to content

Instantly share code, notes, and snippets.

@mexitek
Created June 30, 2011 01:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mexitek/1055436 to your computer and use it in GitHub Desktop.
Save mexitek/1055436 to your computer and use it in GitHub Desktop.
Lua SubPub Utility (or PubSub, whatevs)
--
-- 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