¶
luxe API (2025.1.2)¶
luxe: events module¶
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.
- new()
- once(tags:
List, fn:Fn) - listen(tags:
List, fn:Fn) - unlisten(tags:
List, fn:Fn) - unlisten_id(tags:
List, id:String) - unlisten(tags:
List) - emit(tags:
List) - emit(tags:
List, data:Any)
Events
Create a new
Eventsinstance to use.var events = Events.new()
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 fromemit.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)
List, fn: Fn)
¶String
Connect a function to the given tags. The function will be called each time the event is emitted, until
unlistenis called. The function takes a single argument,data, which is sent throughemit. Returns an id that you give tounlisten.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
List, fn: Fn)
¶None
Removes a connected function for the specified tags (if one exists), by specifying the same function passed to
listen. Seelistenfor example.events.unlisten(["tag"], fn)
List, id: String)
¶None
Removes a connected function for the specified tags (if one exists). The id is the one returned from
listen. Seelistenfor example.events.unlisten_id(["tag"], id)
List)
¶None
Removes ALL functions from the specified tags, clearing them.
events.unlisten(["tag"])
List)
¶None
Emit the event tags so that any connected functions will be called. Sends
nullfor the data argument to the functions. Seelistenfor an example.events.emit(["tag"])
List, data: Any)
¶None
Emit the event tags so that any connected functions will be called. Sends
dataas is for the data argument to the functions. Seelistenfor an example.events.emit(["tag"], ["hello"])