Signal
A Signal is a class that is used to substitute the use of Bindable Events, this implementation tries to handle most of the edge cases that could happen, while being as performant as possible, with additional features for Advanced users such as selectable disconnect mode.
This implementation tries to have similar API to the standard signal module, GoodSignal, with some additional features.
Quick example:
local NewSignal = require(path.to.NewSignal)
-- Create a signal
local Signal = NewSignal.new()
-- Connect a listener
local Connection = Signal:Connect(function(Param)
print(`The Signal Got fired, param: {Param}`)
end)
-- Fire the signal with the default method
Signal:Fire("Hello")
-- Disconnect the listener
Connection:Disconnect()
Functions
New
Creates a new Signal object
local Signal = NewSignal.new()
Get
Gets an existing Signal object with the given Id, or creates a new one if it doesn't exist.
local Signal = NewSignal.new()
Connect
Connects a function to the signal, which will be called everytime the signal is fired.
Signal:Connect(function(Message, Number)
print(Message, Number)
end)
Signal:Fire("Hello", 25)
Once
Connects a function to the signal, which will be called once and disconnected immediatly.
Signal:Once(function(Message, Number)
print(Message, Number)
end)
Signal:Fire("Hello", 25)
Signal:Fire("This message will not get printed", 10)
Wait
This is a yielding function. When called, it will pause the Lua thread that called the function until a result is ready to be returned, without interrupting other scripts. YieldsSignal:Wait() → ...anyYields the current thread until :Fire occurs, and returns the arguments fired from the signal.
task.spawn(function()
print(Signal:Wait()) --> "Test", 89, nil, false
end)
Signal:Fire("Test", 89, nil, false)
FireAsync
Signal:FireAsync(...: any) → ()Fires a Signal object Asynchronously with the arguments passed.
Signal:Connect(function(Text)
print(Text) --> "Something..."
end)
Signal:FireAsync("Something...")
FireDeferred
Signal:FireDeferred(...: any) → ()
Fires a Signal object with the arguments passed with a task.defer.
Doesn't take use of the thread recicling protocol.
Signal:Connect(function(Text)
print(Text) --> "Something..."
end)
Signal:FireDeferred("Something...")
FireSafe
Signal:FireSafe(...: any) → ()Fires a Signal object asynchronously with the arguments passed using a xpcall.
Signal:Connect(function(Text)
print(Text) --> "Something..."
end)
Signal:FireSafe("Something...")
DisconnectAll
Signal:DisconnectAll() → ()Disconnects all current Connections from the Signal.
Signal:Connect(function(Args) end)
Signal:DisconnectAll()
Destroy
Signal:Destroy() → ()Destroys a Signal object, disconnecting all connections and making it unusable.
Signal:Connect(function(Args) end)
Signal:Destroy() -- Destroys the signal and disconnects all connections
Fire
Signal:Fire() → ()
Alias for FireAsync by default.
Can be changed to any of the other fire methods by modiying the DefaultFireMethod variable.
Signal:Connect(function(Text)
print(Text) --> "Something..."
end)
Signal:Fire("Something...") -- Same behaviour as `FireAsync` by default