Module: Esp::Recents

Defined in:
lib/esp/recents.rb

Overview

Per-user “recently opened projects” list, persisted as JSON in the data directory the Tauri shell injects via ESP_DATA_DIR (macOS: ~/Library/Application Support/com.coreyellis.espresso/). Falls back to ~/.config/esp/ for CLI / unit-test use. The MW_DATA_DIR env var still works as a one-release deprecation alias so existing dev shells keep finding the same on-disk state; remove after step 24 ships. Single-writer per process — the mutex serialises threaded WEBrick handlers; cross-process collisions are accepted in v1 (one ESPresso ↔one backend).

Constant Summary collapse

CAP =
10

Class Method Summary collapse

Class Method Details

.add(root) ⇒ Object

Record an opened project: dedupe by root, prepend, cap. Returns the updated list so callers don’t have to re-read.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/esp/recents.rb', line 37

def add(root)
  normalized = File.expand_path(root.to_s)
  @mutex.synchronize do
    entries = read_unsafe.reject { |entry| entry['root'] == normalized }
    entries.unshift(
      'root' => normalized,
      'name' => File.basename(normalized),
      'opened_at' => Time.now.utc.iso8601
    )
    entries = entries.first(CAP)
    write_unsafe(entries)
    entries
  end
end

.clear!Object

Used by tests to start from a known state.



53
54
55
# File 'lib/esp/recents.rb', line 53

def clear!
  @mutex.synchronize { write_unsafe([]) }
end

.data_dirObject



25
26
27
# File 'lib/esp/recents.rb', line 25

def data_dir
  ENV['ESP_DATA_DIR'] || ENV['MW_DATA_DIR'] || File.expand_path('~/.config/esp')
end

.listObject

The current list, newest-first. Missing or malformed file → [] (we’d rather start clean than block the landing on a parse error).



31
32
33
# File 'lib/esp/recents.rb', line 31

def list
  @mutex.synchronize { read_unsafe }
end

.pathObject



21
22
23
# File 'lib/esp/recents.rb', line 21

def path
  File.join(data_dir, 'recents.json')
end