Skip to content

Commit

Permalink
website(docs/bait): complete documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
TorchedSammy committed Dec 1, 2023
1 parent f516e1b commit d921d3f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 35 deletions.
46 changes: 35 additions & 11 deletions docs/api/bait.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ this function will set the user prompt.
## Functions
|||
|----|----|
|<a href="#catch">catch(name, cb)</a>|Catches a hook. This function is used to act on hooks/events.|
|<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="#catch">catch(name, cb)</a>|Catches an event. This function can be used to act on events.|
|<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 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="#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'>
<h4 class='heading'>
Expand All @@ -49,7 +49,7 @@ bait.catch(name, cb)
</a>
</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
Expand All @@ -75,9 +75,14 @@ bait.catchOnce(name, cb)
</a>
</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
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>

<hr><div id='hooks'>
Expand All @@ -88,9 +93,11 @@ bait.hooks(name) -> table
</a>
</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
This function has no parameters.
`string` **`name`**
The name of the function

</div>

<hr><div id='release'>
Expand All @@ -104,8 +111,25 @@ bait.release(name, catcher)
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.


#### 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>

<hr><div id='throw'>
Expand All @@ -116,7 +140,7 @@ bait.throw(name, ...args)
</a>
</h4>

Throws a hook with `name` with the provided `args`
Throws a hook with `name` with the provided `args`.
#### Parameters
`string` **`name`**
The name of the hook.
Expand Down
18 changes: 6 additions & 12 deletions emmyLuaDocs/bait.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,25 @@

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

--- Same as catch, but only runs the `cb` once and then removes the hook
--- @param name string
--- @param cb function
--- Catches an event, but only once. This will remove the hook immediately after it runs for the first time.
function bait.catchOnce(name, cb) end

--- Returns a table with hooks (callback functions) on the event with `name`.
--- @param name string
--- @returns table<function>
--- Returns a list of callbacks that are hooked on an event with the corresponding `name`.
function bait.hooks(name) end

--- 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.
--- @param name string
--- @param catcher function
---
---
function bait.release(name, catcher) end

--- Throws a hook with `name` with the provided `args`
--- @param name string
--- @vararg any
--- Throws a hook with `name` with the provided `args`.
function bait.throw(name, ...args) end

return bait
33 changes: 21 additions & 12 deletions golibs/bait/bait.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func handleHook(t *rt.Thread, c *rt.GoCont, name string, catcher *rt.Closure, ar
}

// 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 cb function The function that will be called when the hook is thrown.
/*
Expand All @@ -270,9 +270,9 @@ func (b *Bait) bcatch(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
}

// catchOnce(name, cb)
// Same as catch, but only runs the `cb` once and then removes the hook
// --- @param name string
// --- @param cb function
// Catches an event, but only once. This will remove the hook immediately after it runs for the first time.
// #param name string The name of the event
// #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) {
name, catcher, err := util.HandleStrCallback(t, c)
if err != nil {
Expand All @@ -285,9 +285,9 @@ func (b *Bait) bcatchOnce(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
}

// hooks(name) -> table
// Returns a table with hooks (callback functions) on the event with `name`.
// --- @param name string
// --- @returns table<function>
// Returns a list of callbacks that are hooked on an event with the corresponding `name`.
// #param name string The name of the function
// #returns table<function>
func (b *Bait) bhooks(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
if err := c.Check1Arg(); err != nil {
return nil, err
Expand Down Expand Up @@ -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`.
// For this to work, `catcher` has to be the same function used to catch
// an event, like one saved to a variable.
// --- @param name string
// --- @param catcher function
// #param name string Name of the event the hook is on
// #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) {
name, catcher, err := util.HandleStrCallback(t, c)
if err != nil {
Expand All @@ -336,9 +347,7 @@ func (b *Bait) brelease(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
// throw(name, ...args)
// #param name string The name of the hook.
// #param args ...any The arguments to pass to the hook.
// Throws a hook with `name` with the provided `args`
// --- @param name string
// --- @vararg any
// Throws a hook with `name` with the provided `args`.
func (b *Bait) bthrow(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
if err := c.Check1Arg(); err != nil {
return nil, err
Expand Down

0 comments on commit d921d3f

Please sign in to comment.