the event emitter
Bait is the event emitter for Hilbish. Much like Node.js and
its events
system, many actions in Hilbish emit events.
Unlike Node.js, Hilbish events are global. So make sure to
pick a unique name!
Usage of the Bait module consists of userstanding
event-driven architecture, but it’s pretty simple:
If you want to act on a certain event, you can catch
it.
You can act on events via callback functions.
Examples of this are in the Hilbish default config! Consider this part of it:
1bait.catch('command.exit', function(code)
2 running = false
3 doPrompt(code ~= 0)
4 doNotifyPrompt()
5end)
What this does is, whenever the command.exit
event is thrown,
this function will set the user prompt.
catch(name, cb) | Catches an event. This function can be used to act on events. |
catchOnce(name, cb) | Catches an event, but only once. This will remove the hook immediately after it runs for the first time. |
hooks(name) -> table | Returns a list of callbacks that are hooked on an event with the corresponding name . |
release(name, catcher) | Removes the catcher for the event with name . |
throw(name, …args) | Throws a hook with name with the provided args . |
Catches an event. This function can be used to act on events.
string
name
The name of the hook.
function
cb
The function that will be called when the hook is thrown.
1bait.catch('hilbish.exit', function()
2 print 'Goodbye Hilbish!'
3end)
Catches an event, but only once. This will remove the hook immediately after it runs for the first time.
string
name
The name of the event
function
cb
The function that will be called when the event is thrown.
Returns a list of callbacks that are hooked on an event with the corresponding name
.
string
name
The name of the function
Removes the catcher
for the event with name
.
For this to work, catcher
has to be the same function used to catch
an event, like one saved to a variable.
string
name
Name of the event the hook is on
function
catcher
Hook function to remove
1local hookCallback = function() print 'hi' end
2
3bait.catch('event', hookCallback)
4
5-- a little while later....
6bait.release('event', hookCallback)
7-- and now hookCallback will no longer be ran for the event.
Want to help improve this page? Create an issue.