Class: LivereloadRails::Middleware
- Inherits:
-
Object
- Object
- LivereloadRails::Middleware
- Defined in:
- lib/livereload_rails/middleware.rb
Constant Summary collapse
- ASYNC_RESPONSE =
[-1, {}, []]
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, assets:, matchers: LivereloadRails.matchers) ⇒ Middleware
constructor
A new instance of Middleware.
Constructor Details
#initialize(app, assets:, matchers: LivereloadRails.matchers) ⇒ Middleware
Returns a new instance of Middleware.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/livereload_rails/middleware.rb', line 9 def initialize(app, assets:, matchers: LivereloadRails.matchers) @app = app @clients = Set.new @clients.extend(MonitorMixin) assets.configure do |environment| @watcher = Watcher.new(LivereloadRails.paths.call(environment.paths), matchers: matchers) do |file| client_path = "#{assets.prefix}/#{file}" clients = @clients.synchronize { @clients.dup } LivereloadRails.logger.debug "Reloading #{clients.size} clients with #{client_path}." clients.each { |client| client.reload(client_path) } end @watcher_thread = Thread.new do Thread.current.abort_on_exception = true @watcher.run end end end |
Instance Method Details
#call(env) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/livereload_rails/middleware.rb', line 30 def call(env) if env["PATH_INFO"] == "/livereload" websocket = LivereloadRails::WebSocket.from_rack(env) do |ws| client = LivereloadRails::Client.new(ws) ws.on(:open) do @clients.synchronize { @clients.add(client) } LivereloadRails.logger.debug "#{client} joined: #{@clients.size}." end ws.on(:close) do @clients.synchronize { @clients.delete(client) } LivereloadRails.logger.debug "#{client} left: #{@clients.size}." end end if websocket ASYNC_RESPONSE else @app.call(env) end else @app.call(env) end end |