SkillAgentSearch skills...

Signal.lua

Basic event module for Lua 🛰️

Install / Use

/learn @kitsunies/Signal.lua
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

<h1 align=center>signal.lua 🛰️</h1> <h3 align=center>An event module made to attach and emmit singals 📡</h3>

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.

  • event is 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 by signal.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 by signal.new
  • callback - Must be a function, exectuted when the event gets fired
  • connection - 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 by signal.new
  • callback - Must be a function, exectuted when the event gets fired
  • connection - The returned connection

Detaching

connection:detach()

Concludes the usage of the given connection object.

  • connection is 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.

View on GitHub
GitHub Stars8
CategoryDevelopment
Updated2y ago
Forks1

Languages

Lua

Security Score

70/100

Audited on Sep 12, 2023

No findings