refactor(channels): add factory registry and export SetRunning on BaseChannel

This commit is contained in:
Hoshina
2026-02-20 23:18:46 +08:00
parent 8a1fb03974
commit dfcf15bfff
2 changed files with 36 additions and 0 deletions
+4
View File
@@ -101,3 +101,7 @@ func (c *BaseChannel) HandleMessage(senderID, chatID, content string, media []st
func (c *BaseChannel) setRunning(running bool) {
c.running = running
}
func (c *BaseChannel) SetRunning(running bool) {
c.running = running
}
+32
View File
@@ -0,0 +1,32 @@
package channels
import (
"sync"
"github.com/sipeed/picoclaw/pkg/bus"
"github.com/sipeed/picoclaw/pkg/config"
)
// ChannelFactory is a constructor function that creates a Channel from config and message bus.
// Each channel subpackage registers one or more factories via init().
type ChannelFactory func(cfg *config.Config, bus *bus.MessageBus) (Channel, error)
var (
factoriesMu sync.RWMutex
factories = map[string]ChannelFactory{}
)
// RegisterFactory registers a named channel factory. Called from subpackage init() functions.
func RegisterFactory(name string, f ChannelFactory) {
factoriesMu.Lock()
defer factoriesMu.Unlock()
factories[name] = f
}
// getFactory looks up a channel factory by name.
func getFactory(name string) (ChannelFactory, bool) {
factoriesMu.RLock()
defer factoriesMu.RUnlock()
f, ok := factories[name]
return f, ok
}