Class: Jekyll::Commands::Serve::LiveReloadReactor

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll/commands/serve/live_reload_reactor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLiveReloadReactor

Returns a new instance of LiveReloadReactor.


15
16
17
18
19
20
# File 'lib/jekyll/commands/serve/live_reload_reactor.rb', line 15

def initialize
  @websockets = []
  @connections_count = 0
  @started_event = Utils::ThreadEvent.new
  @stopped_event = Utils::ThreadEvent.new
end

Instance Attribute Details

#started_eventObject (readonly)

Returns the value of attribute started_event


11
12
13
# File 'lib/jekyll/commands/serve/live_reload_reactor.rb', line 11

def started_event
  @started_event
end

#stopped_eventObject (readonly)

Returns the value of attribute stopped_event


12
13
14
# File 'lib/jekyll/commands/serve/live_reload_reactor.rb', line 12

def stopped_event
  @stopped_event
end

#threadObject (readonly)

Returns the value of attribute thread


13
14
15
# File 'lib/jekyll/commands/serve/live_reload_reactor.rb', line 13

def thread
  @thread
end

Instance Method Details

#handle_websockets_event(websocket) ⇒ Object


33
34
35
36
37
38
# File 'lib/jekyll/commands/serve/live_reload_reactor.rb', line 33

def handle_websockets_event(websocket)
  websocket.onopen { |handshake| connect(websocket, handshake) }
  websocket.onclose { disconnect(websocket) }
  websocket.onmessage { |msg| print_message(msg) }
  websocket.onerror { |error| log_error(error) }
end

#reload(pages) ⇒ Object


69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/jekyll/commands/serve/live_reload_reactor.rb', line 69

def reload(pages)
  pages.each do |p|
    json_message = JSON.dump(
      :command => "reload",
      :path    => p.url,
      :liveCSS => true
    )

    Jekyll.logger.debug "LiveReload:", "Reloading #{p.url}"
    Jekyll.logger.debug "", json_message
    @websockets.each { |ws| ws.send(json_message) }
  end
end

#running?Boolean

Returns:

  • (Boolean)

29
30
31
# File 'lib/jekyll/commands/serve/live_reload_reactor.rb', line 29

def running?
  EM.reactor_running?
end

#start(opts) ⇒ Object


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/jekyll/commands/serve/live_reload_reactor.rb', line 40

def start(opts)
  @thread = Thread.new do
    # Use epoll if the kernel supports it
    EM.epoll
    EM.run do
      EM.error_handler { |e| log_error(e) }

      EM.start_server(
        opts["host"],
        opts["livereload_port"],
        HttpAwareConnection,
        opts
      ) do |ws|
        handle_websockets_event(ws)
      end

      # Notify blocked threads that EventMachine has started or shutdown
      EM.schedule { @started_event.set }
      EM.add_shutdown_hook { @stopped_event.set }

      Jekyll.logger.info "LiveReload address:",
                         "http://#{opts["host"]}:#{opts["livereload_port"]}"
    end
  end
  @thread.abort_on_exception = true
end

#stopObject


22
23
24
25
26
27
# File 'lib/jekyll/commands/serve/live_reload_reactor.rb', line 22

def stop
  # There is only one EventMachine instance per Ruby process so stopping
  # it here will stop the reactor thread we have running.
  EM.stop if EM.reactor_running?
  Jekyll.logger.debug "LiveReload Server:", "halted"
end