Class: Wytch::ReloadCoordinator

Inherits:
Object
  • Object
show all
Defined in:
lib/wytch/reload_coordinator.rb

Overview

Coordinates hot reloading of site code and content during development.

The ReloadCoordinator watches the site code and content directories for changes using the Listen gem. When files change, it marks the appropriate component as dirty and reloads it on the next request.

It uses a read-write lock to ensure thread-safe reloading while allowing concurrent reads during page rendering.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeReloadCoordinator

Creates a new ReloadCoordinator and sets up file watchers.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/wytch/reload_coordinator.rb', line 22

def initialize
  @reload_lock = Concurrent::ReadWriteLock.new

  @site_code_dirty = true
  @content_dirty = true

  @start_site_code_listener = Once.new do
    Listen.to(Wytch.site.site_code_path) do
      @site_code_dirty = true
    end.start
  end

  @start_content_listener = Once.new do
    Listen.to(Wytch.site.content_dir) do
      @content_dirty = true
    end.start
  end
end

Instance Attribute Details

#reload_lockConcurrent::ReadWriteLock (readonly)

Returns lock for coordinating reloads.

Returns:

  • (Concurrent::ReadWriteLock)

    lock for coordinating reloads



19
20
21
# File 'lib/wytch/reload_coordinator.rb', line 19

def reload_lock
  @reload_lock
end

Instance Method Details

#reload!void

This method returns an undefined value.

Reloads site code and/or content if changes have been detected.

Starts file listeners on first call. If site code has changed, reloads both site code (via Zeitwerk) and content. If only content has changed, reloads just the content.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/wytch/reload_coordinator.rb', line 48

def reload!
  @start_site_code_listener&.call
  @start_content_listener&.call

  return unless @site_code_dirty || @content_dirty

  reload_lock.with_write_lock do
    if @site_code_dirty
      # Site code changed: reload site code then reload content
      @site_code_dirty = false
      Wytch.site.site_code_loader.reload
      Wytch.site.load_content
    elsif @content_dirty
      # Only content changed: just reload content
      @content_dirty = false
      Wytch.site.load_content
    end
  end
end