mirror of
https://github.com/aljazceru/opencode.git
synced 2025-12-29 05:34:19 +01:00
chore: refactoring status updates
This commit is contained in:
64
internal/status/manager.go
Normal file
64
internal/status/manager.go
Normal file
@@ -0,0 +1,64 @@
|
||||
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)
|
||||
}
|
||||
|
||||
80
internal/status/status.go
Normal file
80
internal/status/status.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package status
|
||||
|
||||
import (
|
||||
"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
|
||||
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
|
||||
type Service interface {
|
||||
pubsub.Suscriber[StatusMessage]
|
||||
Info(message string)
|
||||
Warn(message string)
|
||||
Error(message string)
|
||||
Debug(message string)
|
||||
}
|
||||
|
||||
type service struct {
|
||||
*pubsub.Broker[StatusMessage]
|
||||
}
|
||||
|
||||
// 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) {
|
||||
statusMsg := StatusMessage{
|
||||
Level: level,
|
||||
Message: message,
|
||||
Timestamp: time.Now(),
|
||||
}
|
||||
s.Publish(pubsub.CreatedEvent, statusMsg)
|
||||
}
|
||||
|
||||
// NewService creates a new status service
|
||||
func NewService() Service {
|
||||
broker := pubsub.NewBroker[StatusMessage]()
|
||||
return &service{
|
||||
Broker: broker,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user