wiam/lightsignal

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

TestLightSignal (μs)GoodSignal (μs)
ConnectAndDisconnect0.10.3
WaitOnEvent4.14.2
FireManyCallbacks5.05.0
FireWithNoConnections0.10.0
CreateAndFire1.21.3
Fire0.90.8
FireYieldingCallback3.93.0
FireManyArguments1.00.9

Tests source code.