Class: Middleman::LiveReload::Reactor
- Inherits:
-
Object
- Object
- Middleman::LiveReload::Reactor
- Defined in:
- lib/middleman-livereload/reactor.rb
Instance Attribute Summary collapse
-
#app ⇒ Object
Returns the value of attribute app.
-
#thread ⇒ Object
readonly
Returns the value of attribute thread.
-
#web_sockets ⇒ Object
readonly
Returns the value of attribute web_sockets.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(options, app) ⇒ Reactor
constructor
A new instance of Reactor.
- #logger ⇒ Object
- #reload_browser(paths = []) ⇒ Object
- #start_threaded_reactor(options) ⇒ Object
- #stop ⇒ Object
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(, app) @app = app @web_sockets = [] @options = @thread = start_threaded_reactor() @mutex = Thread::Mutex.new end |
Instance Attribute Details
#app ⇒ Object
Returns the value of attribute app.
8 9 10 |
# File 'lib/middleman-livereload/reactor.rb', line 8 def app @app end |
#thread ⇒ Object (readonly)
Returns the value of attribute thread.
8 9 10 |
# File 'lib/middleman-livereload/reactor.rb', line 8 def thread @thread end |
#web_sockets ⇒ Object (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(, app) if @reactor @reactor.app = app else @reactor = new(, app) end @reactor end |
Instance Method Details
#logger ⇒ Object
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() wss = Wss.new(@options[:wss_certificate], @options[:wss_private_key]) Thread.new do EventMachine.run do logger.info "== LiveReload accepting connections from #{wss.scheme}://#{[:host]}:#{[:port]}" EventMachine.start_server([:host], [:port], EventMachine::WebSocket::Connection, wss.) 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. 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 ⇒ Object
36 37 38 |
# File 'lib/middleman-livereload/reactor.rb', line 36 def stop thread.kill end |