Module: Mayu::Resources::HotSwap

Extended by:
T::Sig
Defined in:
lib/mayu/resources/hot_swap.rb,
lib/mayu/resources/hot_swap/file_watcher.rb

Defined Under Namespace

Modules: FileWatcher

Class Method Summary collapse

Class Method Details

.start(registry, &block) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/mayu/resources/hot_swap.rb', line 15

def self.start(registry, &block)
  FileWatcher.watch(registry.root, ["app"]) do |event|
    Console.logger.info(self, "\e[33mSwapping code\e[0m")
    start_at = Time.now.to_f

    visited = T::Set[String].new

    event.modified.each do |path|
      registry.reload_resource(path, visited:)
    end

    event.added.each do |path|
      registry.reload_resource(
        path,
        visited:,
        add: path.start_with?("/app/pages/")
      )
    end

    event.removed.each { |path| registry.unload_resource(path, visited:) }

    Console.logger.info(
      self,
      format("\e[33mSwapped code in %.3fs\e[0m", Time.now.to_f - start_at)
    )

    yield
  end
end