Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ node_modules
build
.env
dist
example-config
example-config
.fallow
27 changes: 20 additions & 7 deletions apps/finicky/src/config/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,28 @@ import (
"fmt"
"log/slog"
"os"
"sync"

"github.com/dop251/goja"
)

type VM struct {
// mu serializes all use of runtime, which is not safe for concurrent use.
// The VM is shared by the URL event loop, the REST API's handlers and the
// update checker, so every access must go through Run or a method that
// takes mu.
mu sync.Mutex
runtime *goja.Runtime
namespace string
isJSConfig bool
}

// ConfigOptions holds the values of all runtime config options.
type ConfigOptions struct {
KeepRunning bool
HideIcon bool
LogRequests bool
CheckForUpdates bool
KeepRunning bool `json:"keepRunning"`
HideIcon bool `json:"hideIcon"`
LogRequests bool `json:"logRequests"`
CheckForUpdates bool `json:"checkForUpdates"`
}

// ConfigState represents the current state of the configuration
Expand Down Expand Up @@ -119,6 +125,8 @@ func (vm *VM) setup(apiContent []byte, content []byte) error {
}

func (vm *VM) GetConfigState() *ConfigState {
vm.mu.Lock()
defer vm.mu.Unlock()
state, err := vm.runtime.RunString("finickyConfigAPI.getConfigState(finalConfig)")
if err != nil {
slog.Error("Failed to get config state", "error", err)
Expand Down Expand Up @@ -162,6 +170,8 @@ func (vm *VM) GetAllConfigOptions() ConfigOptions {
if vm == nil || vm.runtime == nil {
return defaults
}
vm.mu.Lock()
defer vm.mu.Unlock()
script := `({
keepRunning: finickyConfigAPI.getOption('keepRunning', finalConfig, true),
hideIcon: finickyConfigAPI.getOption('hideIcon', finalConfig, false),
Expand All @@ -182,7 +192,10 @@ func (vm *VM) GetAllConfigOptions() ConfigOptions {
}
}

// Runtime returns the underlying goja.Runtime
func (vm *VM) Runtime() *goja.Runtime {
return vm.runtime
// Run calls fn with exclusive access to the underlying goja.Runtime. fn must
// not keep the runtime or call other VM methods that take the lock.
func (vm *VM) Run(fn func(*goja.Runtime) error) error {
vm.mu.Lock()
defer vm.mu.Unlock()
return fn(vm.runtime)
}
10 changes: 4 additions & 6 deletions apps/finicky/src/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@ var file *os.File
type windowWriter struct{}

func (w *windowWriter) Write(p []byte) (n int, err error) {
// Remove trailing newline if present
msg := string(p)
if len(msg) > 0 && msg[len(msg)-1] == '\n' {
msg = msg[:len(msg)-1]
payload := p
if len(payload) > 0 && payload[len(payload)-1] == '\n' {
payload = payload[:len(payload)-1]
}

window.SendMessageToWebView("log", msg)
window.BroadcastSSERaw("log", payload)
return len(p), nil
}

Expand Down
Loading
Loading