Guest User

reactor.lua v0.3

a guest
Jun 24th, 2012
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.26 KB | None | 0 0
  1. --[[
  2. kazagistar's event reactor
  3. Do whatever you want with it, no rights reserved, as per WTFPL
  4. (http://sam.zoy.org/wtfpl/COPYING)
  5.  
  6. The point of this program is to provide a centralized observer pattern. Events
  7. can be generated by hardware and software, and any "observer" function which
  8. is registered will be called.
  9.  
  10. reactor:register(event, name, func): Registers a function as an event listener,
  11. given the event string and a unique name string to ID this function. For the
  12. parameters passed to the function, see the documentation of the event sender,
  13. but they are always passed the event and name. If you want to listen for any
  14. event, listen for "reactor.any", but dont overuse this, as it can lead to poor
  15. performance.
  16.  
  17. reactor:unregister(event, name): Unregisters a function from an event, given
  18. the event and name.
  19.  
  20. reactor:event(event, ...): Pushes an event to resolve.
  21.  
  22. reactor:run() Starts the reactor running. Set reactor.running = false to stop
  23. --]]
  24. if not reactor then
  25.  
  26. reactor = {}
  27. reactor.observer = {}
  28. reactor.register = function(r, event, name, func)
  29.     if not event then
  30.         event = "reactor.any"
  31.     end
  32.     if not r.observer[event] then
  33.         r.observer[event] = {}
  34.     end
  35.     r.observer[event][name]=func
  36. end
  37. reactor.unregister = function(r, event, name)
  38.     if event == nil then
  39.         event = "reactor.any"
  40.     end
  41.     r:event("reactor.delete", event, name)
  42. end
  43. reactor.event = function(r, ...)
  44.     os.queueEvent(...)
  45. end
  46. reactor.resolve = function(r, event, ...)
  47.     local args = {...}
  48.     if event == "reactor.delete" then
  49.         local event, name = unpack(args)
  50.         r.observer[event][name] = nil
  51.         if not next(r.observer[event]) then
  52.             r.observer[event] = nil
  53.         end
  54.     end
  55.     if r.observer[event] then
  56.         for name, func in pairs(r.observer[event]) do
  57.             func(event, name, unpack(args))
  58.         end
  59.     end
  60.     if r.observer["reactor.any"] then
  61.         for name, func in pairs(r.observer["reactor.any"]) do
  62.             func(event, name, unpack(args))
  63.         end
  64.     end
  65. end
  66.  
  67.  
  68. -- This utility function is stupidly useful, so I put it here
  69. reactor.capture = function(str, ...) return str, arg end
  70.  
  71. -- Until the reactor.running = false, it will pull events and share em around
  72. reactor.run = function(r)
  73.     r.running = true
  74.     while r.running do
  75.         r:resolve(coroutine.yield())
  76.     end
  77. end
  78.  
  79. end
Advertisement
Add Comment
Please, Sign In to add comment