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.
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,
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
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.