rails_live_reload 0.1.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +6 -19
- data/lib/rails_live_reload/checker.rb +9 -3
- data/lib/rails_live_reload/config.rb +10 -6
- data/lib/rails_live_reload/engine.rb +16 -7
- data/lib/rails_live_reload/javascript/websocket.js +6 -0
- data/lib/rails_live_reload/middleware/base.rb +55 -0
- data/lib/rails_live_reload/server/base.rb +72 -0
- data/lib/rails_live_reload/server/connections.rb +27 -0
- data/lib/rails_live_reload/version.rb +1 -1
- data/lib/rails_live_reload/watcher.rb +39 -3
- data/lib/rails_live_reload/web_socket/base.rb +116 -0
- data/lib/rails_live_reload/web_socket/client_socket.rb +153 -0
- data/lib/rails_live_reload/web_socket/event_loop.rb +131 -0
- data/lib/rails_live_reload/web_socket/message_buffer.rb +53 -0
- data/lib/rails_live_reload/web_socket/stream.rb +109 -0
- data/lib/rails_live_reload/web_socket/wrapper.rb +27 -0
- data/lib/rails_live_reload.rb +21 -9
- metadata +17 -39
- data/lib/rails_live_reload/client.rb +0 -75
- data/lib/rails_live_reload/rails/middleware/base.rb +0 -63
- data/lib/rails_live_reload/rails/middleware/long_polling.rb +0 -51
- data/lib/rails_live_reload/rails/middleware/polling.rb +0 -21
@@ -1,63 +0,0 @@
|
|
1
|
-
module RailsLiveReload
|
2
|
-
module Rails
|
3
|
-
module Middleware
|
4
|
-
class Base
|
5
|
-
def initialize(app)
|
6
|
-
@app = app
|
7
|
-
end
|
8
|
-
|
9
|
-
def call(env)
|
10
|
-
dup.call!(env)
|
11
|
-
end
|
12
|
-
|
13
|
-
def call!(env)
|
14
|
-
request = Rack::Request.new(env)
|
15
|
-
|
16
|
-
if env["REQUEST_PATH"] == RailsLiveReload.config.url
|
17
|
-
rails_live_response(request)
|
18
|
-
else
|
19
|
-
@status, @headers, @response = @app.call(env)
|
20
|
-
|
21
|
-
if html? && @response.respond_to?(:[]) && (@status == 500 || (@status.to_s =~ /20./ && request.get?))
|
22
|
-
new_response = make_new_response(@response[0])
|
23
|
-
@headers['Content-Length'] = new_response.bytesize.to_s
|
24
|
-
@response = [new_response]
|
25
|
-
end
|
26
|
-
|
27
|
-
[@status, @headers, @response]
|
28
|
-
end
|
29
|
-
rescue Exception => ex
|
30
|
-
puts ex.message
|
31
|
-
puts ex.backtrace.take(10)
|
32
|
-
raise ex
|
33
|
-
end
|
34
|
-
|
35
|
-
private
|
36
|
-
|
37
|
-
def rails_live_response(request)
|
38
|
-
raise NotImplementedError
|
39
|
-
end
|
40
|
-
|
41
|
-
def client_javascript(request)
|
42
|
-
raise NotImplementedError
|
43
|
-
end
|
44
|
-
|
45
|
-
def make_new_response(body)
|
46
|
-
body.sub("</head>", "</head>#{client_javascript}")
|
47
|
-
end
|
48
|
-
|
49
|
-
def html?
|
50
|
-
@headers["Content-Type"].to_s.include?("text/html")
|
51
|
-
end
|
52
|
-
|
53
|
-
def new_thread
|
54
|
-
Thread.new {
|
55
|
-
t2 = Thread.current
|
56
|
-
t2.abort_on_exception = true
|
57
|
-
yield
|
58
|
-
}
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
@@ -1,51 +0,0 @@
|
|
1
|
-
module RailsLiveReload
|
2
|
-
module Rails
|
3
|
-
module Middleware
|
4
|
-
class LongPolling < Base
|
5
|
-
private
|
6
|
-
|
7
|
-
def rails_live_response(request)
|
8
|
-
params = request.params
|
9
|
-
body = lambda do |stream|
|
10
|
-
new_thread do
|
11
|
-
counter = 0
|
12
|
-
|
13
|
-
loop do
|
14
|
-
command = RailsLiveReload::Command.new(params)
|
15
|
-
|
16
|
-
if command.reload?
|
17
|
-
stream.write(command.payload.to_json) and break
|
18
|
-
end
|
19
|
-
|
20
|
-
sleep(RailsLiveReload.config.long_polling_sleep_duration)
|
21
|
-
counter += 1
|
22
|
-
|
23
|
-
stream.write(command.payload.to_json) and break if counter >= max_sleeps_count
|
24
|
-
end
|
25
|
-
ensure
|
26
|
-
stream.close
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
[ 200, { 'Content-Type' => 'application/json', 'rack.hijack' => body }, nil ]
|
31
|
-
end
|
32
|
-
|
33
|
-
def client_javascript
|
34
|
-
RailsLiveReload::Client.long_polling_js
|
35
|
-
end
|
36
|
-
|
37
|
-
def max_sleeps_count
|
38
|
-
RailsLiveReload.config.timeout * (1 / RailsLiveReload.config.long_polling_sleep_duration)
|
39
|
-
end
|
40
|
-
|
41
|
-
def new_thread
|
42
|
-
Thread.new {
|
43
|
-
t2 = Thread.current
|
44
|
-
t2.abort_on_exception = true
|
45
|
-
yield
|
46
|
-
}
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
@@ -1,21 +0,0 @@
|
|
1
|
-
module RailsLiveReload
|
2
|
-
module Rails
|
3
|
-
module Middleware
|
4
|
-
class Polling < Base
|
5
|
-
private
|
6
|
-
|
7
|
-
def rails_live_response(request)
|
8
|
-
[
|
9
|
-
200,
|
10
|
-
{ 'Content-Type' => 'application/json' },
|
11
|
-
[ RailsLiveReload::Command.new(request.params).payload.to_json ]
|
12
|
-
]
|
13
|
-
end
|
14
|
-
|
15
|
-
def client_javascript
|
16
|
-
RailsLiveReload::Client.polling_js
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|