react-rails-hot-loader 0.5.0 → 0.6.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: 2ccd666621d038150005b210e1780351a32c3869
4
- data.tar.gz: 3b162749ab5c6fc4dddf9c9afec34f9cb0588b83
3
+ metadata.gz: 7984784ebf6f4c9bd1c59261311bc3bf2c30e37e
4
+ data.tar.gz: 4ab544abfee0ecd7fa15f80069ad08e36661f973
5
5
  SHA512:
6
- metadata.gz: 7b73516078d8c51f6cb23ee7525e29f5e4adfccd35e23f449884a5b5c1d8bc0de3c6fa3e071f6bdbf82872a469a6e63c6b0d75d40032b1476d994e94e48cb735
7
- data.tar.gz: 98500e70dfe7c7d137a995cb63ae65ac349a90ccb29d4d34892d49a02b3a201d9a87873e602a4f79fb9797b95df937d23b024d6a7b710ac15971530f34577535
6
+ metadata.gz: 3069d4c3f71c033bdc9f02cc1bf84aef91bde023c181e5e0c17261df3cd80f272631588b88b7c155cd50ce352ec43d01e7395e9eb70998df520a194c95279035
7
+ data.tar.gz: 306bd528fc9636645f0d67926e623b7859ed2cbe143eb73246a36b8c6a20285bda08cbb481da45d0eaaccef6b488631ff52bd0b85517bccb908bf0700df0c197
data/.travis.yml CHANGED
@@ -1,7 +1,7 @@
1
1
  language: ruby
2
+ cache: bundler
2
3
  rvm:
3
- - 2.2.0
4
- before_install: gem install bundler -v 1.10.3
4
+ - 2.3.1
5
5
  addons:
6
6
  code_climate:
7
7
  repo_token: 89617e5ca8a5789349231b46f543b5666f139bfb23d5946bc4517609926071f9
data/README.md CHANGED
@@ -24,15 +24,6 @@ Or install it yourself as:
24
24
 
25
25
  ## Usage
26
26
 
27
- - Add an initializer:
28
-
29
- ```ruby
30
- # config/initializers/react_rails_hot_loader.rb
31
- if Rails.env.development?
32
- React::Rails::HotLoader.start()
33
- end
34
- ```
35
-
36
27
  - Include the JavaScript:
37
28
 
38
29
  ```js
@@ -46,6 +37,8 @@ Or install it yourself as:
46
37
 
47
38
  - Edit files in `/app/assets/javascripts` & save changes -- they'll be reloaded in the client and React components will be remounted.
48
39
 
40
+ (This gem includes an initializer to start a change notification server in development _only_.)
41
+
49
42
  ## Configuration
50
43
 
51
44
  If you notice that your assets are not being recompiled and hot loaded, it could be because they aren't being matched by the default asset glob used (`**/*.{js,coffee}*`). You can modify this asset glob like so:
@@ -55,6 +48,13 @@ If you notice that your assets are not being recompiled and hot loaded, it could
55
48
  React::Rails::HotLoader::AssetChangeSet.asset_glob = "**/*.{js,rb}*" # I <3 Opal
56
49
  ```
57
50
 
51
+ You can choose a port to start it on (default is `8082`):
52
+
53
+ ```ruby
54
+ # config/initializers/react_rails_hot_loader.rb
55
+ React::Rails::HotLoader.port = 8088
56
+ ```
57
+
58
58
  ## Doeses & Doesn'ts
59
59
 
60
60
  `react-rails-hot-loader` ...
@@ -67,9 +67,8 @@ React::Rails::HotLoader::AssetChangeSet.asset_glob = "**/*.{js,rb}*" # I <3 Opal
67
67
 
68
68
  ## TODO
69
69
 
70
- - Replace pinging with file watching?
71
- - Add `rails g react-rails-hot-loader:install` to add initializer and JS
72
- - Remove need for initializer by automatically starting in `config.after_initialize`?
70
+ - Figure out how the "real" React hot-loader preserves state and do that
71
+ - Log out when a push fails, or log the JS code if the push succeeds but doesn't get eval'ed
73
72
 
74
73
  ## License
75
74
 
@@ -1,6 +1,6 @@
1
1
  var ReactRailsHotLoader = {
2
2
  since: null,
3
- port: 8082,
3
+ port: <%= React::Rails::HotLoader.port %>,
4
4
  host: window.location.hostname,
5
5
  shouldLog: true,
6
6
 
@@ -17,17 +17,11 @@ var ReactRailsHotLoader = {
17
17
 
18
18
  this._socket.onmessage = function(message) {
19
19
  var changes = JSON.parse(message.data)
20
- _this.log("updating: " + changes.changed_file_names.join(", ") )
21
- changes.changed_asset_contents.forEach(function(jsCode) {
22
- try {
23
- eval.call(window, jsCode)
24
- } catch (err) {
25
- _this.log(err)
26
- }
27
- })
28
- ReactRailsUJS.mountComponents()
29
- _this._resetSince()
30
- _this.log("update finished")
20
+ if (changes.bankrupt) {
21
+ _this._handleBankrupt(changes)
22
+ } else {
23
+ _this._hotLoad(changes)
24
+ }
31
25
  }
32
26
 
33
27
  this._interval = setInterval(function() {
@@ -53,7 +47,28 @@ var ReactRailsHotLoader = {
53
47
  if (this.shouldLog) {
54
48
  console.log("[HotLoader] " + msg)
55
49
  }
56
- }
50
+ },
51
+
52
+ _hotLoad: function(changes) {
53
+ this.log("updating: " + changes.changed_file_names.join(", ") )
54
+
55
+ changes.changed_asset_contents.forEach(function(jsCode) {
56
+ try {
57
+ eval.call(window, jsCode)
58
+ } catch (err) {
59
+ _this.log(err)
60
+ }
61
+ })
62
+ ReactRailsUJS.mountComponents()
63
+ this._resetSince()
64
+ this.log("update finished")
65
+ },
66
+
67
+ _handleBankrupt: function(response) {
68
+ this.log("declaring bankruptcy! " + response.changed_files_count + " files changed.")
69
+ this.stop()
70
+ this.log("stopped")
71
+ },
57
72
  }
58
73
 
59
74
  ReactRailsHotLoader.start()
@@ -2,9 +2,12 @@ module React
2
2
  module Rails
3
3
  module HotLoader
4
4
  class AssetChangeSet
5
- attr_reader :since, :path, :changed_files, :changed_file_names, :changed_asset_contents
6
- class_attribute :asset_glob
5
+ attr_reader :since, :path, :changed_files, :changed_file_names
6
+ class_attribute :asset_glob, :bankruptcy_count
7
+ # Search for changes with this glob
7
8
  self.asset_glob = "/**/*.{js,coffee}*"
9
+ # If this many files change at once, give up hope! (Probably checked out a new branch)
10
+ self.bankruptcy_count = 5
8
11
 
9
12
  # initialize with a path and time
10
13
  # to find files which changed since that time
@@ -13,12 +16,11 @@ module React
13
16
  @path = path.to_s
14
17
  asset_glob = File.join(path, AssetChangeSet.asset_glob)
15
18
  @changed_files = Dir.glob(asset_glob).select { |f| File.mtime(f) >= since }
16
- @changed_file_names = changed_files.map { |f| f.split("/").last }
17
- @changed_asset_contents = changed_files.map do |f|
18
- logical_path = to_logical_path(f)
19
- asset = ::Rails.application.assets[logical_path]
20
- asset.to_s
21
- end
19
+ @changed_file_names = changed_files.map { |f| File.basename(f) }
20
+ end
21
+
22
+ def bankrupt?
23
+ changed_files.length >= self.class.bankruptcy_count
22
24
  end
23
25
 
24
26
  def any?
@@ -34,6 +36,14 @@ module React
34
36
  }
35
37
  end
36
38
 
39
+ def changed_asset_contents
40
+ @changed_asset_contents ||= changed_files.map do |f|
41
+ logical_path = to_logical_path(f)
42
+ asset = ::Rails.application.assets[logical_path]
43
+ asset.to_s
44
+ end
45
+ end
46
+
37
47
  private
38
48
 
39
49
  def to_logical_path(asset_path)
@@ -8,20 +8,24 @@ module React
8
8
  end
9
9
 
10
10
  config.after_initialize do |app|
11
- ActionDispatch::Reloader.to_prepare do
12
- # Seems like Passenger kills threads on the main process
13
- # In that case, `starting_worker_process` isn't good enough
14
- # because it doesn't run :(
11
+ if ::Rails.env.development?
15
12
  React::Rails::HotLoader.restart
16
- end
17
13
 
18
- if defined?(PhusionPassenger)
19
- PhusionPassenger.on_event(:starting_worker_process) do |forked|
20
- if forked
21
- # We're in smart spawning mode.
22
- React::Rails::HotLoader.restart
23
- else
24
- # We're in direct spawning mode. We don't need to do anything.
14
+ ActionDispatch::Reloader.to_prepare do
15
+ # Seems like Passenger kills threads on the main process
16
+ # In that case, `starting_worker_process` isn't good enough
17
+ # because it doesn't run :(
18
+ React::Rails::HotLoader.restart
19
+ end
20
+
21
+ if defined?(PhusionPassenger)
22
+ PhusionPassenger.on_event(:starting_worker_process) do |forked|
23
+ if forked
24
+ # We're in smart spawning mode.
25
+ React::Rails::HotLoader.restart
26
+ else
27
+ # We're in direct spawning mode. We don't need to do anything.
28
+ end
25
29
  end
26
30
  end
27
31
  end
@@ -6,7 +6,7 @@ module React
6
6
  class Server
7
7
  attr_reader :host, :port, :change_set_class
8
8
 
9
- def initialize(host: "0.0.0.0", port: 8082, change_set_class: React::Rails::HotLoader::AssetChangeSet)
9
+ def initialize(host: "0.0.0.0", port:, change_set_class: React::Rails::HotLoader::AssetChangeSet)
10
10
  @host = host
11
11
  @port = port
12
12
  @change_set_class = change_set_class
@@ -16,7 +16,11 @@ module React
16
16
  def restart
17
17
  start
18
18
  rescue StandardError => err
19
- React::Rails::HotLoader.error(err)
19
+ if err.message =~ /no acceptor/
20
+ React::Rails::HotLoader.log("WS server is already running (#{ws_url})")
21
+ else
22
+ React::Rails::HotLoader.error(err)
23
+ end
20
24
  end
21
25
 
22
26
  private
@@ -38,7 +42,17 @@ module React
38
42
  # React::Rails::HotLoader.log("received message: #{msg}")
39
43
  since_time = Time.at(msg.to_i)
40
44
  changes = change_set_class.new(since: since_time)
41
- if changes.any?
45
+ if changes.bankrupt?
46
+ changed_files_count = changes.changed_file_names.length
47
+ React::Rails::HotLoader.log("declared bankruptcy! (#{changed_files_count} files changed)")
48
+
49
+ bankruptcy_response = {
50
+ bankrupt: true,
51
+ changed_files_count: changed_files_count,
52
+ }
53
+
54
+ ws.send(bankruptcy_response.to_json)
55
+ elsif changes.any?
42
56
  React::Rails::HotLoader.log("sent changes: #{changes.changed_file_names}")
43
57
  ws.send(changes.to_json)
44
58
  end
@@ -57,12 +71,7 @@ module React
57
71
  end
58
72
 
59
73
  React::Rails::HotLoader.log("started WS server (#{ws_url})")
60
- rescue StandardError => err
61
- if err.message =~ /no acceptor/
62
- React::Rails::HotLoader.log("WS server is already running (#{ws_url})")
63
- else
64
- React::Rails::HotLoader.error(err)
65
- end
74
+
66
75
  end
67
76
 
68
77
  def already_has_event_machine_server?
@@ -1,7 +1,7 @@
1
1
  module React
2
2
  module Rails
3
3
  module HotLoader
4
- VERSION = '0.5.0'
4
+ VERSION = "0.6.0"
5
5
  end
6
6
  end
7
7
  end
data/lib/hot_loader.rb CHANGED
@@ -2,17 +2,13 @@ module React
2
2
  module Rails
3
3
  module HotLoader
4
4
  mattr_accessor :server
5
+ mattr_accessor :port
6
+ self.port = 8082
5
7
 
6
- # Create a new server with `options` & start it
7
- def self.start(options={})
8
- server_class = options.delete(:server_class) || Server
9
- self.server = server_class.new(options)
10
- restart
11
- end
12
-
13
- # Restart the server with the same options
8
+ # Start _or_ restart the server
14
9
  def self.restart
15
- server.restart
10
+ self.server ||= Server.new(port: port)
11
+ self.server.restart
16
12
  rescue StandardError => err
17
13
  React::Rails::HotLoader.error(err)
18
14
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: react-rails-hot-loader
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Mosolgo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-24 00:00:00.000000000 Z
11
+ date: 2016-05-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: em-websocket
@@ -210,7 +210,7 @@ files:
210
210
  - bin/console
211
211
  - bin/setup
212
212
  - lib/assets/dummy/react-rails-hot-loader.js
213
- - lib/assets/react-rails-hot-loader.js
213
+ - lib/assets/react-rails-hot-loader.js.erb
214
214
  - lib/hot_loader.rb
215
215
  - lib/hot_loader/asset_change_set.rb
216
216
  - lib/hot_loader/asset_path.rb
@@ -239,7 +239,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
239
239
  version: '0'
240
240
  requirements: []
241
241
  rubyforge_project:
242
- rubygems_version: 2.4.5
242
+ rubygems_version: 2.5.1
243
243
  signing_key:
244
244
  specification_version: 4
245
245
  summary: Live-reload React.js components with Ruby on Rails