Advertisement
Guest User

EventLib.lua

a guest
Oct 16th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.97 KB | None | 0 0
  1. --[[
  2. Lua-based Event library to allow event emitting with attached callbacks.
  3.  
  4. Usage:
  5.  
  6.   require "events"
  7.  
  8.   -- Create an event.
  9.   Event.create("join")
  10.  
  11.   -- Observe the event with a callback.
  12.   -- The callback function will receive an event object
  13.   -- which will have two parameters:
  14.   --   .type -- a string, which is the type of this event.
  15.   --   .args -- a table with data relative to this event.
  16.   -- There is an optional 3rd argument to observe(), which
  17.   -- if true, will create the event if it does not already
  18.   -- exist.
  19.   function a_callback(event)
  20.     io.write(string.format("I got an event: %s\n", event.type))
  21.     io.write(string.format("With %d arguments.\n", table.getn(event.args)))
  22.   end
  23.  
  24.   Event.observe("join", a_callback)
  25.  
  26.   -- Emit the event!
  27.   -- The second argument to emit() should be a table
  28.   -- of data related to the event. It can be nil, in
  29.   -- which case the created event object's .args
  30.   -- property will be set to {}.
  31.   Event.emit("join", {nick = "foobar", host = "..."})
  32.  
  33.   -- To no longer observe and event, pass in the callback
  34.   -- function once again. Callbacks are matched based on
  35.   -- the function you passed in. Remember, it HAS to be the
  36.   -- same function; passing in anonymous functions won't work,
  37.   -- unless you save the return value from Event.observe(),
  38.   -- since it returns the callback function provided.
  39.   Event.unobserve("join", a_callback)
  40.  
  41.   -- Silence an event if you wanted to prevent callbacks from
  42.   -- being called.
  43.   Event.silence("join")
  44.  
  45.   -- any callbacks won't be called now.
  46.   Event.emit("join", {nick = "foobar", host = "..."})
  47.  
  48.   -- Allow callbacks to happen once more.
  49.   Event.unsilence("join")
  50.  
  51.   -- You can also silence an event for a single call of a
  52.   -- function. The next argument after the event to silence
  53.   -- is the name of the function to call after silencing the
  54.   -- event, and any additional arguments are passed to that
  55.   -- function.
  56.   function func(arg1, arg2)
  57.     ...
  58.   end
  59.  
  60.   Event.silence("join", func, "foo", "bar")
  61.  
  62.   -- Finally, you can remove events as well. This will remove
  63.   -- the event and all of it's callbacks by setting the event
  64.   -- to nil, allowing it to be garbage collected.
  65.   Event.remove("join")
  66. ]]
  67.  
  68. local function getClass(super)
  69.     local obj = {}
  70.     obj.__index = obj
  71.     setmetatable(obj, super)
  72.  
  73.     function obj.new(...)
  74.         if obj._instance then
  75.             return obj._instance
  76.         end
  77.  
  78.         local instance = setmetatable({}, obj)
  79.         if instance.ctor then
  80.             instance:ctor(...)
  81.         end
  82.  
  83.         obj._instance = instance
  84.         return obj._instance
  85.     end
  86.  
  87.     --  The Event object.
  88.     local obj.events   = {}
  89.     local obj.silenced = {}
  90.    
  91.  
  92.     -- Create an event.
  93.     function obj.create(event)
  94.       if not obj.events[event] then
  95.         obj.events[event] = {}
  96.       end
  97.     end
  98.    
  99.     -- Remove an event.
  100.     function obj.remove(event)
  101.       obj.events[event] = nil
  102.     end
  103.  
  104.     -- Check to see if an event is known.
  105.     function obj.has_event(event)
  106.       return obj.events[event] -- will be nil if it doesn't exist.
  107.     end
  108.  
  109.     -- Observe an event. A callback function is required.
  110.     -- If do_create is true, and the event does not exist,
  111.     -- it will be created before storing the callback.
  112.     -- The callback function will be returned if it was
  113.     -- added, else nil is returned.
  114.     function obj.observe(event, callback, do_create)
  115.       if not obj.has_event(event) and do_create then
  116.         obj.create(event)
  117.       end
  118.  
  119.       table.insert(obj.events[event], callback)
  120.  
  121.       return callback
  122.     end
  123.  
  124.     -- No longer observe an event. The callback given to the
  125.     -- original observe() call must be given. It HAS to be
  126.     -- the same function, matching the function's ID, or else
  127.     -- the callback won't be removed. Thus, be careful passing
  128.     -- in anonymous functions as callbacks, unless you save the
  129.     -- return value from Event.observe() to later use to
  130.     -- unobserve.
  131.     function obj.unobserve(event, callback)
  132.       if obj.has_event(event) then
  133.         local tmp = {}
  134.  
  135.         for _, cb in obj.events[event] do
  136.           if not callback == cb then
  137.             table.insert(tmp, cb)
  138.           end
  139.         end
  140.  
  141.         obj.events[event] = tmp
  142.         return true
  143.       end
  144.  
  145.       return false
  146.     end
  147.  
  148.     -- Emit an event. Callback functions are passed an
  149.     -- event object with .type and .args properties, with
  150.     -- .type being set to the type of event, and .args being
  151.     -- set to the passed args value, which should be a table.
  152.     -- If the args argument is nil, then an empty table will
  153.     -- be assigned to it.
  154.     function obj.emit(event, args)
  155.       if obj.has_event(event) then
  156.         if not args then args = {} end
  157.  
  158.         local ev = {type = event, args = args}
  159.  
  160.         for _, callback in pairs(obj.events[event]) do
  161.           callback(event, ev)
  162.         end
  163.       end
  164.     end
  165.  
  166.     -- Silence an event, causing any of it's callback
  167.     -- functions to not be called if the event is emitted.
  168.     -- You can pass a function as the second argument, which  will cause the event to be silenced only for the call
  169.     -- of that function. Any additional arguments after this
  170.     -- function will be passed to that function. After the
  171.     -- function is called, the event will be unsilenced again.
  172.     -- This function will return true if the event is still
  173.     -- silenced at the end of this function, or nil if it
  174.     -- isn't (which will only happen if you pass in a callback
  175.     -- function.)
  176.     function obj.silence(event, ...)
  177.       if not obj.silenced[event] then
  178.         obj.silenced[event] = true
  179.       end
  180.  
  181.       if type(arg[1]) == "function" then
  182.         callback = table.remove(arg, 1)
  183.  
  184.         callback(unpack(arg))
  185.         obj.silenced[event] = nil
  186.       end
  187.  
  188.       return obj.silenced[event]
  189.     end
  190.  
  191.     -- Remove the silencing of an event.
  192.     function obj.unsilence(event)
  193.       obj.silenced[event] = nil
  194.     end
  195.  
  196.     -- Checks to see if an event is silenced.
  197.     function obj.is_silenced(event)
  198.       return obj.silenced[event]
  199.     end
  200.    
  201.  
  202.     return obj
  203. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement