Module: Tina4::DevReload

Defined in:
lib/tina4/websocket.rb

Overview

Shared, process-wide manager for the dev-reload channel (/__dev_reload).

Every browser that loads a page in debug mode opens a WebSocket here and keeps it open. POST /__dev/api/reload (issued by the tina4 Rust CLI on a file change) calls Tina4::DevReload.broadcast(...) to push an instant file, mtime message to every connected client — no respawn, no poll. Mirrors Python's single _ws_manager holding /__dev_reload connections by path; in Ruby each route upgrade otherwise spins its own isolated WebSocket engine, so a dedicated shared manager is what makes a broadcast reach all clients.

Class Method Summary collapse

Class Method Details

.add(connection) ⇒ Object

Register an open connection so a later broadcast reaches it.



822
823
824
# File 'lib/tina4/websocket.rb', line 822

def add(connection)
  manager.connections[connection.id] = connection
end

.broadcast(message) ⇒ Object

Push a text frame to every connected dev-reload client. Best-effort: a dead socket or zero clients must never raise into the caller (the /__dev/api/reload endpoint), so failures are swallowed per-connection.



839
840
841
842
843
844
845
# File 'lib/tina4/websocket.rb', line 839

def broadcast(message)
  manager.connections.values.each do |conn|
    conn.send_text(message)
  rescue StandardError
    next
  end
end

.countObject

Number of live dev-reload clients (used by tests / introspection).



832
833
834
# File 'lib/tina4/websocket.rb', line 832

def count
  manager.connections.size
end

.managerObject

The shared WebSocket manager that holds every /__dev_reload connection.



817
818
819
# File 'lib/tina4/websocket.rb', line 817

def manager
  @mutex.synchronize { @manager ||= Tina4::WebSocket.new }
end

.remove(connection) ⇒ Object

Drop a connection on close.



827
828
829
# File 'lib/tina4/websocket.rb', line 827

def remove(connection)
  manager.connections.delete(connection.id)
end