wiam/lightsignal
v0.1.3 ·
Lightweight signal implementation
Introduction
lightsignal is a minimal Roblox (and Lune) signal implementation, inspired by GoodSignal, with a twist: there's no connections. Instead, a signal, thanks to metatables, is simply a list of its callbacks.
Installation
Wally
Add
lightsignal = "wiam77/[email protected]"
in your wally.toml, under the [dependencies] table.
Or..
Download the model from the latest release.
Usage
local LightSignal = require("path/to/lightsignal")
local Signal : LightSignal.Signal = LightSignal.new()
-- Connect
-- Returns the index the callback resides at.
local CallbackIndex = Signal:Connect(function(message)
print(message)
end)
-- Once
-- Same thing as Connect.
local CallbackIndex = Signal:Once(function(message)
print(message)
end)
-- Wait
local v = Signal:Wait()
-- Checking if a callback is connected to a signal.
local Connected : boolean = if Signal[CallbackIndex] then true else false
-- Fire
Signal:Fire("Hello, World!")
-- Disconnect
Signal[CallbackIndex] = nil
-- Disconnect all
table.clear(Signal)
GoodSignal performance comparison
| Test | LightSignal (μs) | GoodSignal (μs) |
|---|---|---|
| ConnectAndDisconnect | 0.1 | 0.3 |
| WaitOnEvent | 4.1 | 4.2 |
| FireManyCallbacks | 5.0 | 5.0 |
| FireWithNoConnections | 0.1 | 0.0 |
| CreateAndFire | 1.2 | 1.3 |
| Fire | 0.9 | 0.8 |
| FireYieldingCallback | 3.9 | 3.0 |
| FireManyArguments | 1.0 | 0.9 |
Tests source code.