Sha256: ff93d730e0d39c53ad908d6011a9fe885daca7c3ca22988160378c45537589e2
Contents?: true
Size: 1.14 KB
Versions: 2
Compression:
Stored size: 1.14 KB
Contents
module Inesita class LiveReload INJECT_CODE = Opal.compile(File.read(File.expand_path('../../../opal/inesita/live_reload.rb', __FILE__))) def initialize(app, _options = {}) @app = app Thread.new do begin init_live_reload rescue => e puts e end end end 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 def inject_script(body) new_body = '' body.each { |line| new_body += line.to_s } new_body.gsub('{ Opal.loaded', "{ #{INJECT_CODE} Opal.loaded") end 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.onmessage { |msg| ws.send 'pong' if msg == 'ping' } end end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
inesita-livereload-0.1.1 | lib/inesita/live_reload.rb |
inesita-livereload-0.1.0 | lib/inesita/live_reload.rb |