Extract Grammaratur, Dregarnuhr, Geduldh, Mestionora, Ordoschnellic, Schutzaria, Kunstzeal and Seheweit behind compatible application contracts. Include accumulated editor resilience, navigation and theme UX, remote CLI, storage/schema and DR groundwork, tests and design/operations documentation. Verified: uncached full Go suite; go vet ./...; race tests for all eight components; 11 targeted Chromium tests; Node suites; 19 Python CLI tests; reproducible editor build. No push, release tag or deployment in this checkpoint.
105 lines
2.2 KiB
Go
105 lines
2.2 KiB
Go
package dregarnuhr
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
type Trigger struct {
|
|
Name string
|
|
After, Every time.Duration
|
|
Run func(context.Context, time.Time) error
|
|
}
|
|
type Loop struct {
|
|
Clock Clock
|
|
Every time.Duration
|
|
Wake <-chan struct{}
|
|
OnWake func(context.Context, time.Time) error
|
|
Triggers []Trigger
|
|
Step func(context.Context, time.Time) error
|
|
Report func(string, error)
|
|
}
|
|
|
|
// Run coalesces overdue ticks (no catch-up storm), stops its timer on
|
|
// cancellation, and invokes caller-supplied hooks. It knows no event types.
|
|
func (l Loop) Run(ctx context.Context) error {
|
|
if l.Every <= 0 || l.Step == nil {
|
|
return errors.New("loop requires positive cadence and step")
|
|
}
|
|
clock := l.Clock
|
|
if clock == nil {
|
|
clock = SystemClock{}
|
|
}
|
|
now := clock.Now()
|
|
next := now.Add(l.Every)
|
|
due := make([]time.Time, len(l.Triggers))
|
|
seen := map[string]bool{}
|
|
for i, t := range l.Triggers {
|
|
if t.Name == "" || seen[t.Name] || t.Run == nil || t.After < 0 || t.Every < 0 || (t.After == 0 && t.Every == 0) {
|
|
return fmt.Errorf("invalid trigger %q", t.Name)
|
|
}
|
|
seen[t.Name] = true
|
|
delay := t.After
|
|
if delay == 0 {
|
|
delay = t.Every
|
|
}
|
|
due[i] = now.Add(delay)
|
|
}
|
|
wake := l.Wake
|
|
call := func(name string, fn func(context.Context, time.Time) error, now time.Time) {
|
|
if fn != nil {
|
|
if err := fn(ctx, now); err != nil && l.Report != nil {
|
|
l.Report(name, err)
|
|
}
|
|
}
|
|
}
|
|
for ctx.Err() == nil {
|
|
at := next
|
|
for _, d := range due {
|
|
if !d.IsZero() && d.Before(at) {
|
|
at = d
|
|
}
|
|
}
|
|
timer := clock.NewTimer(max(time.Duration(0), at.Sub(clock.Now())))
|
|
woken := false
|
|
select {
|
|
case <-ctx.Done():
|
|
timer.Stop()
|
|
return ctx.Err()
|
|
case _, ok := <-wake:
|
|
if !ok {
|
|
wake = nil
|
|
timer.Stop()
|
|
continue
|
|
}
|
|
woken = true
|
|
case <-timer.C():
|
|
}
|
|
timer.Stop()
|
|
if ctx.Err() != nil {
|
|
return ctx.Err()
|
|
}
|
|
now = clock.Now()
|
|
if woken {
|
|
call("wake", l.OnWake, now)
|
|
}
|
|
for i, t := range l.Triggers {
|
|
if !due[i].IsZero() && !due[i].After(now) {
|
|
call(t.Name, t.Run, now)
|
|
if t.Every > 0 {
|
|
due[i] = now.Add(t.Every)
|
|
} else {
|
|
due[i] = time.Time{}
|
|
}
|
|
}
|
|
}
|
|
call("step", l.Step, now)
|
|
if !next.After(now) {
|
|
next = now.Add(l.Every)
|
|
}
|
|
}
|
|
return ctx.Err()
|
|
}
|