auto_reloader 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 10beff4b13c065b8d6c20336b1046c0786590c70
4
- data.tar.gz: eee5bfb397efaa8846dfe155d81a657b8e2f56d7
3
+ metadata.gz: 73f6ff42577565773f43c6d8089f35c4fdce994c
4
+ data.tar.gz: 65e7a40ff8248c7fddc86d895360077ce6fc16fd
5
5
  SHA512:
6
- metadata.gz: 5c3e98205d8ff2c48e73fd4a6c8e6046cec6c296de3388dc8df3d17ad6f924970b380075f30f43ee2389a6f28f57de343b0d36ec1f21b6f7b6352e388d5917c3
7
- data.tar.gz: 5520c5208041eaff4b9c23d33b0b290150f72cb9c2340b1f80264309d06a485ee84acd6b71469f8269e1e28cc3bd205f55fd6abed35dc4d60bc03bf018821157
6
+ metadata.gz: 1fd10e677eebbc4d7f08c1d284b40936912f6c71ba49bd747e778f5e25f68dd585f2f87d6592856178beea703e5b4c56f2d4684e5645cb3ef259511ea2d17c4a
7
+ data.tar.gz: 43323abf4020359ce4fb2f59132e7dc1abe53fb470120310d1cff8459f3d64505ce9379b44363c9b49ad7dbcaffbddaffb0a13f0a084d73e2c9346df2e828e96
data/.travis.yml CHANGED
@@ -1,4 +1,5 @@
1
1
  language: ruby
2
2
  rvm:
3
3
  - 2.3.1
4
+ - jruby-9.1.2.0
4
5
  before_install: gem install bundler -v 1.11.2
data/README.md CHANGED
@@ -59,7 +59,11 @@ Just change "Sample output" to something else and reload the page.
59
59
 
60
60
  By default reloading will only happen if one of the reloadable file was changed since it was
61
61
  required. This can be overriden by providing the `onchange: false` option to either `activate`
62
- or `reload!`.
62
+ or `reload!`. When the `listen` gem is available it's used by default to detect changes in
63
+ the `reloadable_paths` using specific OS watch mechanisms which allows AutoReloader to speed
64
+ up `reload!` when no changes happened although it won't probably do much difference unless
65
+ your application has tons of reloadable files loaded upon each request. If you don't want to
66
+ use `listen` when available, set `watch_paths: false` when calling `activate`.
63
67
 
64
68
  ## Known Caveats
65
69
 
@@ -19,4 +19,5 @@ Gem::Specification.new do |spec|
19
19
  spec.add_development_dependency 'bundler', '~> 1.11'
20
20
  spec.add_development_dependency 'rake', '~> 10.0'
21
21
  spec.add_development_dependency 'rspec', '~> 3.0'
22
+ spec.add_development_dependency 'listen', '~> 3.0'
22
23
  end
@@ -1,3 +1,3 @@
1
1
  class AutoReloader
2
- VERSION = '0.1.2'
2
+ VERSION = '0.2.0'
3
3
  end
data/lib/auto_reloader.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'auto_reloader/version'
2
2
  require 'singleton'
3
+ require 'forwardable'
3
4
  require 'monitor'
4
5
  require 'thread' # for Mutex
5
6
  require 'set'
@@ -7,9 +8,12 @@ require 'time' unless defined?(Process::CLOCK_MONOTONIC)
7
8
 
8
9
  class AutoReloader
9
10
  include Singleton
11
+ extend SingleForwardable
10
12
 
11
13
  attr_reader :reloadable_paths, :default_onchange, :default_delay
12
14
 
15
+ def_delegators :instance, :activate, :reload!, :reloadable_paths, :reloadable_paths=, :unload!
16
+
13
17
  module RequireOverride
14
18
  def require(path)
15
19
  AutoReloader.instance.require(path) { super }
@@ -21,37 +25,33 @@ class AutoReloader
21
25
  end
22
26
  end
23
27
 
24
- def self.activate(*args)
25
- instance.activate *args
26
- end
27
-
28
28
  def initialize
29
29
  @activate_lock = Mutex.new
30
30
  end
31
31
 
32
32
  ActivatedMoreThanOnce = Class.new RuntimeError
33
- def activate(reloadable_paths: [], onchange: true, delay: nil)
33
+ def activate(reloadable_paths: [], onchange: true, delay: nil, watch_paths: nil,
34
+ watch_latency: 1)
34
35
  @activate_lock.synchronize do
35
36
  raise ActivatedMoreThanOnce, "Can only activate Autoreloader once" if @reloadable_paths
36
37
  @default_delay = delay
37
38
  @default_onchange = onchange
38
- self.reloadable_paths = reloadable_paths
39
- Object.include RequireOverride
39
+ @watch_latency = watch_latency
40
40
  @require_lock = Monitor.new # monitor is like Mutex, but reentrant
41
41
  @reload_lock = Mutex.new
42
42
  @top_level_consts_stack = []
43
43
  @unload_constants = Set.new
44
44
  @unload_files = Set.new
45
45
  @last_reloaded = clock_time
46
+ try_listen unless watch_paths == false
47
+ self.reloadable_paths = reloadable_paths
48
+ Object.include RequireOverride
46
49
  end
47
50
  end
48
51
 
49
- def self.reloadable_paths=(paths)
50
- instance.reloadable_paths = paths
51
- end
52
-
53
52
  def reloadable_paths=(paths)
54
53
  @reloadable_paths = paths.map{|rp| File.expand_path(rp).freeze }.freeze
54
+ setup_listener if @watch_paths
55
55
  end
56
56
 
57
57
  def require(path, &block)
@@ -86,14 +86,6 @@ class AutoReloader
86
86
  Object.require fullpath
87
87
  end
88
88
 
89
- def self.reload!(delay: instance.default_delay, onchange: instance.default_onchange)
90
- if block_given?
91
- instance.reload!(delay: delay, onchange: onchange){ yield }
92
- else
93
- instance.reload!(delay: delay, onchange: onchange)
94
- end
95
- end
96
-
97
89
  InvalidUsage = Class.new RuntimeError
98
90
  def reload!(delay: default_delay, onchange: default_onchange)
99
91
  if onchange && !block_given?
@@ -111,10 +103,6 @@ class AutoReloader
111
103
  result
112
104
  end
113
105
 
114
- def self.unload!
115
- instance.unload!
116
- end
117
-
118
106
  def unload!
119
107
  @reload_lock.synchronize do
120
108
  @unload_files.each{|f| $LOADED_FEATURES.delete f }
@@ -124,8 +112,27 @@ class AutoReloader
124
112
  end
125
113
  end
126
114
 
115
+ def stop_listener
116
+ @listener.stop if @listener
117
+ end
118
+
127
119
  private
128
120
 
121
+ def try_listen
122
+ Kernel.require 'listen'
123
+ @watch_paths = true
124
+ rescue LoadError # ignore
125
+ #puts 'listen is not available. Add it to Gemfile if you want to speed up change detection.'
126
+ end
127
+
128
+ def setup_listener
129
+ @listener.stop if @listener
130
+ @listener = Listen.to(*@reloadable_paths, latency: @watch_latency) do |m, a, r|
131
+ @paths_changed = true
132
+ end
133
+ @listener.start
134
+ end
135
+
129
136
  if defined?(Process::CLOCK_MONOTONIC)
130
137
  def clock_time
131
138
  Process.clock_gettime(Process::CLOCK_MONOTONIC)
@@ -145,6 +152,8 @@ class AutoReloader
145
152
  end
146
153
 
147
154
  def changed?
155
+ return false if @watch_paths && !@paths_changed
156
+ @paths_changed = false
148
157
  return true unless @last_mtime_by_path
149
158
  @reload_lock.synchronize do
150
159
  return @unload_files.any?{|f| @last_mtime_by_path[f] != safe_mtime(f) }
metadata CHANGED
@@ -1,58 +1,72 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: auto_reloader
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rodrigo Rosenfeld Rosas
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
  date: 2016-07-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
+ name: bundler
14
15
  requirement: !ruby/object:Gem::Requirement
15
16
  requirements:
16
17
  - - "~>"
17
18
  - !ruby/object:Gem::Version
18
19
  version: '1.11'
19
- name: bundler
20
- prerelease: false
21
20
  type: :development
21
+ prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.11'
27
27
  - !ruby/object:Gem::Dependency
28
+ name: rake
28
29
  requirement: !ruby/object:Gem::Requirement
29
30
  requirements:
30
31
  - - "~>"
31
32
  - !ruby/object:Gem::Version
32
33
  version: '10.0'
33
- name: rake
34
- prerelease: false
35
34
  type: :development
35
+ prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
41
  - !ruby/object:Gem::Dependency
42
+ name: rspec
42
43
  requirement: !ruby/object:Gem::Requirement
43
44
  requirements:
44
45
  - - "~>"
45
46
  - !ruby/object:Gem::Version
46
47
  version: '3.0'
47
- name: rspec
48
+ type: :development
48
49
  prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
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'
49
62
  type: :development
63
+ prerelease: false
50
64
  version_requirements: !ruby/object:Gem::Requirement
51
65
  requirements:
52
66
  - - "~>"
53
67
  - !ruby/object:Gem::Version
54
68
  version: '3.0'
55
- description:
69
+ description:
56
70
  email:
57
71
  - rr.rosas@gmail.com
58
72
  executables: []
@@ -75,7 +89,7 @@ homepage: https://github.com/rosenfeld/auto_reloader
75
89
  licenses:
76
90
  - MIT
77
91
  metadata: {}
78
- post_install_message:
92
+ post_install_message:
79
93
  rdoc_options: []
80
94
  require_paths:
81
95
  - lib
@@ -90,9 +104,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
90
104
  - !ruby/object:Gem::Version
91
105
  version: '0'
92
106
  requirements: []
93
- rubyforge_project:
94
- rubygems_version: 2.6.4
95
- signing_key:
107
+ rubyforge_project:
108
+ rubygems_version: 2.5.1
109
+ signing_key:
96
110
  specification_version: 4
97
111
  summary: A transparent code reloader.
98
112
  test_files: []