react-rails-hot-loader 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 87d38a21b1f7917eb29e27a54592a9018402baeb
4
+ data.tar.gz: 88269e19352ab97728fd47c4dd8fb21bd4021d4a
5
+ SHA512:
6
+ metadata.gz: 53c3b2306215e96d4fa626ee9f933c4aea0689d917b97365baf922e50f390b953b68dee8d53a71b65595c64dda0e5a52bf0b06b9c04a8f0c436e4804dc7ceda7
7
+ data.tar.gz: 55291a15d02363ebe0bb1b671ec9458e51e55c76268230edcf6478a3cf103c5fb3b498f5de9f066e1ef2ea440ba02de969cf59143a5e104b8adf2de10a1586c0
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ before_install: gem install bundler -v 1.10.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in react-rails-hot-loader.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,5 @@
1
+ guard :minitest do
2
+ watch(%r{^test/(.*)\/?(.*)_test\.rb$})
3
+ watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| "test/#{m[1]}#{m[2]}_test.rb" }
4
+ watch(%r{^test/test_helper\.rb$}) { 'test' }
5
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Robert Mosolgo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # React::Rails::HotLoader
2
+
3
+ Reload React.js components with Ruby on Rails & [`react-rails`](http://github.com/reactjs/react-rails).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'react-rails-hot-loader'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install react-rails-hot-loader
20
+
21
+ ## Usage
22
+
23
+ - Add an initializer (eg, `app/config/initializers/react_rails_hot_loader.rb`):
24
+
25
+ ```ruby
26
+ if Rails.env.development?
27
+ React::Rails::HotLoader.start()
28
+ end
29
+ ```
30
+
31
+ - Include the Javascript (eg, in `app/assets/javascripts/application.js`):
32
+
33
+ ```js
34
+ //= require react_rails_hot_loader
35
+ ```
36
+
37
+ (When not `Rails.env.development?`, this requires an empty file, so don't worry about leaving it in production deploys.)
38
+
39
+ - Restart your development server
40
+
41
+ ## Doeses & Doesn'ts
42
+
43
+ `react-rails-hot-loader` ...
44
+
45
+ - __does__ set up a WebSocket server & client
46
+ - __does__ reload JS assets when they change (from `/app/assets/javascripts/*.js*`)
47
+ - __does__ remount components (via `ReactRailsUJS`) after reloading assets
48
+ - __does__ preserve state & props (because `React.render` does that out of the box)
49
+ - __doesn't__ reload Rails view files (`html`, `erb`, `slim`, etc)
50
+ - __doesn't__ reload CSS (although that could be fixed)
51
+
52
+ ## TODO
53
+
54
+ - Handle Passenger occasionally killing background threads :(
55
+ - Replace pinging with file watching
56
+ - Test Server, AssetPath
57
+ - Add `rails g react-rails-hot-loader:install` to add initializer and JS
58
+
59
+ ## License
60
+
61
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "react/rails/hot/loader"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ // This file is blank intentionally
2
+ // so that you can safely require react-rails-hot-loader
3
+ // in development, and not worry about what happens in production.
@@ -0,0 +1,69 @@
1
+ ReactRailsHotLoader = {
2
+ since: null,
3
+ port: 8082,
4
+ host: window.location.host,
5
+ shouldLog: true,
6
+
7
+ start: function() {
8
+ this._resetSince()
9
+ var wsURL = "ws://" + this.host + ":" + this.port
10
+ this.log(" connecting to " + wsURL)
11
+ this._socket = new WebSocket(wsURL)
12
+
13
+ var _this = this
14
+
15
+ this._socket.onmessage = function(message) {
16
+ var changes = JSON.parse(message.data)
17
+ _this.log("updating " + changes.changed_asset_paths.length + " assets")
18
+ changes.changed_asset_paths.forEach(_this._reloadAsset, _this)
19
+ _this._resetSince()
20
+ }
21
+
22
+ this._interval = setInterval(function() {
23
+ if (_this._socket.readyState == WebSocket.OPEN) {
24
+ _this._socket.send(_this.since)
25
+ } else {
26
+ _this.log("WebSocket not ready, readyState: " + _this._socket.readyState)
27
+ }
28
+ }, 1500)
29
+ },
30
+
31
+ stop: function() {
32
+ this._socket.close()
33
+ clearInterval(this._interval)
34
+ },
35
+
36
+ _reloadAsset: function(path) {
37
+ var _this = this
38
+ var _handleResponse = function(js) {
39
+ try {
40
+ eval.call(window, js)
41
+ } catch (err) {
42
+ _this.log(err)
43
+ }
44
+ ReactRailsUJS.mountComponents()
45
+ var name = path.split("/").pop()
46
+ _this.log("reloaded " + name)
47
+ }
48
+
49
+ $.ajax({
50
+ type: "GET",
51
+ url: path,
52
+ success: _handleResponse,
53
+ error: _handleResponse,
54
+ })
55
+ },
56
+
57
+ _resetSince: function() {
58
+ // Ruby prefers seconds.
59
+ this.since = Date.now() / 1000
60
+ },
61
+
62
+ log: function(msg) {
63
+ if (this.shouldLog) {
64
+ console.log("[HotLoader] " + msg)
65
+ }
66
+ }
67
+ }
68
+
69
+ ReactRailsHotLoader.start()
data/lib/hot_loader.rb ADDED
@@ -0,0 +1,21 @@
1
+ module React
2
+ module Rails
3
+ module HotLoader
4
+ mattr_accessor :server
5
+ def self.start(*args)
6
+ self.server = Server.new(*args)
7
+ server.restart
8
+ end
9
+
10
+ def self.restart
11
+ server.restart
12
+ rescue StandardError => err
13
+ log("failed to restart: #{err}")
14
+ end
15
+
16
+ def self.log(message)
17
+ ::Rails.logger.info("[HotLoader] #{message}")
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,30 @@
1
+ module React
2
+ module Rails
3
+ module HotLoader
4
+ class AssetChangeSet
5
+ attr_reader :since, :path, :changed_files, :changed_asset_paths
6
+ # initialize with a path and time
7
+ # to find files which changed since that time
8
+ def initialize(since:, path: ::Rails.root.join("app/assets/javascripts"))
9
+ @since = since
10
+ @path = path.to_s
11
+ asset_glob = path.to_s + "/**/*.js.*"
12
+ @changed_files = Dir.glob(asset_glob).select { |f| File.mtime(f) >= since }
13
+ @changed_asset_paths = changed_files.map {|f| f.sub(path.to_s, "/assets").sub(/\.js.*/, ".js")}
14
+ end
15
+
16
+ def any?
17
+ changed_asset_paths.any?
18
+ end
19
+
20
+ def as_json(options={})
21
+ {
22
+ since: since,
23
+ path: path,
24
+ changed_asset_paths: changed_asset_paths,
25
+ }
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,16 @@
1
+ module React
2
+ module Rails
3
+ module HotLoader
4
+ class AssetPath
5
+ GEM_LIB_PATH = Pathname.new('../../').expand_path(__FILE__)
6
+
7
+ attr_reader :to_s
8
+
9
+ def initialize(dummy: false)
10
+ asset_path = dummy ? "assets/dummy": "assets"
11
+ @to_s = File.join(GEM_LIB_PATH, asset_path)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,24 @@
1
+ module React
2
+ module Rails
3
+ module HotLoader
4
+ class Railtie < ::Rails::Railtie
5
+ config.before_initialize do |app|
6
+ # We want to include different files in dev/prod.
7
+ # The production file is empty!
8
+ asset_path = React::Rails::HotLoader::AssetPath.new(dummy: !(::Rails.env.development?))
9
+ app.config.assets.paths << asset_path.to_s
10
+ end
11
+
12
+ config.after_initialize do |app|
13
+ ActionDispatch::Reloader.to_prepare do
14
+ begin
15
+ React::Rails::HotLoader.restart
16
+ rescue StandardError => err
17
+ React::Rails::HotLoader.log("to_prepare failed: #{err}\n#{err.backtrace.join("\n")}")
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,59 @@
1
+ require 'em-websocket'
2
+
3
+ module React
4
+ module Rails
5
+ module HotLoader
6
+ class Server
7
+ attr_reader :host, :port, :change_set_class
8
+
9
+ def initialize(host: "0.0.0.0", port: 8082, change_set_class: React::Rails::HotLoader::AssetChangeSet)
10
+ @host = host
11
+ @port = port
12
+ @change_set_class = change_set_class
13
+ @thread = nil
14
+ end
15
+
16
+ def restart
17
+ if @thread.blank? || @thread.stop?
18
+ "[HotLoader] restarting: #{@thread}, #{@thread.try(:status)}"
19
+ @thread = Thread.new do
20
+ begin
21
+ serve
22
+ rescue StandardError => err
23
+ React::Rails::HotLoader.log("failed to serve: #{err}\n#{err.backtrace.join("\n")}")
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ def serve
30
+ EM.run {
31
+ React::Rails::HotLoader.log("starting WS server...")
32
+
33
+ EM::WebSocket.run(host: host, port: port) do |ws|
34
+ ws.onopen { React::Rails::HotLoader.log("opened a connection") }
35
+
36
+ ws.onmessage { |msg|
37
+ begin
38
+ since_time = Time.at(msg.to_i)
39
+ changes = change_set_class.new(since: since_time)
40
+ if changes.any?
41
+ React::Rails::HotLoader.log("received message: #{msg}")
42
+ React::Rails::HotLoader.log("sent response: #{changes.to_json}")
43
+ ws.send(changes.to_json)
44
+ end
45
+ rescue StandardError => err
46
+ React::Rails::HotLoader.log("message error: #{err}\n #{err.backtrace.join("\n")}")
47
+ end
48
+ }
49
+
50
+ ws.onclose { React::Rails::HotLoader.log("closed a connection") }
51
+ end
52
+
53
+ React::Rails::HotLoader.log("started WS server")
54
+ }
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,7 @@
1
+ module React
2
+ module Rails
3
+ module HotLoader
4
+ VERSION = '0.0.1'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,6 @@
1
+ require 'hot_loader'
2
+ require 'hot_loader/asset_change_set'
3
+ require 'hot_loader/asset_path'
4
+ require 'hot_loader/server'
5
+ require 'hot_loader/railtie'
6
+ require 'hot_loader/version'
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'hot_loader/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "react-rails-hot-loader"
8
+ spec.version = React::Rails::HotLoader::VERSION
9
+ spec.authors = ["Robert Mosolgo"]
10
+ spec.email = ["rdmosolgo@gmail.com"]
11
+
12
+ spec.summary = %q{Live-reload React.js components with Ruby on Rails}
13
+ spec.description = %q{Tie into the `react-rails` gem to notify the client of changed files & reload assets on the client when they are changed.}
14
+ spec.homepage = "http://github.com/rmosolgo/react-rails-hot-loader"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_runtime_dependency "em-websocket"
21
+ spec.add_runtime_dependency "react-rails"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.10"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "minitest"
26
+ spec.add_development_dependency "guard"
27
+ spec.add_development_dependency "guard-minitest"
28
+ end
metadata ADDED
@@ -0,0 +1,162 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: react-rails-hot-loader
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Robert Mosolgo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: em-websocket
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: react-rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: guard
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: guard-minitest
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Tie into the `react-rails` gem to notify the client of changed files
112
+ & reload assets on the client when they are changed.
113
+ email:
114
+ - rdmosolgo@gmail.com
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".gitignore"
120
+ - ".travis.yml"
121
+ - Gemfile
122
+ - Guardfile
123
+ - LICENSE.txt
124
+ - README.md
125
+ - Rakefile
126
+ - bin/console
127
+ - bin/setup
128
+ - lib/assets/dummy/react-rails-hot-loader.js
129
+ - lib/assets/react-rails-hot-loader.js
130
+ - lib/hot_loader.rb
131
+ - lib/hot_loader/asset_change_set.rb
132
+ - lib/hot_loader/asset_path.rb
133
+ - lib/hot_loader/railtie.rb
134
+ - lib/hot_loader/server.rb
135
+ - lib/hot_loader/version.rb
136
+ - lib/react-rails-hot-loader.rb
137
+ - react-rails-hot-loader.gemspec
138
+ homepage: http://github.com/rmosolgo/react-rails-hot-loader
139
+ licenses:
140
+ - MIT
141
+ metadata: {}
142
+ post_install_message:
143
+ rdoc_options: []
144
+ require_paths:
145
+ - lib
146
+ required_ruby_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ required_rubygems_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ requirements: []
157
+ rubyforge_project:
158
+ rubygems_version: 2.4.5
159
+ signing_key:
160
+ specification_version: 4
161
+ summary: Live-reload React.js components with Ruby on Rails
162
+ test_files: []