2022-12-15 04:00:54 +00:00
|
|
|
---
|
|
|
|
title: Module bait
|
|
|
|
description: the event emitter
|
|
|
|
layout: doc
|
|
|
|
menu:
|
|
|
|
docs:
|
|
|
|
parent: "API"
|
|
|
|
---
|
|
|
|
|
|
|
|
## Introduction
|
2023-09-02 16:57:13 +00:00
|
|
|
|
|
|
|
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:
|
2023-09-02 18:18:40 +00:00
|
|
|
```lua
|
2023-09-02 16:57:13 +00:00
|
|
|
bait.catch('command.exit', function(code)
|
|
|
|
running = false
|
|
|
|
doPrompt(code ~= 0)
|
|
|
|
doNotifyPrompt()
|
|
|
|
end)
|
|
|
|
```
|
|
|
|
|
|
|
|
What this does is, whenever the `command.exit` event is thrown,
|
|
|
|
this function will set the user prompt.
|
2022-12-15 04:00:54 +00:00
|
|
|
|
|
|
|
## Functions
|
2023-09-02 18:18:40 +00:00
|
|
|
|||
|
|
|
|
|----|----|
|
2023-11-30 22:30:54 +00:00
|
|
|
|<a href="#catch">catch(name, cb)</a>|Catches a hook. This function is used to act on hooks/events.|
|
2023-09-02 18:18:40 +00:00
|
|
|
|<a href="#catchOnce">catchOnce(name, cb)</a>|Same as catch, but only runs the `cb` once and then removes the hook|
|
|
|
|
|<a href="#hooks">hooks(name) -> table</a>|Returns a table with hooks (callback functions) on the event with `name`.|
|
|
|
|
|<a href="#release">release(name, catcher)</a>|Removes the `catcher` for the event with `name`.|
|
|
|
|
|<a href="#throw">throw(name, ...args)</a>|Throws a hook with `name` with the provided `args`|
|
|
|
|
|
|
|
|
<hr><div id='catch'>
|
|
|
|
<h4 class='heading'>
|
|
|
|
bait.catch(name, cb)
|
|
|
|
<a href="#catch" class='heading-link'>
|
|
|
|
<i class="fas fa-paperclip"></i>
|
|
|
|
</a>
|
|
|
|
</h4>
|
|
|
|
|
2023-11-30 22:30:54 +00:00
|
|
|
Catches a hook. This function is used to act on hooks/events.
|
|
|
|
|
|
|
|
|
2023-08-26 15:30:51 +00:00
|
|
|
#### Parameters
|
2023-09-02 16:57:13 +00:00
|
|
|
`string` **`name`**
|
2023-11-30 22:30:54 +00:00
|
|
|
The name of the hook.
|
2023-09-02 16:57:13 +00:00
|
|
|
|
2023-09-02 18:18:40 +00:00
|
|
|
`function` **`cb`**
|
2023-11-30 22:30:54 +00:00
|
|
|
The function that will be called when the hook is thrown.
|
2023-09-02 18:18:40 +00:00
|
|
|
|
2023-11-30 22:30:54 +00:00
|
|
|
#### Example
|
|
|
|
```lua
|
|
|
|
bait.catch('hilbish.exit', function()
|
|
|
|
print 'Goodbye Hilbish!'
|
|
|
|
end)
|
|
|
|
````
|
2023-09-02 20:01:57 +00:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<hr><div id='catchOnce'>
|
2023-09-02 18:18:40 +00:00
|
|
|
<h4 class='heading'>
|
|
|
|
bait.catchOnce(name, cb)
|
|
|
|
<a href="#catchOnce" class='heading-link'>
|
|
|
|
<i class="fas fa-paperclip"></i>
|
|
|
|
</a>
|
|
|
|
</h4>
|
2022-12-15 04:00:54 +00:00
|
|
|
|
|
|
|
Same as catch, but only runs the `cb` once and then removes the hook
|
2023-08-26 15:30:51 +00:00
|
|
|
#### Parameters
|
|
|
|
This function has no parameters.
|
2023-09-02 20:01:57 +00:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<hr><div id='hooks'>
|
2023-09-02 18:18:40 +00:00
|
|
|
<h4 class='heading'>
|
|
|
|
bait.hooks(name) -> table
|
|
|
|
<a href="#hooks" class='heading-link'>
|
|
|
|
<i class="fas fa-paperclip"></i>
|
|
|
|
</a>
|
|
|
|
</h4>
|
2022-12-15 04:00:54 +00:00
|
|
|
|
2022-12-21 00:59:55 +00:00
|
|
|
Returns a table with hooks (callback functions) on the event with `name`.
|
2023-08-26 15:30:51 +00:00
|
|
|
#### Parameters
|
|
|
|
This function has no parameters.
|
2023-09-02 20:01:57 +00:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<hr><div id='release'>
|
2023-09-02 18:18:40 +00:00
|
|
|
<h4 class='heading'>
|
|
|
|
bait.release(name, catcher)
|
|
|
|
<a href="#release" class='heading-link'>
|
|
|
|
<i class="fas fa-paperclip"></i>
|
|
|
|
</a>
|
|
|
|
</h4>
|
2022-12-15 04:00:54 +00:00
|
|
|
|
2023-02-07 22:19:24 +00:00
|
|
|
Removes the `catcher` for the event with `name`.
|
2022-12-15 04:00:54 +00:00
|
|
|
For this to work, `catcher` has to be the same function used to catch
|
|
|
|
an event, like one saved to a variable.
|
2023-08-26 15:30:51 +00:00
|
|
|
#### Parameters
|
|
|
|
This function has no parameters.
|
2023-09-02 20:01:57 +00:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<hr><div id='throw'>
|
2023-09-02 18:18:40 +00:00
|
|
|
<h4 class='heading'>
|
|
|
|
bait.throw(name, ...args)
|
|
|
|
<a href="#throw" class='heading-link'>
|
|
|
|
<i class="fas fa-paperclip"></i>
|
|
|
|
</a>
|
|
|
|
</h4>
|
2022-12-15 04:00:54 +00:00
|
|
|
|
|
|
|
Throws a hook with `name` with the provided `args`
|
2023-08-26 15:30:51 +00:00
|
|
|
#### Parameters
|
|
|
|
`string` **`name`**
|
|
|
|
The name of the hook.
|
|
|
|
|
|
|
|
`any` **`args`** (This type is variadic. You can pass an infinite amount of parameters with this type.)
|
|
|
|
The arguments to pass to the hook.
|
|
|
|
|
2023-09-02 20:01:57 +00:00
|
|
|
</div>
|
|
|
|
|