Signal
Signal
is a generic utility that allows functions to subscribe to and respond to events.
It is used internally by all other Weo services.
Signal Utility
Constructors
Signal.new()
Function
Signal.new()
- Creates a new Signal instance.
Returns:
Signal<T>
Signal- The created Signal instance.
Generic type
<T>
represents the types of arguments the signal callbacks will receive.
Signal API
(Signal):Connect()
Signal
Signal:Connect(callback)
- Connects a callback to the signal.
Parameters:
callback
function- The function to call when the signal fires (fun(...: T)).
Returns:
table
table- A connection object that can be disconnected.
The callback will be called with the arguments passed to
Signal:Fire
.
The returned connection can be disconnected manually usingconnection:Disconnect()
.
(Signal):Fire()
Method
Signal:Fire(...)
- Fires the signal, invoking all connected callbacks with the provided arguments.
Parameters:
...
T- Variable arguments to pass to all connected callbacks.
Signal:KillAll()
Method
Signal:KillAll()
- Disconnects all connected callbacks and clears the signal.
This removes every connection and should be used cautiously.
Signal:Kill()
Method
Signal:Kill(connection)
- Disconnects a specific connection obtained via Connect.
Parameters:
connection
table- The connection object returned from Signal:Connect().
Connection Object
connection:Disconnect()
Function
- Disconnects the specific callback from the signal.
- Can be called on any connection object returned from
Signal:Connect()
.
Example
-- Create a new signal
local signal = Signal.new()
-- Connect callbacks
local conn1 = signal:Connect(function(msg, value)
print("Callback 1 - Message:", msg, "Value:", value)
end)
local conn2 = signal:Connect(function(msg, value)
print("Callback 2 - Received:", msg)
end)
-- Fire the signal with arguments
signal:Fire("Hello World", 42)
-- Output:
-- Callback 1 - Message: Hello World Value: 42
-- Callback 2 - Received: Hello World
-- Disconnect specific connection
conn1:Disconnect()
signal:Fire("After disconnect", 100)
-- Output:
-- Callback 2 - Received: After disconnect
-- Kill specific connection using Signal:Kill
signal:Kill(conn2)
signal:Fire("Ignored", 0)
-- No output - all connections disconnected
-- Example with multiple signals
local playerJoined = Signal.new()
local playerLeft = Signal.new()
playerJoined:Connect(function(playerName, playerId)
print(playerName .. " joined the game! (ID: " .. playerId .. ")")
end)
playerLeft:Connect(function(playerName)
print(playerName .. " left the game.")
end)
playerJoined:Fire("Alice", 123)
playerLeft:Fire("Alice")