2021-03-26 05:12:55 +00:00
|
|
|
package bait
|
|
|
|
|
|
|
|
import (
|
2021-12-01 21:29:57 +00:00
|
|
|
"fmt"
|
2021-10-16 16:40:53 +00:00
|
|
|
"hilbish/util"
|
|
|
|
|
2021-03-26 05:12:55 +00:00
|
|
|
"github.com/chuckpreslar/emission"
|
|
|
|
"github.com/yuin/gopher-lua"
|
2021-03-30 23:47:02 +00:00
|
|
|
"layeh.com/gopher-luar"
|
2021-03-26 05:12:55 +00:00
|
|
|
)
|
|
|
|
|
2021-03-30 23:47:02 +00:00
|
|
|
type Bait struct{
|
|
|
|
Em *emission.Emitter
|
|
|
|
}
|
2021-03-26 05:12:55 +00:00
|
|
|
|
|
|
|
func New() Bait {
|
2021-12-01 21:29:57 +00:00
|
|
|
emitter := emission.NewEmitter()
|
2021-12-01 21:35:12 +00:00
|
|
|
emitter.RecoverWith(func(hookname, hookfunc interface{}, err error) {
|
|
|
|
emitter.Off(hookname, hookfunc)
|
2021-12-01 21:29:57 +00:00
|
|
|
fmt.Println(err)
|
|
|
|
})
|
2021-03-30 23:47:02 +00:00
|
|
|
return Bait{
|
2021-12-01 21:29:57 +00:00
|
|
|
Em: emitter,
|
2021-03-30 23:47:02 +00:00
|
|
|
}
|
2021-03-26 05:12:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bait) Loader(L *lua.LState) int {
|
2021-03-30 23:47:02 +00:00
|
|
|
mod := L.SetFuncs(L.NewTable(), map[string]lua.LGFunction{})
|
2021-11-22 23:58:30 +00:00
|
|
|
|
|
|
|
util.Document(L, mod,
|
|
|
|
`Bait is the event emitter for Hilbish. Why name it bait?
|
|
|
|
Because it throws hooks that you can catch (emits events
|
|
|
|
that you can listen to) and because why not, fun naming
|
|
|
|
is fun. This is what you will use if you want to listen
|
|
|
|
in on hooks to know when certain things have happened,
|
|
|
|
like when you've changed directory, a command has
|
|
|
|
failed, etc. To find all available hooks, see doc hooks.`)
|
|
|
|
|
2021-10-16 14:21:05 +00:00
|
|
|
L.SetField(mod, "throw", luar.New(L, b.bthrow))
|
|
|
|
L.SetField(mod, "catch", luar.New(L, b.bcatch))
|
2021-12-04 21:59:19 +00:00
|
|
|
L.SetField(mod, "catchOnce", luar.New(L, b.bcatchOnce))
|
2021-03-26 05:12:55 +00:00
|
|
|
|
|
|
|
L.Push(mod)
|
|
|
|
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2021-10-16 14:21:05 +00:00
|
|
|
// throw(name, ...args)
|
|
|
|
// Throws a hook with `name` with the provided `args`
|
2022-02-25 22:15:25 +00:00
|
|
|
// --- @param name string
|
|
|
|
// --- @vararg any
|
2021-10-16 14:21:05 +00:00
|
|
|
func (b *Bait) bthrow(name string, args ...interface{}) {
|
2021-03-30 23:47:02 +00:00
|
|
|
b.Em.Emit(name, args...)
|
|
|
|
}
|
|
|
|
|
2021-10-16 14:21:05 +00:00
|
|
|
// catch(name, cb)
|
|
|
|
// Catches a hook with `name`. Runs the `cb` when it is thrown
|
2022-02-25 22:15:25 +00:00
|
|
|
// --- @param name string
|
|
|
|
// --- @param cb function
|
2021-10-16 14:21:05 +00:00
|
|
|
func (b *Bait) bcatch(name string, catcher func(...interface{})) {
|
2021-03-30 23:47:02 +00:00
|
|
|
b.Em.On(name, catcher)
|
|
|
|
}
|
2021-12-04 21:59:19 +00:00
|
|
|
|
|
|
|
// catchOnce(name, cb)
|
|
|
|
// Same as catch, but only runs the `cb` once and then removes the hook
|
2022-02-25 22:15:25 +00:00
|
|
|
// --- @param name string
|
|
|
|
// --- @param cb function
|
2021-12-04 21:59:19 +00:00
|
|
|
func (b *Bait) bcatchOnce(name string, catcher func(...interface{})) {
|
|
|
|
b.Em.Once(name, catcher)
|
|
|
|
}
|