adsf-live 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d47be61674359db151dee6087cdd56c3953a7a211b3536746f40de00ea04f65a
4
+ data.tar.gz: 1fa194bd21d9e39af672fa8c156563da61510bbfc729e512f5288b276c90f875
5
+ SHA512:
6
+ metadata.gz: c9310e0603333d21366469a26d28d4750e107ea558aab1155f7f6e7335af2b65c89a0d3e0bb63859d043ecf97e0b9c2c20483eb76b981e75270119aae08dea83
7
+ data.tar.gz: badb7edd55e039eeec4c3dc97f5da98c2ed0da6ad532859f7ef06d88a8edeb41e7cc9e8c6c7dd169d960aeacc3d3d4c3ad81337896d4009bf8980dbfc90ec513
data/NEWS.md ADDED
@@ -0,0 +1,3 @@
1
+ # Release notes for adsf-live
2
+
3
+ The release notes for adsf-live are part of adsf.
data/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # adsf-live
2
+
3
+ See the documentation for adsf instead.
data/lib/adsf/live.rb ADDED
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'adsf'
4
+ require 'eventmachine'
5
+ require 'em-websocket'
6
+ require 'json'
7
+ require 'listen'
8
+ require 'rack-livereload'
9
+
10
+ module Adsf
11
+ module Live
12
+ end
13
+ end
14
+
15
+ require_relative 'live/version'
16
+ require_relative 'live/web_socket_server'
17
+ require_relative 'live/watcher'
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Adsf
4
+ module Live
5
+ VERSION = '1.4.0'
6
+ end
7
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Adsf
4
+ module Live
5
+ class Watcher
6
+ def initialize(root_dir:)
7
+ unless root_dir.start_with?('/')
8
+ raise ArgumentError, 'Watcher#initialize: The root_path argument must be an absolute path'
9
+ end
10
+
11
+ @root_dir = root_dir
12
+ end
13
+
14
+ def start
15
+ @server = start_server
16
+ @listener = start_listener(@server)
17
+ end
18
+
19
+ def stop
20
+ @listener&.stop
21
+ @server&.stop
22
+ end
23
+
24
+ def start_server
25
+ ::Adsf::Live::WebSocketServer.new(
26
+ host: '0.0.0.0',
27
+ port: '35729',
28
+ )
29
+ end
30
+
31
+ def start_listener(server)
32
+ options = {
33
+ latency: 0.0,
34
+ wait_for_delay: 0.0,
35
+ }
36
+
37
+ listener =
38
+ Listen.to(@root_dir, options) do |ch_mod, ch_add, ch_del|
39
+ handle_changes(server, [ch_mod, ch_add, ch_del].flatten)
40
+ end
41
+ listener.start
42
+ listener
43
+ end
44
+
45
+ def handle_changes(server, chs)
46
+ prefix_length = @root_dir.length
47
+ paths = chs.map { |pa| pa[prefix_length..-1] }
48
+ server.reload(paths)
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Adsf
4
+ module Live
5
+ class WebSocketServer
6
+ def initialize(host:, port:)
7
+ @host = host
8
+ @port = port
9
+
10
+ @thread = start
11
+ @sockets = []
12
+
13
+ @stopped_waiter = SizedQueue.new(1)
14
+ end
15
+
16
+ def stop
17
+ EventMachine.stop
18
+ @stopped_waiter.pop
19
+ end
20
+
21
+ def reload(paths)
22
+ paths.each do |path|
23
+ data =
24
+ JSON.dump(
25
+ command: 'reload',
26
+ path: "#{Dir.pwd}#{path}",
27
+ )
28
+
29
+ @sockets.each { |ws| ws.send(data) }
30
+ end
31
+ end
32
+
33
+ private
34
+
35
+ def start
36
+ started_waiter = SizedQueue.new(1)
37
+
38
+ thread =
39
+ Thread.new do
40
+ Thread.current.abort_on_exception = true
41
+ run(started_waiter)
42
+ @stopped_waiter << true
43
+ end
44
+
45
+ started_waiter.pop
46
+
47
+ thread
48
+ end
49
+
50
+ def run(started_waiter)
51
+ EventMachine.run do
52
+ EventMachine.defer(-> { started_waiter << true })
53
+
54
+ EventMachine::WebSocket.run(host: @host, port: @port) do |socket|
55
+ socket.onopen { on_socket_connected(socket) }
56
+ socket.onclose { on_socket_disconnected(socket) }
57
+ end
58
+ end
59
+ end
60
+
61
+ def on_socket_connected(socket)
62
+ socket.send(
63
+ JSON.dump(
64
+ command: 'hello',
65
+ protocols: ['http://livereload.com/protocols/official-7'],
66
+ serverName: 'nanoc-view',
67
+ ),
68
+ )
69
+
70
+ @sockets << socket
71
+ end
72
+
73
+ def on_socket_disconnected(socket)
74
+ @sockets.delete(socket)
75
+ end
76
+ end
77
+ end
78
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: adsf-live
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.4.0
5
+ platform: ruby
6
+ authors:
7
+ - Denis Defreyne
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-11-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: adsf
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: em-websocket
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.5'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: eventmachine
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.2'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: listen
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rack-livereload
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.3'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.3'
83
+ description: Automatically reloads when changes are detected.
84
+ email: denis.defreyne@stoneship.org
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - NEWS.md
90
+ - README.md
91
+ - lib/adsf/live.rb
92
+ - lib/adsf/live/version.rb
93
+ - lib/adsf/live/watcher.rb
94
+ - lib/adsf/live/web_socket_server.rb
95
+ homepage: http://github.com/ddfreyne/adsf/
96
+ licenses:
97
+ - MIT
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - "~>"
106
+ - !ruby/object:Gem::Version
107
+ version: '2.3'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.7.2
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: livereload support for adsf
119
+ test_files: []