Class: Middleman::LiveReload::Reactor

Inherits:
Object
  • Object
show all
Defined in:
lib/middleman-livereload/reactor.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, app) ⇒ Reactor

Returns a new instance of Reactor.



20
21
22
23
24
25
26
# 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

Instance Attribute Details

#appObject

Returns the value of attribute app.



8
9
10
# File 'lib/middleman-livereload/reactor.rb', line 8

def app
  @app
end

#threadObject (readonly)

Returns the value of attribute thread.



8
9
10
# File 'lib/middleman-livereload/reactor.rb', line 8

def thread
  @thread
end

#web_socketsObject (readonly)

Returns the value of attribute web_sockets.



8
9
10
# File 'lib/middleman-livereload/reactor.rb', line 8

def web_sockets
  @web_sockets
end

Class Method Details

.create(options, app) ⇒ Object



10
11
12
13
14
15
16
17
18
# 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

Instance Method Details

#loggerObject



32
33
34
# File 'lib/middleman-livereload/reactor.rb', line 32

def logger
  @mutex.synchronize { @app.logger }
end

#reload_browser(paths = []) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# 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) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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

#stopObject



36
37
38
# File 'lib/middleman-livereload/reactor.rb', line 36

def stop
  thread.kill
end