diff options
| author | adamdottv <[email protected]> | 2025-05-12 08:43:34 -0500 |
|---|---|---|
| committer | adamdottv <[email protected]> | 2025-05-12 08:43:34 -0500 |
| commit | ed9fba99c9e230094ed5d468c88f81469d60c911 (patch) | |
| tree | 0cebc5f7610e91c58c0289d3aa12d1f4c57eab8e /internal/status | |
| parent | f1007771997bd0401516eda87a7e0ac92f269680 (diff) | |
| download | opencode-ed9fba99c9e230094ed5d468c88f81469d60c911.tar.gz opencode-ed9fba99c9e230094ed5d468c88f81469d60c911.zip | |
wip: refactoring
Diffstat (limited to 'internal/status')
| -rw-r--r-- | internal/status/manager.go | 64 | ||||
| -rw-r--r-- | internal/status/status.go | 82 |
2 files changed, 57 insertions, 89 deletions
diff --git a/internal/status/manager.go b/internal/status/manager.go deleted file mode 100644 index 307316386..000000000 --- a/internal/status/manager.go +++ /dev/null @@ -1,64 +0,0 @@ -package status - -import ( - "log/slog" - "sync" -) - -// Manager handles status message management -type Manager struct { - service Service - mu sync.RWMutex -} - -// Global instance of the status manager -var globalManager *Manager - -// InitManager initializes the global status manager with the provided service -func InitManager(service Service) { - globalManager = &Manager{ - service: service, - } - - // Subscribe to status events for any global handling if needed - // go func() { - // ctx := context.Background() - // _ = service.Subscribe(ctx) - // }() - - slog.Debug("Status manager initialized") -} - -// GetService returns the status service from the global manager -func GetService() Service { - if globalManager == nil { - slog.Warn("Status manager not initialized, initializing with default service") - InitManager(NewService()) - } - - globalManager.mu.RLock() - defer globalManager.mu.RUnlock() - - return globalManager.service -} - -// Info publishes an info level status message using the global manager -func Info(message string) { - GetService().Info(message) -} - -// Warn publishes a warning level status message using the global manager -func Warn(message string) { - GetService().Warn(message) -} - -// Error publishes an error level status message using the global manager -func Error(message string) { - GetService().Error(message) -} - -// Debug publishes a debug level status message using the global manager -func Debug(message string) { - GetService().Debug(message) -} - diff --git a/internal/status/status.go b/internal/status/status.go index 5a6064c5b..9c63c8957 100644 --- a/internal/status/status.go +++ b/internal/status/status.go @@ -1,35 +1,36 @@ package status import ( + "context" + "fmt" + "sync" "time" "github.com/opencode-ai/opencode/internal/pubsub" ) -// Level represents the severity level of a status message type Level string const ( - // LevelInfo represents an informational status message - LevelInfo Level = "info" - // LevelWarn represents a warning status message - LevelWarn Level = "warn" - // LevelError represents an error status message + LevelInfo Level = "info" + LevelWarn Level = "warn" LevelError Level = "error" - // LevelDebug represents a debug status message LevelDebug Level = "debug" ) -// StatusMessage represents a status update to be displayed in the UI type StatusMessage struct { Level Level `json:"level"` Message string `json:"message"` Timestamp time.Time `json:"timestamp"` } -// Service defines the interface for the status service +const ( + EventStatusPublished pubsub.EventType = "status_published" +) + type Service interface { - pubsub.Suscriber[StatusMessage] + pubsub.Subscriber[StatusMessage] + Info(message string) Warn(message string) Error(message string) @@ -37,44 +38,75 @@ type Service interface { } type service struct { - *pubsub.Broker[StatusMessage] + broker *pubsub.Broker[StatusMessage] + mu sync.RWMutex +} + +var globalStatusService *service + +func InitService() error { + if globalStatusService != nil { + return fmt.Errorf("status service already initialized") + } + broker := pubsub.NewBroker[StatusMessage]() + globalStatusService = &service{ + broker: broker, + } + return nil +} + +func GetService() Service { + if globalStatusService == nil { + panic("status service not initialized. Call status.InitService() at application startup.") + } + return globalStatusService } -// Info publishes an info level status message func (s *service) Info(message string) { s.publish(LevelInfo, message) } -// Warn publishes a warning level status message func (s *service) Warn(message string) { s.publish(LevelWarn, message) } -// Error publishes an error level status message func (s *service) Error(message string) { s.publish(LevelError, message) } -// Debug publishes a debug level status message func (s *service) Debug(message string) { s.publish(LevelDebug, message) } -// publish creates and publishes a status message with the given level and message -func (s *service) publish(level Level, message string) { +func (s *service) publish(level Level, messageText string) { statusMsg := StatusMessage{ Level: level, - Message: message, + Message: messageText, Timestamp: time.Now(), } - s.Publish(pubsub.CreatedEvent, statusMsg) + s.broker.Publish(EventStatusPublished, statusMsg) } -// NewService creates a new status service -func NewService() Service { - broker := pubsub.NewBroker[StatusMessage]() - return &service{ - Broker: broker, - } +func (s *service) Subscribe(ctx context.Context) <-chan pubsub.Event[StatusMessage] { + return s.broker.Subscribe(ctx) +} + +func Info(message string) { + GetService().Info(message) +} + +func Warn(message string) { + GetService().Warn(message) +} + +func Error(message string) { + GetService().Error(message) } +func Debug(message string) { + GetService().Debug(message) +} + +func Subscribe(ctx context.Context) <-chan pubsub.Event[StatusMessage] { + return GetService().Subscribe(ctx) +} |
