Skip to content

Commit b1dbde8

Browse files
committed
Stop theme watcher logging on every poll tick
Separate stat from parse so unchanged files are detected with two os.Stat calls instead of reading and parsing each tick, which was emitting a log line every 2s.
1 parent f87b00e commit b1dbde8

1 file changed

Lines changed: 20 additions & 20 deletions

File tree

internal/theme/watcher.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -59,37 +59,35 @@ func candidatePaths() []string {
5959
return append(paths, filepath.Join(platform.ThemeDir(), "colors.toml"))
6060
}
6161

62-
// resolveAndParse stats each candidate in order, parses the first that
63-
// reads successfully, and returns its path, mtime, and color map. All
64-
// zero/nil values indicate nothing usable was found.
65-
func (w *ThemeWatcher) resolveAndParse() (string, time.Time, map[string]string) {
62+
// resolveAndStat returns the first candidate path that exists along
63+
// with its mtime. Empty values mean nothing was found.
64+
func (w *ThemeWatcher) resolveAndStat() (string, time.Time) {
6665
for _, p := range w.paths {
67-
info, err := os.Stat(p)
68-
if err != nil {
69-
continue
70-
}
71-
colors := parseColorsTOML(p)
72-
if colors == nil {
73-
continue
66+
if info, err := os.Stat(p); err == nil {
67+
return p, info.ModTime()
7468
}
75-
return p, info.ModTime(), colors
7669
}
77-
return "", time.Time{}, nil
70+
return "", time.Time{}
7871
}
7972

8073
// CurrentColors returns the current parsed color map. Returns nil if no
8174
// colors.toml is readable.
8275
func (w *ThemeWatcher) CurrentColors() map[string]string {
83-
_, _, colors := w.resolveAndParse()
84-
return colors
76+
path, _ := w.resolveAndStat()
77+
if path == "" {
78+
return nil
79+
}
80+
return parseColorsTOML(path)
8581
}
8682

8783
// Start begins polling the file for changes. Call from app.startup.
8884
func (w *ThemeWatcher) Start(ctx context.Context) {
89-
if path, mtime, colors := w.resolveAndParse(); colors != nil {
85+
if path, mtime := w.resolveAndStat(); path != "" {
9086
w.lastPath = path
9187
w.lastMod = mtime
92-
wailsrt.EventsEmit(ctx, "theme-colors-changed", colors)
88+
if colors := parseColorsTOML(path); colors != nil {
89+
wailsrt.EventsEmit(ctx, "theme-colors-changed", colors)
90+
}
9391
}
9492

9593
go w.poll(ctx)
@@ -104,16 +102,18 @@ func (w *ThemeWatcher) poll(ctx context.Context) {
104102
case <-w.stop:
105103
return
106104
case <-ticker.C:
107-
path, mtime, colors := w.resolveAndParse()
108-
if colors == nil {
105+
path, mtime := w.resolveAndStat()
106+
if path == "" {
109107
continue
110108
}
111109
if path == w.lastPath && !mtime.After(w.lastMod) {
112110
continue
113111
}
114112
w.lastPath = path
115113
w.lastMod = mtime
116-
wailsrt.EventsEmit(ctx, "theme-colors-changed", colors)
114+
if colors := parseColorsTOML(path); colors != nil {
115+
wailsrt.EventsEmit(ctx, "theme-colors-changed", colors)
116+
}
117117
}
118118
}
119119
}

0 commit comments

Comments
 (0)