Class: Hawkins::LiveReloadReactor

Inherits:
Object
  • Object
show all
Defined in:
lib/hawkins/websockets.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ LiveReloadReactor

Returns a new instance of LiveReloadReactor.



64
65
66
67
68
69
# File 'lib/hawkins/websockets.rb', line 64

def initialize(opts)
  @opts = opts
  @thread = nil
  @websockets = []
  @connections_count = 0
end

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts.



62
63
64
# File 'lib/hawkins/websockets.rb', line 62

def opts
  @opts
end

#threadObject (readonly)

Returns the value of attribute thread.



61
62
63
# File 'lib/hawkins/websockets.rb', line 61

def thread
  @thread
end

Instance Method Details

#running?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/hawkins/websockets.rb', line 76

def running?
  !@thread.nil? && @thread.alive?
end

#startObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/hawkins/websockets.rb', line 80

def start
  @thread = Thread.new do
    # Use epoll if the kernel supports it
    EM.epoll
    # TODO enable SSL
    EM.run do
      Jekyll.logger.info("LiveReload Server:", "#{opts['host']}:#{opts['reload_port']}")
      EM.start_server(opts['host'], opts['reload_port'], HttpAwareConnection, opts) do |ws|
        ws.onopen do |handshake|
          connect(ws, handshake)
        end

        ws.onclose do
          disconnect(ws)
        end

        ws.onmessage do |msg|
          print_message(msg)
        end
      end
    end
  end

  Jekyll::Hooks.register(:site, :post_render) do |site|
    regenerator = Jekyll::Regenerator.new(site)
    @changed_pages = site.pages.select do |p|
      regenerator.regenerate?(p)
    end
  end

  Jekyll::Hooks.register(:site, :post_write) do
    reload(@changed_pages) unless @changed_pages.nil?
    @changed_pages = nil
  end
end

#stopObject



71
72
73
74
# File 'lib/hawkins/websockets.rb', line 71

def stop
  Jekyll.logger.debug("LiveReload Server:", "halted")
  @thread.kill unless @thread.nil?
end