Advertisement
Necr0

Simple Event Handler

Aug 7th, 2013
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.42 KB | None | 0 0
  1. --[[   
  2.     #SimpleEventHandler 1.0.0.2
  3.     Created in 2013 by Necro
  4.     Licensed under CC0 1.0 Universal(CC0 1.0) (read below)
  5. ]]--
  6.  
  7. --[[
  8.     CC0 1.0 Universal (CC0 1.0) - Public Domain Dedication:
  9.     The person who associated a work with this deed has dedicated the work to the public domain
  10.     by waiving all of his or her rights to the work worldwide under copyright law, including all
  11.     related and neighboring rights, to the extent allowed by law.
  12.  
  13.     You can copy, modify, distribute and perform the work, even for commercial purposes,
  14.     all without asking permission. See Other Information below.
  15. ]]--
  16.  
  17. SimpleEventHandler=SimpleEventHandler or {}
  18. SimpleEventHandler.listenerTable=SimpleEventHandler.listenerTable or {}
  19.  
  20. function SimpleEventHandler.addEvent(e)
  21.     if not SimpleEventHandler.listenerTable[e] then
  22.         SimpleEventHandler.listenerTable[e]={}
  23.         print("SEH:Added event "..tostring(e))
  24.         return true
  25.     else
  26.         error("SEH exception:attempt to override an existing event")
  27.     end
  28. end
  29.  
  30. function SimpleEventHandler.removeEvent(e)
  31.     if SimpleEventHandler.listenerTable[e] then
  32.         SimpleEventHandler.listenerTable[e]=nil
  33.         print("SEH:Removed event "..tostring(e))
  34.         return true
  35.     else
  36.         error("SEH exception:attempt to remove a not existing event")
  37.     end
  38. end
  39.  
  40. --[[
  41.     To be called by a script.
  42. ]]--
  43.  
  44. function SimpleEventHandler.addListener(e,f)
  45.     if not SimpleEventHandler.listenerTable[e] then
  46.         error("SEH exception:attempt to add listener to a not existing event")
  47.     end
  48.     f = (type(f)=="string" and _G[f]) or f
  49.     table.insert(SimpleEventHandler.listenerTable[e],f)
  50.     print("SEH:Added listener "..tostring(f).." to "..tostring(e))
  51.     return true
  52. end
  53.  
  54. function SimpleEventHandler.callEvent(e,...)
  55.     if not SimpleEventHandler.listenerTable[e] then
  56.         error("SEH exception:attempt to call a not existing event")
  57.     end
  58.     local ret=nil
  59.     for i,j in pairs(SimpleEventHandler.listenerTable[e]) do
  60.         local temp=j(...)
  61.         if ret==nil then
  62.             ret=temp
  63.         end
  64.     end
  65.     --print("SEH:Called event "..tostring(e))
  66.     return ret
  67. end
  68.  
  69. function SimpleEventHandler.removeListener(e,f)
  70.     local ret=false
  71.     for i,j in pairs(SimpleEventHandler.listenerTable[e]) do
  72.         if j==f then
  73.             table.remove(SimpleEventHandler.listenerTable[e],i)
  74.             print("SEH:Removed listener "..tostring(f).." from "..tostring(e))
  75.             ret=true
  76.         end
  77.     end
  78.     if not ret then
  79.         error("SEH exception:attempt to remove listener from not existing event")
  80.     end
  81.     return ret
  82. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement