2021-03-26 05:12:55 +00:00
|
|
|
package bait
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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-03-30 23:47:02 +00:00
|
|
|
return Bait{
|
|
|
|
Em: emission.NewEmitter(),
|
|
|
|
}
|
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-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-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`
|
|
|
|
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
|
|
|
|
func (b *Bait) bcatch(name string, catcher func(...interface{})) {
|
2021-03-30 23:47:02 +00:00
|
|
|
b.Em.On(name, catcher)
|
|
|
|
}
|