Some checks failed
Security Scan / security (push) Failing after 25s
- Import paths: forge.lthn.ai/core/go → dappco.re/go/core
- Import paths: forge.lthn.ai/core/go-log → dappco.re/go/core/log
- Import paths: forge.lthn.ai/core/go-io → dappco.re/go/core/io
- RegisterTask → c.Action("name", handler) across all 15 services
- QueryHandler signature: (any, bool, error) → core.Result
- PERFORM(task) → Action.Run(ctx, opts)
- QUERY returns single core.Result (not 3 values)
- All 17 packages build and test clean on v0.8.0-alpha.1
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
103 lines
3 KiB
Go
103 lines
3 KiB
Go
// pkg/events/service.go
|
|
package events
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
|
|
coreerr "dappco.re/go/core/log"
|
|
core "dappco.re/go/core"
|
|
)
|
|
|
|
// Options holds configuration for the events service (currently empty).
|
|
type Options struct{}
|
|
|
|
// Service bridges Wails custom events into Core IPC.
|
|
// Emit/On/Off/OnMultiple/Reset are available as Tasks; QueryListeners reads state.
|
|
type Service struct {
|
|
*core.ServiceRuntime[Options]
|
|
platform Platform
|
|
|
|
mu sync.Mutex
|
|
listeners map[string][]func() // IPC-registered cancels per event name
|
|
counts map[string]int // listener counts per event name
|
|
}
|
|
|
|
// OnStartup registers query and action handlers.
|
|
func (s *Service) OnStartup(_ context.Context) core.Result {
|
|
s.Core().RegisterQuery(s.handleQuery)
|
|
s.Core().Action("events.emit", func(_ context.Context, opts core.Options) core.Result {
|
|
t, _ := opts.Get("task").Value.(TaskEmit)
|
|
cancelled := s.platform.Emit(t.Name, t.Data)
|
|
return core.Result{Value: cancelled, OK: true}
|
|
})
|
|
s.Core().Action("events.on", func(ctx context.Context, opts core.Options) core.Result {
|
|
t, _ := opts.Get("task").Value.(TaskOn)
|
|
if t.Name == "" {
|
|
return core.Result{Value: coreerr.E("events.on", "event name must not be empty", nil), OK: false}
|
|
}
|
|
cancel := s.platform.On(t.Name, func(event *CustomEvent) {
|
|
_ = s.Core().ACTION(ActionEventFired{Event: *event})
|
|
})
|
|
s.mu.Lock()
|
|
s.listeners[t.Name] = append(s.listeners[t.Name], cancel)
|
|
s.counts[t.Name]++
|
|
s.mu.Unlock()
|
|
return core.Result{OK: true}
|
|
})
|
|
s.Core().Action("events.off", func(_ context.Context, opts core.Options) core.Result {
|
|
t, _ := opts.Get("task").Value.(TaskOff)
|
|
s.platform.Off(t.Name)
|
|
s.mu.Lock()
|
|
for _, cancel := range s.listeners[t.Name] {
|
|
cancel()
|
|
}
|
|
delete(s.listeners, t.Name)
|
|
delete(s.counts, t.Name)
|
|
s.mu.Unlock()
|
|
return core.Result{OK: true}
|
|
})
|
|
return core.Result{OK: true}
|
|
}
|
|
|
|
// OnShutdown cancels all IPC-registered platform listeners.
|
|
func (s *Service) OnShutdown(_ context.Context) core.Result {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
for _, cancels := range s.listeners {
|
|
for _, cancel := range cancels {
|
|
cancel()
|
|
}
|
|
}
|
|
s.listeners = make(map[string][]func())
|
|
s.counts = make(map[string]int)
|
|
return core.Result{OK: true}
|
|
}
|
|
|
|
// HandleIPCEvents satisfies the core.Service interface (no-op for now).
|
|
func (s *Service) HandleIPCEvents(_ *core.Core, _ core.Message) core.Result {
|
|
return core.Result{OK: true}
|
|
}
|
|
|
|
func (s *Service) handleQuery(_ *core.Core, q core.Query) core.Result {
|
|
switch q.(type) {
|
|
case QueryListeners:
|
|
return core.Result{Value: s.listenerSnapshot(), OK: true}
|
|
default:
|
|
return core.Result{}
|
|
}
|
|
}
|
|
|
|
// listenerSnapshot returns a sorted slice of ListenerInfo for all known event names.
|
|
//
|
|
// snapshot := s.listenerSnapshot()
|
|
// for _, info := range snapshot { log(info.EventName, info.Count) }
|
|
func (s *Service) listenerSnapshot() []ListenerInfo {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
snapshot := make([]ListenerInfo, 0, len(s.counts))
|
|
for name, count := range s.counts {
|
|
snapshot = append(snapshot, ListenerInfo{EventName: name, Count: count})
|
|
}
|
|
return snapshot
|
|
}
|