migrate to eventbus, add support to macos

This commit is contained in:
aynakeya
2025-10-02 21:57:22 +08:00
parent a81eb4a131
commit 918e2e81b3
49 changed files with 251 additions and 236 deletions

View File

@@ -191,7 +191,7 @@ func (b *bus) SubscribeAny(eventId, handlerName string, fn HandlerFunc) error {
return nil
}
func (b *bus) SubscribeOnce(eventId, handlerName string, fn HandlerFunc) error {
func (b *bus) SubscribeOnce(channel, eventId, handlerName string, fn HandlerFunc) error {
if eventId == "" || handlerName == "" || fn == nil {
return errors.New("invalid SubscribeOnce args")
}
@@ -202,7 +202,7 @@ func (b *bus) SubscribeOnce(eventId, handlerName string, fn HandlerFunc) error {
m = make(map[string]handlerRec)
b.handlers[eventId] = m
}
m[handlerName] = handlerRec{name: handlerName, fn: fn, once: true}
m[handlerName] = handlerRec{channel: channel, name: handlerName, fn: fn, once: true}
return nil
}
@@ -225,6 +225,10 @@ func (b *bus) Publish(eventId string, data interface{}) error {
return b.PublishEvent(&Event{Id: eventId, Data: data})
}
func (b *bus) PublishToChannel(channel string, eventId string, data interface{}) error {
return b.PublishEvent(&Event{Id: eventId, Channel: channel, Data: data})
}
func (b *bus) PublishEvent(ev *Event) error {
if ev == nil || ev.Id == "" {
return errors.New("invalid PublishEvent args")
@@ -327,6 +331,15 @@ func (b *bus) Call(eventId string, data interface{}, subEvtId string) (*Event, e
}
}
func (b *bus) Reply(req *Event, eventId string, data interface{}) error {
return b.PublishEvent(&Event{
Id: eventId,
Channel: req.Channel,
EchoId: req.EchoId,
Data: data,
})
}
func (b *bus) nextEchoId() string {
x := b.idCtr.Add(1)
return fmt.Sprintf("echo-%d-%d", time.Now().UnixNano(), x)