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.
@@ -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