change validation to be submission callback based

This commit is contained in:
Robin Olsen
2026-03-04 22:43:28 +01:00
parent 5a9a8238d9
commit 3d183d7fcd
4 changed files with 27 additions and 34 deletions

View File

@@ -17,6 +17,8 @@ type App struct {
CurrentFeedback *ValidationFeedback CurrentFeedback *ValidationFeedback
ActionLogger func(string) ActionLogger func(string)
SubmitChan chan<- struct{}
} }
func (a *App) LogAction(action string) { func (a *App) LogAction(action string) {

View File

@@ -4,7 +4,6 @@ import (
"context" "context"
"fmt" "fmt"
"log/slog" "log/slog"
"time"
"github.com/LazyBachelor/LazyPM/internal/models" "github.com/LazyBachelor/LazyPM/internal/models"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
@@ -69,13 +68,19 @@ func (r *TaskRunner) Run(ctx context.Context, t Tasker, i Interface, iType Inter
// Validation // Validation
feedbackChan := make(chan ValidationFeedback, 10) feedbackChan := make(chan ValidationFeedback, 10)
quitChan := make(chan bool, 1) quitChan := make(chan bool, 1)
submitChan := make(chan struct{}, 1)
if validated, ok := i.(ValidatedInterface); ok { if validated, ok := i.(ValidatedInterface); ok {
validated.SetChannels(feedbackChan, quitChan) validated.SetChannels(feedbackChan, quitChan)
validated.SetSubmitChan(submitChan)
}
if r.app != nil {
r.app.SubmitChan = submitChan
} }
engine := &ValidationEngine{task: t} engine := &ValidationEngine{task: t}
doneChan, stopChan := engine.Start(ctx, func(feedback ValidationFeedback) { doneChan, stopChan := engine.Start(ctx, submitChan, func(feedback ValidationFeedback) {
collector.recordValidation(feedback) collector.recordValidation(feedback)
if feedback.Success { if feedback.Success {
@@ -84,6 +89,10 @@ func (r *TaskRunner) Run(ctx context.Context, t Tasker, i Interface, iType Inter
feedback.Message = "Task not completed!" feedback.Message = "Task not completed!"
} }
if r.app != nil {
r.app.CurrentFeedback = &feedback
}
select { select {
case feedbackChan <- feedback: case feedbackChan <- feedback:
default: default:
@@ -195,30 +204,3 @@ func runQuestionnaire(t Tasker, iType InterfaceType, collector *taskRunCollector
return nil return nil
} }
func startValidationLoop(ctx context.Context, t Tasker, feedbackChan chan ValidationFeedback, doneChan chan bool, quitChan chan bool) {
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
feedback := t.Validate(ctx)
if feedback.Success {
if feedback.Message == "" {
feedback.Message = "Task completed successfully! Going back to the survey menu..."
}
feedbackChan <- feedback
time.Sleep(4 * time.Second)
doneChan <- true
return
}
feedback.Message = "Task not completed!"
feedbackChan <- feedback
case <-quitChan:
return
case <-ctx.Done():
return
}
}
}

View File

@@ -9,17 +9,14 @@ type ValidationEngine struct {
task Tasker task Tasker
} }
func (v *ValidationEngine) Start(ctx context.Context, onFeedback func(ValidationFeedback)) (done <-chan struct{}, stop chan<- struct{}) { func (v *ValidationEngine) Start(ctx context.Context, submitChan <-chan struct{}, onFeedback func(ValidationFeedback)) (done <-chan struct{}, stop chan<- struct{}) {
doneChan := make(chan struct{}, 1) doneChan := make(chan struct{}, 1)
stopChan := make(chan struct{}, 1) stopChan := make(chan struct{}, 1)
go func() { go func() {
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for { for {
select { select {
case <-ticker.C: case <-submitChan:
feedback := v.task.Validate(ctx) feedback := v.task.Validate(ctx)
if onFeedback != nil { if onFeedback != nil {

View File

@@ -13,12 +13,24 @@ import (
type ValidationFeedback = models.ValidationFeedback type ValidationFeedback = models.ValidationFeedback
var taskFeedback ValidationFeedback var taskFeedback ValidationFeedback
var submitChan chan<- struct{}
func SetTaskFeedback(feedback ValidationFeedback) { func SetTaskFeedback(feedback ValidationFeedback) {
taskFeedback = feedback taskFeedback = feedback
} }
func SetSubmitChan(ch chan<- struct{}) {
submitChan = ch
}
func HandleTaskStatus(w http.ResponseWriter, r *http.Request) { func HandleTaskStatus(w http.ResponseWriter, r *http.Request) {
if submitChan != nil {
select {
case submitChan <- struct{}{}:
default:
}
}
hx := HTMX(r) hx := HTMX(r)
if hx.IsHxRequest() { if hx.IsHxRequest() {
hx.WriteString(`<a hx-get="/status/modal" hx-target="#modal-container" hx-swap="innerHTML">` + taskFeedback.Message + `</a>`) hx.WriteString(`<a hx-get="/status/modal" hx-target="#modal-container" hx-swap="innerHTML">` + taskFeedback.Message + `</a>`)