website(docs/bait): complete documentation

pull/260/head
sammyette 2023-11-30 22:07:48 -04:00
parent f516e1bb87
commit d921d3f752
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
3 changed files with 62 additions and 35 deletions

View File

@ -35,11 +35,11 @@ this function will set the user prompt.
## Functions ## Functions
||| |||
|----|----| |----|----|
|<a href="#catch">catch(name, cb)</a>|Catches a hook. This function is used to act on hooks/events.| |<a href="#catch">catch(name, cb)</a>|Catches an event. This function can be used to act on events.|
|<a href="#catchOnce">catchOnce(name, cb)</a>|Same as catch, but only runs the `cb` once and then removes the hook| |<a href="#catchOnce">catchOnce(name, cb)</a>|Catches an event, but only once. This will remove the hook immediately after it runs for the first time.|
|<a href="#hooks">hooks(name) -> table</a>|Returns a table with hooks (callback functions) on the event with `name`.| |<a href="#hooks">hooks(name) -> table</a>|Returns a list of callbacks that are hooked on an event with the corresponding `name`.|
|<a href="#release">release(name, catcher)</a>|Removes the `catcher` for 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`| |<a href="#throw">throw(name, ...args)</a>|Throws a hook with `name` with the provided `args`.|
<hr><div id='catch'> <hr><div id='catch'>
<h4 class='heading'> <h4 class='heading'>
@ -49,7 +49,7 @@ bait.catch(name, cb)
</a> </a>
</h4> </h4>
Catches a hook. This function is used to act on hooks/events. Catches an event. This function can be used to act on events.
#### Parameters #### Parameters
@ -75,9 +75,14 @@ bait.catchOnce(name, cb)
</a> </a>
</h4> </h4>
Same as catch, but only runs the `cb` once and then removes the hook Catches an event, but only once. This will remove the hook immediately after it runs for the first time.
#### Parameters #### Parameters
This function has no parameters. `string` **`name`**
The name of the event
`function` **`cb`**
The function that will be called when the event is thrown.
</div> </div>
<hr><div id='hooks'> <hr><div id='hooks'>
@ -88,9 +93,11 @@ bait.hooks(name) -> table
</a> </a>
</h4> </h4>
Returns a table with hooks (callback functions) on the event with `name`. Returns a list of callbacks that are hooked on an event with the corresponding `name`.
#### Parameters #### Parameters
This function has no parameters. `string` **`name`**
The name of the function
</div> </div>
<hr><div id='release'> <hr><div id='release'>
@ -104,8 +111,25 @@ bait.release(name, catcher)
Removes the `catcher` for the event with `name`. Removes the `catcher` for the event with `name`.
For this to work, `catcher` has to be the same function used to catch For this to work, `catcher` has to be the same function used to catch
an event, like one saved to a variable. an event, like one saved to a variable.
#### Parameters #### Parameters
This function has no parameters. `string` **`name`**
Name of the event the hook is on
`function` **`catcher`**
Hook function to remove
#### Example
```lua
local hookCallback = function() print 'hi' end
bait.catch('event', hookCallback)
-- a little while later....
bait.release('event', hookCallback)
-- and now hookCallback will no longer be ran for the event.
````
</div> </div>
<hr><div id='throw'> <hr><div id='throw'>
@ -116,7 +140,7 @@ bait.throw(name, ...args)
</a> </a>
</h4> </h4>
Throws a hook with `name` with the provided `args` Throws a hook with `name` with the provided `args`.
#### Parameters #### Parameters
`string` **`name`** `string` **`name`**
The name of the hook. The name of the hook.

View File

@ -2,31 +2,25 @@
local bait = {} local bait = {}
--- Catches a hook. This function is used to act on hooks/events. --- Catches an event. This function can be used to act on events.
--- ---
--- ---
function bait.catch(name, cb) end function bait.catch(name, cb) end
--- Same as catch, but only runs the `cb` once and then removes the hook --- Catches an event, but only once. This will remove the hook immediately after it runs for the first time.
--- @param name string
--- @param cb function
function bait.catchOnce(name, cb) end function bait.catchOnce(name, cb) end
--- Returns a table with hooks (callback functions) on the event with `name`. --- Returns a list of callbacks that are hooked on an event with the corresponding `name`.
--- @param name string
--- @returns table<function>
function bait.hooks(name) end function bait.hooks(name) end
--- Removes the `catcher` for the event with `name`. --- Removes the `catcher` for the event with `name`.
--- For this to work, `catcher` has to be the same function used to catch --- For this to work, `catcher` has to be the same function used to catch
--- an event, like one saved to a variable. --- an event, like one saved to a variable.
--- @param name string ---
--- @param catcher function ---
function bait.release(name, catcher) end function bait.release(name, catcher) end
--- Throws a hook with `name` with the provided `args` --- Throws a hook with `name` with the provided `args`.
--- @param name string
--- @vararg any
function bait.throw(name, ...args) end function bait.throw(name, ...args) end
return bait return bait

View File

@ -248,7 +248,7 @@ func handleHook(t *rt.Thread, c *rt.GoCont, name string, catcher *rt.Closure, ar
} }
// catch(name, cb) // catch(name, cb)
// Catches a hook. This function is used to act on hooks/events. // Catches an event. This function can be used to act on events.
// #param name string The name of the hook. // #param name string The name of the hook.
// #param cb function The function that will be called when the hook is thrown. // #param cb function The function that will be called when the hook is thrown.
/* /*
@ -270,9 +270,9 @@ func (b *Bait) bcatch(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
} }
// catchOnce(name, cb) // catchOnce(name, cb)
// Same as catch, but only runs the `cb` once and then removes the hook // Catches an event, but only once. This will remove the hook immediately after it runs for the first time.
// --- @param name string // #param name string The name of the event
// --- @param cb function // #param cb function The function that will be called when the event is thrown.
func (b *Bait) bcatchOnce(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { func (b *Bait) bcatchOnce(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
name, catcher, err := util.HandleStrCallback(t, c) name, catcher, err := util.HandleStrCallback(t, c)
if err != nil { if err != nil {
@ -285,9 +285,9 @@ func (b *Bait) bcatchOnce(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
} }
// hooks(name) -> table // hooks(name) -> table
// Returns a table with hooks (callback functions) on the event with `name`. // Returns a list of callbacks that are hooked on an event with the corresponding `name`.
// --- @param name string // #param name string The name of the function
// --- @returns table<function> // #returns table<function>
func (b *Bait) bhooks(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { func (b *Bait) bhooks(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
if err := c.Check1Arg(); err != nil { if err := c.Check1Arg(); err != nil {
return nil, err return nil, err
@ -320,8 +320,19 @@ func (b *Bait) bhooks(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
// Removes the `catcher` for the event with `name`. // Removes the `catcher` for the event with `name`.
// For this to work, `catcher` has to be the same function used to catch // For this to work, `catcher` has to be the same function used to catch
// an event, like one saved to a variable. // an event, like one saved to a variable.
// --- @param name string // #param name string Name of the event the hook is on
// --- @param catcher function // #param catcher function Hook function to remove
/*
#example
local hookCallback = function() print 'hi' end
bait.catch('event', hookCallback)
-- a little while later....
bait.release('event', hookCallback)
-- and now hookCallback will no longer be ran for the event.
#example
*/
func (b *Bait) brelease(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { func (b *Bait) brelease(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
name, catcher, err := util.HandleStrCallback(t, c) name, catcher, err := util.HandleStrCallback(t, c)
if err != nil { if err != nil {
@ -336,9 +347,7 @@ func (b *Bait) brelease(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
// throw(name, ...args) // throw(name, ...args)
// #param name string The name of the hook. // #param name string The name of the hook.
// #param args ...any The arguments to pass to the hook. // #param args ...any The arguments to pass to the hook.
// Throws a hook with `name` with the provided `args` // Throws a hook with `name` with the provided `args`.
// --- @param name string
// --- @vararg any
func (b *Bait) bthrow(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { func (b *Bait) bthrow(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
if err := c.Check1Arg(); err != nil { if err := c.Check1Arg(); err != nil {
return nil, err return nil, err