Class: Inesita::LiveReload
- Inherits:
-
Object
- Object
- Inesita::LiveReload
- Defined in:
- lib/inesita/live_reload.rb
Constant Summary collapse
- INJECT_CODE =
Opal.compile(File.read(File.('../../../opal/inesita/live_reload.rb', __FILE__)))
Instance Method Summary collapse
- #call(env) ⇒ Object
- #init_live_reload ⇒ Object
-
#initialize(app, _options = {}) ⇒ LiveReload
constructor
A new instance of LiveReload.
- #inject_script(body) ⇒ Object
Constructor Details
#initialize(app, _options = {}) ⇒ LiveReload
Returns a new instance of LiveReload.
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/inesita/live_reload.rb', line 7 def initialize(app, = {}) @app = app Thread.new do begin init_live_reload rescue => e puts e end end end |
Instance Method Details
#call(env) ⇒ Object
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/inesita/live_reload.rb', line 18 def call(env) status, headers, body = @app.call(env) if status == 200 new_body = inject_script(body) headers['Content-Length'] = new_body.bytesize.to_s [status, headers, [new_body]] else [status, headers, body] end end |
#init_live_reload ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/inesita/live_reload.rb', line 35 def init_live_reload AppFilesListener.instance server = Rubame::Server.new('0.0.0.0', 23654) loop do server.run do |ws| ws.onopen { AppFilesListener.instance.add_ws(ws) } ws.onclose { AppFilesListener.instance.rm_ws(ws) } ws. { |msg| ws.send 'pong' if msg == 'ping' } end end end |
#inject_script(body) ⇒ Object
29 30 31 32 33 |
# File 'lib/inesita/live_reload.rb', line 29 def inject_script(body) new_body = '' body.each { |line| new_body += line.to_s } new_body.gsub('{ Opal.loaded', "{ #{INJECT_CODE} Opal.loaded") end |