Advertisement
Guest User

Roblox.lua: Pseudo-Event API

a guest
Jan 21st, 2018
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.91 KB | None | 0 0
  1. --[[
  2.     THE GOAL: Recreate the Roblox Event API that is used on their API instances (such as Part.Touched) in order to be applied in custom pseudo-API's (normally through a moduleScript);
  3.     This API attempts to accurately use the same syntax and properties of the original API.
  4. --]]
  5.  
  6. local Event = { -- recreate the RBXScriptSignal instance to be applied on custom gui objects
  7.     new = function()
  8.         local signalAPI = {
  9.             FunctionConnectionQueue = {}, -- internal usage by the module to manage connected functions
  10.             WaitLink = {WaitCount = 0, CanFlush = false, Args = nil}, -- internal usage by the module to manage yielding threads
  11.            
  12.             Connect = function(event, f) -- adds a function to the queue that is fired when the event happens
  13.                 local ScriptConnection = {Connected = true, Listener = f, Disconnect = function(connection)  -- recreates the RBXScriptConnection instance
  14.                     connection.Connected = false
  15.                 end}
  16.                 table.insert(event.FunctionConnectionQueue, ScriptConnection)
  17.                 return ScriptConnection
  18.             end,
  19.            
  20.             Wait = function(event) -- yields the script until the event is fired
  21.                 event.WaitLink.WaitCount = event.WaitLink.WaitCount + 1
  22.                 repeat
  23.                     wait()
  24.                 until event.WaitLink.CanFlush
  25.                
  26.                 event.WaitLink.WaitCount = event.WaitLink.WaitCount - 1
  27.                 return unpack(event.WaitLink.Args)
  28.             end,
  29.            
  30.             Fire = function(event, ...)  -- internal usage by the module that makes the magic happen
  31.                 local packedArgs = {...}
  32.                 for i = #(event.FunctionConnectionQueue), 1 do
  33.                     -- flush all :Wait() calls
  34.                     event.WaitLink.Args = packedArgs
  35.                     event.WaitLink.CanFlush = true
  36.                     coroutine.resume(coroutine.create(function()
  37.                         repeat
  38.                             wait()
  39.                         until event.WaitLink.WaitCount == 0  -- revoke the flush permission when all :Wait() calls have been returned
  40.                        
  41.                         event.WaitLink.CanFlush = false
  42.                     end))
  43.                    
  44.                     -- RBXScriptSignal fires functions in reverse order they were connected, so do I
  45.                     local connection = event.FunctionConnectionQueue[i]
  46.                     if connection.Connected then
  47.                         coroutine.resume(coroutine.create(function()
  48.                             connection.Listener(unpack(packedArgs))
  49.                         end))
  50.                     end
  51.                 end
  52.             end
  53.         }
  54.         return signalAPI
  55.     end
  56. }
  57.  
  58.  
  59. --[[
  60.     USAGE:
  61.     Any dictionary can be deemed as a pseudo-API;
  62.     To attach a word to an event, just set that value as Event.new();
  63.     You can fire events using :Fire(), and connect events through :Connect() and/or :Wait() - last one is untested!
  64.     :Connect() will return a Connection that you can disconnect through :Disconnect()
  65. ]]--
  66.  
  67. -- Sample code:
  68.  
  69. local pseudoApi = {
  70.     Noobify = function(api, noob)
  71.         -- inserts code here
  72.         api.Noobified:Fire(noob)
  73.     end,
  74.     Noobified = Event.new()
  75. }
  76.  
  77. local x
  78. x = pseudoApi.Noobified:Connect(function(nub)
  79.     print("OOF!!", nub)
  80.     x:Disconnect()
  81. end)
  82.  
  83. pseudoApi:Noobify("davness")
  84. pseudoApi:Noobify("theonewhoisreadingthis")
  85.  
  86.  
  87. --[[
  88. OUTPUT:
  89. OOF!! davness
  90. ]]--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement