Module: localevent

This module allows to send or listen for local events.
The difference with Events is that they're not sent to the server or other clients and only meant to be consumed locally.
A local event is sent with a name and any number of arguments, like so:

LocalEvent:Send("some_name", "foo", "bar", 123)

Corresponding listeners can be declared like this:

local l = LocalEvent:Listener("some_name", function(a, b, c)
	-- prints "foo", "bar", 123 when calling
	-- `LocalEvent:Send("some_name", "foo", "bar", 123)`
	print(a, b, c)
end)

-- Listeners created first receive events first by default.
-- A listener can return `true` to capture the event
-- and prevent next listeners from receiving it too.
local l = LocalEvent:Listener("some_name", function(a, b, c)
	print(a, b, c)
	return true
end)

-- An optional config table can be provided when creating a listener
-- For now the only supported config field is `topPriority`.
-- Setting `config.topPriority` allows to be become the first receiving
-- listener even if not created first.
local l = LocalEvent:Listener("some_name", function(a, b, c)
	print(a, b, c) -- print happens before other "some_name" listener callbacks
end, { topPriority = true })

localevent

Functions

Listen ( localevent self, string name, function callback, table config optional ) listener

Listen returns a new listener for the provided event name.
The listener will trigger the provided callback when the event is sent.

listeners created first receive events first by default.

The optional config parameter allows to request top priority delivery through config.topPriority.

local localevent = require("localevent")
local l = localevent:Listen("some_name", function(a, b, c)
	print(a, b, c)
)
localevent:Send("some_name", "foo", "bar", 123)
-- prints "foo bar 123"
Send ( localevent self, string name, any ... ) boolean

Sends an event with provided name and any number of arguments.
It returns a boolean, true if the event has been captured by one of the listeners.

local localevent = require("localevent")
localevent:Send("some_name", "foo", "bar", 123)

local captured = localevent:Send("some_other_name", math.random(1,10))
print("event has been captured:", captured == true and "YES" or "NO")

listener

Functions

Pause ( listener self )

Pauses the listener.

local localevent = require("localevent")
local listener = localevent:Listen(LocalEvent.Name.Tick, function(dt)
	-- execute something in loop
end)
listener:Pause()
-- callback will not be triggered again until listener:Resume()
Remove ( listener self )

Removes the listener.

local localevent = require("localevent")
local listener = localevent:Listen(LocalEvent.Name.Tick, function(dt)
	-- execute something in loop
end)
listener:Remove() -- callback will never be called again after this
Resume ( listener self )

Makes the listener listen for events again if it was paused.

local localevent = require("localevent")
local listener = localevent:Listen(LocalEvent.Name.Tick, function(dt)
	-- execute something in loop
end)
listener:Pause()
-- callback not be triggered when events are sent
listener:Resume()
-- callback triggered as soon as an new event is sent
📃 Source