Skip to content

luxe: events


import "luxe: events" for Events

A simple event system for listening to and emitting events.

Note: this API will likely change to ID based soon, where on listening, an ID will be returned, and use that ID to unlisten rather than needing the function object.

Events.new() : Events

Create a new Events instance to use.

var events = Events.new()

Events.once(tags : List, fn : Fn) : String

Connect a function to the given tags, that is automatically removed after the event is emitted. The function takes a single argument, data, which is sent from emit.

events.once(["example"]) {|data|
Log.print("event received: data = `%(data)`")
}
//make the event happen, will call the above function
//which prints event received: data = `321`
events.emit(["example"], 321)
//fire the event again, but this one does NOT print,
//because the event was only listening once
events.emit(["example"], 654)

Events.listen(tags : List, fn : Fn) : String

Connect a function to the given tags. The function will be called each time the event is emitted, until unlisten is called. The function takes a single argument, data, which is sent through emit. Returns an id that you give to unlisten.

var tags = ["example", "tags"]
var fn = Fn.new {|data|
Log.print("data = `%(data)`")
}
var id = events.listen(tags, fn)
events.emit(tags, "hello") //prints data = `hello`
events.emit(tags, { "map":"data" }) //prints data = `{map:data}`
events.emit(tags) //prints data = `null`
events.unlisten_id(tags, id) //remove the function
events.emit(tags) //nothing printed

Events.unlisten(tags : List, fn : Fn) : None

Removes a connected function for the specified tags (if one exists), by specifying the same function passed to listen. See listen for example.

events.unlisten(["tag"], fn)

Events.unlisten_id(tags : List, id : String) : None

Removes a connected function for the specified tags (if one exists). The id is the one returned from listen. See listen for example.

events.unlisten_id(["tag"], id)

Events.unlisten(tags : List) : None

Removes ALL functions from the specified tags, clearing them.

events.unlisten(["tag"])

Events.emit(tags : List) : None

Emit the event tags so that any connected functions will be called. Sends null for the data argument to the functions. See listen for an example.

events.emit(["tag"])

Events.emit(tags : List, data : Any) : None

Emit the event tags so that any connected functions will be called. Sends data as is for the data argument to the functions. See listen for an example.

events.emit(["tag"], ["hello"])