An advanced event library
Available for downloading on the Hel Repository.
The library was written by @LeshaInc. I only packaged it here so it's now available to be downloaded with OPPM, and wrote the documentation.
Advantages over the standard event library:
- It features the thing I really needed: pushing event with payload of any types, including functions and tables.
- Priorities system.
- The event system is local, which means you can have multiple event engines running simulateously, and the events won't interfere with other engines.
- Events are classes.
- Events are cancellable, cancelling one makes it not to go any further.
The library returns an Engine class, which has to be initialized. Generally you'll need to do something like this (note the parentheses at the end):
local engine = require("aevent")()| Method | Description |
|---|---|
engine:event(name: string) |
Registers an event with a name name. It may be then called to get an instance of the event. |
engine:push(event: table(Event)) |
Pushes the event instance. |
engine:subscribe(name: string, priority: number, handler: function) |
Assigns a callable (function or table with defined __call) to an event with a specific priority. |
engine:stdEvent(eventName: string, event: table(Event)) |
Registers a new listener that will listen for the given event, and will fire instance of the given event. |
engine:timer(interval: number, event: table(Event)[, times: number]) |
Creates a new timer that will push the given event on every tick. |
engine:__gc() |
Unregisters all listeners for the computer signals. Call this if the __gc metamethod is disabled. |
Constructor:
Event(data: table[, once: bool])Returns an instance of the event with the specified payload. Theoncearguments makes the event be processed by only one subscriber. Generally you'd useEvent {test = "test"}.
Event payload can be indexed just like tables, e.g. event.foo.
local EvtEngine = require("aevent")()
local Event1 = EvtEngine:event("event1")
EvtEngine:subscribe("event1", 0, function(evt)
print("0 callback!", evt.test) -- If the event is indexed, and the index is none of
-- standard keys (e.g., cancel), it'll look for the
-- value in the event's payload.
evt:cancel()
end)
EvtEngine:subscribe("event1", 1, function(evt)
print("1 callback!", evt.test)
end)
EvtEngine:push(Event1{
test = "Hello!"
})
--> 0 callback! Hello!