Advertisement
Guest User

Untitled

a guest
Nov 17th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.45 KB | None | 0 0
  1. ------------------------------------------------------------------------------------------------------------------------
  2. ------------------------------------------------------------------------------------------------------------------------
  3. ------------------------------------------------------------------------------------------------------------------------
  4. ------------------------------------------------Signal class begin------------------------------------------------------
  5. ------------------------------------------------------------------------------------------------------------------------
  6. ------------------------------------------------------------------------------------------------------------------------
  7. ------------------------------------------------------------------------------------------------------------------------
  8. --[[
  9. A 'Signal' object identical to the internal RBXScriptSignal object in it's public API and semantics. This function
  10. can be used to create "custom events" for user-made code.
  11. API:
  12. Method :connect( function handler )
  13.     Arguments:   The function to connect to.
  14.     Returns:     A new connection object which can be used to disconnect the connection
  15.     Description: Connects this signal to the function specified by |handler|. That is, when |fire( ... )| is called for
  16.                  the signal the |handler| will be called with the arguments given to |fire( ... )|. Note, the functions
  17.                  connected to a signal are called in NO PARTICULAR ORDER, so connecting one function after another does
  18.                  NOT mean that the first will be called before the second as a result of a call to |fire|.
  19.  
  20. Method :disconnect()
  21.     Arguments:   None
  22.     Returns:     None
  23.     Description: Disconnects all of the functions connected to this signal.
  24.  
  25. Method :fire( ... )
  26.     Arguments:   Any arguments are accepted
  27.     Returns:     None
  28.     Description: Calls all of the currently connected functions with the given arguments.
  29.  
  30. Method :wait()
  31.     Arguments:   None
  32.     Returns:     The arguments given to fire
  33.     Description: This call blocks until
  34. ]]
  35.  
  36. function t.CreateSignal()
  37.     local this = {}
  38.  
  39.     local mBindableEvent = Instance.new('BindableEvent')
  40.     local mAllCns = {} --all connection objects returned by mBindableEvent::connect
  41.  
  42.     --main functions
  43.     function this:connect(func)
  44.         if self ~= this then error("connect must be called with `:`, not `.`", 2) end
  45.         if type(func) ~= 'function' then
  46.             error("Argument #1 of connect must be a function, got a "..type(func), 2)
  47.         end
  48.         local cn = mBindableEvent.Event:Connect(func)
  49.         mAllCns[cn] = true
  50.         local pubCn = {}
  51.         function pubCn:disconnect()
  52.             cn:Disconnect()
  53.             mAllCns[cn] = nil
  54.         end
  55.         pubCn.Disconnect = pubCn.disconnect
  56.        
  57.         return pubCn
  58.     end
  59.    
  60.     function this:disconnect()
  61.         if self ~= this then error("disconnect must be called with `:`, not `.`", 2) end
  62.         for cn, _ in pairs(mAllCns) do
  63.             cn:Disconnect()
  64.             mAllCns[cn] = nil
  65.         end
  66.     end
  67.    
  68.     function this:wait()
  69.         if self ~= this then error("wait must be called with `:`, not `.`", 2) end
  70.         return mBindableEvent.Event:Wait()
  71.     end
  72.    
  73.     function this:fire(...)
  74.         if self ~= this then error("fire must be called with `:`, not `.`", 2) end
  75.         mBindableEvent:Fire(...)
  76.     end
  77.    
  78.     this.Connect = this.connect
  79.     this.Disconnect = this.disconnect
  80.     this.Wait = this.wait
  81.     this.Fire = this.fire
  82.  
  83.     return this
  84. end
  85.  
  86. ------------------------------------------------- Sigal class End ------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement