52 lines
1.0 KiB
Go
52 lines
1.0 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
|
|
"github.com/Nwflower/reims-go/internal/control"
|
|
)
|
|
|
|
func (a *App) startPluginControlServer() {
|
|
a.mu.Lock()
|
|
if a.ctx == nil || a.pluginMgr == nil || a.controlServer != nil {
|
|
a.mu.Unlock()
|
|
return
|
|
}
|
|
a.mu.Unlock()
|
|
|
|
server, err := control.Listen(func(ctx context.Context, pluginID, channel string, message json.RawMessage) (json.RawMessage, error) {
|
|
a.mu.Lock()
|
|
mgr := a.pluginMgr
|
|
a.mu.Unlock()
|
|
if mgr == nil {
|
|
return nil, errors.New("plugin manager is not ready")
|
|
}
|
|
return mgr.ChannelMessage(ctx, pluginID, channel, message)
|
|
})
|
|
if err != nil {
|
|
a.emitLog("plugin", "Could not open local plugin control channel: "+err.Error())
|
|
return
|
|
}
|
|
|
|
a.mu.Lock()
|
|
if a.ctx == nil || a.pluginMgr == nil || a.controlServer != nil {
|
|
a.mu.Unlock()
|
|
_ = server.Close()
|
|
return
|
|
}
|
|
a.controlServer = server
|
|
a.mu.Unlock()
|
|
}
|
|
|
|
func (a *App) stopPluginControlServer() {
|
|
a.mu.Lock()
|
|
server := a.controlServer
|
|
a.controlServer = nil
|
|
a.mu.Unlock()
|
|
if server != nil {
|
|
_ = server.Close()
|
|
}
|
|
}
|