class Middleman::LiveReload::Reactor

Attributes

app[R]
thread[R]
web_sockets[R]

Public Class Methods

create(options, app) click to toggle source
# File lib/middleman-livereload/reactor.rb, line 10
def self.create(options, app)
  if @reactor
    @reactor.app = app
  else
    @reactor = new(options, app)
  end

  @reactor
end
new(options, app) click to toggle source
# File lib/middleman-livereload/reactor.rb, line 20
def initialize(options, app)
  @app = app
  @web_sockets = []
  @options     = options
  @thread      = start_threaded_reactor(options)
  @mutex       = Thread::Mutex.new
end

Public Instance Methods

app=(app) click to toggle source
# File lib/middleman-livereload/reactor.rb, line 28
def app= app
  @mutex.synchronize { @app = app }
end
logger() click to toggle source
# File lib/middleman-livereload/reactor.rb, line 32
def logger
  @mutex.synchronize { @app.logger }
end
reload_browser(paths = []) click to toggle source
# File lib/middleman-livereload/reactor.rb, line 40
def reload_browser(paths = [])
  paths = Array(paths)
  logger.info "== LiveReloading path: #{paths.join(' ')}"
  paths.each do |path|
    data = JSON.dump(['refresh', {
      :path           => path,
      :apply_js_live  => @options[:apply_js_live],
      :apply_css_live => @options[:apply_css_live]
    }])

    @web_sockets.each { |ws| ws.send(data) }
  end
end
start_threaded_reactor(options) click to toggle source
# File lib/middleman-livereload/reactor.rb, line 54
def start_threaded_reactor(options)
  wss = Wss.new(@options[:wss_certificate], @options[:wss_private_key])
  Thread.new do
    EventMachine.run do
      logger.info "== LiveReload accepting connections from #{wss.scheme}://#{options[:host]}:#{options[:port]}"
      EventMachine.start_server(options[:host], options[:port], EventMachine::WebSocket::Connection, wss.to_options) do |ws|
        ws.onopen do
          begin
            ws.send "!!ver:1.6"
            @web_sockets << ws
            logger.debug "== LiveReload browser connected"
          rescue
            logger.error $!
            logger.error $!.backtrace
          end
        end

        ws.onmessage do |msg|
          logger.debug "LiveReload Browser URL: #{msg}"
        end

        ws.onclose do
          @web_sockets.delete ws
          logger.debug "== LiveReload browser disconnected"
        end
      end
    end
  end
end
stop() click to toggle source
# File lib/middleman-livereload/reactor.rb, line 36
def stop
  thread.kill
end