Fingercomp-Programs/libaevent at master · OpenPrograms/Fingercomp-Programs · GitHub
Skip to content

Latest commit

 

History

History

Folders and files

README.md

Libaevent

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

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.

Using the library

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")()

Methods

The resulting EvEngine instance

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.

The Event instance

Constructor:

  • Event(data: table[, once: bool]) Returns an instance of the event with the specified payload. The once arguments makes the event be processed by only one subscriber. Generally you'd use Event {test = "test"}.
Method Description
event:cancel() Cancels an event.
event:get() Returns the event's payload.

Event payload can be indexed just like tables, e.g. event.foo.

Sample code

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!