Module: Hotwire::Livereload

Defined in:
lib/hotwire/livereload.rb,
lib/hotwire/livereload/engine.rb,
lib/hotwire/livereload/version.rb,
lib/hotwire/livereload/middleware.rb,
lib/hotwire/livereload/cable_server.rb

Defined Under Namespace

Classes: CableServer, Engine, Middleware, ReloadChannel

Constant Summary collapse

DISABLE_FILE =
"tmp/livereload-disabled.txt"
VERSION =
"2.0.0"

Class Method Summary collapse

Class Method Details

.action_cable(opts) ⇒ Object



31
32
33
# File 'lib/hotwire/livereload.rb', line 31

def action_cable(opts)
  cable_server.broadcast("hotwire-reload", opts)
end

.cable_serverObject



27
28
29
# File 'lib/hotwire/livereload.rb', line 27

def cable_server
  @cable_server ||= Hotwire::Livereload::CableServer.new
end

.debounce(wait_ms, &block) ⇒ Object



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

def debounce(wait_ms, &block)
  if wait_ms.zero?
    return ->(*args) { yield(*args) }
  end

  mutex = Mutex.new
  timer_thread = nil
  seconds = wait_ms.to_f / 1000.0

  lambda do |*args|
    mutex.synchronize do
      # Cancel previous timer
      timer_thread&.kill

      timer_thread = Thread.new do
        sleep(seconds)
        yield(*args)
      end
    end
  end
end

.enabled?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/hotwire/livereload.rb', line 42

def enabled?
  Rails.env.development? && server_process?
end

.server_process?Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
# File 'lib/hotwire/livereload.rb', line 35

def server_process?
  puma_process = defined?(::Puma) && File.basename($0) == "puma"
  rails_server = defined?(Rails::Server)

  !!(puma_process || rails_server)
end

.turbo_stream(locals) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/hotwire/livereload.rb', line 18

def turbo_stream(locals)
  Turbo::StreamsChannel.broadcast_replace_to(
    "hotwire-livereload",
    target: "hotwire-livereload",
    partial: "hotwire/livereload/turbo_stream",
    locals: locals
  )
end