LianYangCn/signal.lua

Basic event module for Lua ๐Ÿ›ฐ๏ธ

โ˜… 0Forks 0GitHub โ†—Compare

README

signal.lua ๐Ÿ›ฐ๏ธ

An event module made to attach and emmit singals ๐Ÿ“ก

Example

local event = require("signal")()

event:on(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.

Contributors

kitsunies

Issues