refactor: use bait event emitter on commander

new-emitter
TorchedSammy 2022-08-02 18:13:17 -04:00
parent c76337e5a4
commit e364897b88
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
4 changed files with 15 additions and 10 deletions

View File

@ -29,8 +29,8 @@ type Bait struct{
rtm *rt.Runtime
}
func New(rtm *rt.Runtime) Bait {
b := Bait{
func New(rtm *rt.Runtime) *Bait {
b := &Bait{
handlers: make(map[string][]*Listener),
rtm: rtm,
}

View File

@ -2,20 +2,20 @@ package commander
import (
"hilbish/util"
"hilbish/golibs/bait"
rt "github.com/arnodel/golua/runtime"
"github.com/arnodel/golua/lib/packagelib"
"github.com/chuckpreslar/emission"
)
type Commander struct{
Events *emission.Emitter
Events *bait.Bait
Loader packagelib.Loader
}
func New() Commander {
func New(rtm *rt.Runtime) Commander {
c := Commander{
Events: emission.NewEmitter(),
Events: bait.New(rtm),
}
c.Loader = packagelib.Loader{
Load: c.loaderFunc,

11
lua.go
View File

@ -32,12 +32,17 @@ func luaInit() {
lib.LoadLibs(l, fs.Loader)
lib.LoadLibs(l, terminal.Loader)
cmds := commander.New()
cmds := commander.New(l)
// When a command from Lua is added, register it for use
cmds.Events.On("commandRegister", func(cmdName string, cmd *rt.Closure) {
cmds.Events.On("commandRegister", func(args ...interface{}) {
cmdName := args[0].(string)
cmd := args[1].(*rt.Closure)
commands[cmdName] = cmd
})
cmds.Events.On("commandDeregister", func(cmdName string) {
cmds.Events.On("commandDeregister", func(args ...interface{}) {
cmdName := args[0].(string)
delete(commands, cmdName)
})
lib.LoadLibs(l, cmds.Loader)

View File

@ -30,7 +30,7 @@ var (
userDataDir string
curuser *user.User
hooks bait.Bait
hooks *bait.Bait
defaultConfPath string
defaultHistPath string
)