Signal.lua
Basic event module for Lua 🛰️
Install / Use
/learn @kitsunies/Signal.luaREADME
Example
local event = require("signal")()
event:attach(function()
print("I've been fired! 🚀")
end)
event:fire()
--> "I've been fired! 🚀"
Installation
Luarocks
If you are using luarocks, just run:
luarocks install signalise
Manual
Copy the signal.lua file somewhere where your Lua interpreter will be able to find it and require it accordingly:
local signal = require("signal")
Interface
Creation
local event = signal.new()
or
local event = signal()
Creates and returns the event. It must be captured in a variable a in order for the events to take place.
eventis the object that must be used to perform the events - see the "Event methods" section below.
Firing
event:fire([...])
Triggers the event, allowing for connections to run once until the event is fired again.
event- An event returned bysignal.new...- The optional arguments to be used for the connections
Attaching
local connection = event:attach(callback)
Executes the given callback function whenever the connected event gets fired with the event.fire method.
event- An event returned bysignal.newcallback- Must be a function, exectuted when the event gets firedconnection- The returned connection
Attach Once
local connection = event:once(callback)
Works almost identically to event.fire, but only fires once before self detaching.
event- An event returned bysignal.newcallback- Must be a function, exectuted when the event gets firedconnection- The returned connection
Detaching
connection:detach()
Concludes the usage of the given connection object.
connectionis the object that is fundamental for executing callbacks
Testing
Install busted & luacheck luarocks install busted && luarocks install luacheck and run:
$ busted
$ luacheck signal.lua
License
This library is free software; you can redistribute and/or modify it under the terms of the MIT license. See LICENSE for details.
