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.



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

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.



833
834
835
836
837
838
839
# File 'lib/tina4/websocket.rb', line 833

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).



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

def count
  manager.connections.size
end

.managerObject

The shared WebSocket manager that holds every /__dev_reload connection.



811
812
813
# File 'lib/tina4/websocket.rb', line 811

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

.remove(connection) ⇒ Object

Drop a connection on close.



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

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