Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move sanity checking to client-server interaction #162

Merged
merged 6 commits into from
Nov 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 24 additions & 24 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ GEM
faraday
async-io (1.32.2)
async
async-pool (0.3.8)
async-pool (0.3.9)
async (>= 1.25)
builder (3.2.4)
coderay (1.1.3)
Expand All @@ -95,7 +95,7 @@ GEM
fiber-local
crass (1.0.6)
erubi (1.10.0)
faraday (1.7.0)
faraday (1.8.0)
faraday-em_http (~> 1.0)
faraday-em_synchrony (~> 1.0)
faraday-excon (~> 1.1)
Expand Down Expand Up @@ -128,7 +128,7 @@ GEM
rake (>= 10.0)
globalid (0.5.2)
activesupport (>= 5.0)
i18n (1.8.10)
i18n (1.8.11)
concurrent-ruby (~> 1.0)
loofah (2.12.0)
crass (~> 1.0.2)
Expand All @@ -137,35 +137,35 @@ GEM
mail (2.7.1)
mini_mime (>= 0.1.1)
marcel (1.0.2)
method_source (0.9.2)
method_source (1.0.0)
mini_mime (1.1.2)
minitest (5.14.4)
mocha (1.13.0)
multi_json (1.15.0)
multipart-post (2.1.1)
nio4r (2.5.8)
nokogiri (1.12.5-arm64-darwin)
nokogiri (1.12.5-x86_64-linux)
racc (~> 1.4)
octokit (4.21.0)
faraday (>= 0.9)
sawyer (~> 0.8.0, >= 0.5.3)
parallel (1.20.1)
parallel (1.21.0)
parser (3.0.2.0)
ast (~> 2.4.1)
protocol-hpack (1.4.2)
protocol-http (0.22.5)
protocol-http1 (0.14.1)
protocol-http1 (0.14.2)
protocol-http (~> 0.22)
protocol-http2 (0.14.2)
protocol-hpack (~> 1.4)
protocol-http (~> 0.18)
pry (0.12.2)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
pry-nav (0.3.0)
pry (>= 0.9.10, < 0.13.0)
pry (0.14.1)
coderay (~> 1.1)
method_source (~> 1.0)
pry-nav (1.0.0)
pry (>= 0.9.10, < 0.15)
public_suffix (4.0.6)
racc (1.5.2)
racc (1.6.0)
rack (2.2.3)
rack-test (1.1.0)
rack (>= 1.0, < 3)
Expand Down Expand Up @@ -199,18 +199,18 @@ GEM
rake (13.0.6)
regexp_parser (2.1.1)
rexml (3.2.5)
rubocop (1.18.4)
rubocop (1.22.3)
parallel (~> 1.10)
parser (>= 3.0.0.0)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml
rubocop-ast (>= 1.8.0, < 2.0)
rubocop-ast (>= 1.12.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.10.0)
rubocop-ast (1.13.0)
parser (>= 3.0.1.1)
rubocop-performance (1.11.4)
rubocop-performance (1.11.5)
rubocop (>= 1.7.0, < 2.0)
rubocop-ast (>= 0.4.0)
ruby-progressbar (1.11.0)
Expand All @@ -226,24 +226,24 @@ GEM
activesupport (>= 4.0)
sprockets (>= 3.0.0)
sqlite3 (1.4.2)
standard (1.1.7)
rubocop (= 1.18.4)
rubocop-performance (= 1.11.4)
standard (1.4.0)
rubocop (= 1.22.3)
rubocop-performance (= 1.11.5)
standardrb (1.0.0)
standard
thor (1.1.0)
thread-local (1.1.0)
timers (4.3.3)
tzinfo (2.0.4)
concurrent-ruby (~> 1.0)
unicode-display_width (2.0.0)
unicode-display_width (2.1.0)
websocket-driver (0.7.5)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
zeitwerk (2.4.2)
zeitwerk (2.5.1)

PLATFORMS
ruby
x86_64-linux

DEPENDENCIES
cable_ready!
Expand All @@ -257,4 +257,4 @@ DEPENDENCIES
standardrb

BUNDLED WITH
2.2.19
2.2.27
2 changes: 2 additions & 0 deletions javascript/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { version } from '../package.json'
import * as MorphCallbacks from './morph_callbacks'
import { shouldMorphCallbacks, didMorphCallbacks } from './morph_callbacks'
import * as Utils from './utils'
Expand All @@ -24,6 +25,7 @@ export default {
consumer,
addOperation,
addOperations,
version,
get DOMOperations () {
console.warn(
'DEPRECATED: Please use `CableReady.operations` instead of `CableReady.DOMOperations`'
Expand Down
14 changes: 12 additions & 2 deletions lib/cable_ready/channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,23 @@ class Channel < OperationBuilder
attr_reader :identifier

def broadcast(clear: true)
ActionCable.server.broadcast identifier, {"cableReady" => true, "operations" => operations_payload}
clients_received = ActionCable.server.broadcast identifier, {
"cableReady" => true,
"operations" => operations_payload,
"version" => CableReady::VERSION
}
reset! if clear
clients_received
end

def broadcast_to(model, clear: true)
identifier.broadcast_to model, {"cableReady" => true, "operations" => operations_payload}
clients_received = identifier.broadcast_to model, {
"cableReady" => true,
"operations" => operations_payload,
"version" => CableReady::VERSION
}
reset! if clear
clients_received
end

def broadcast_later(clear: true)
Expand Down
58 changes: 0 additions & 58 deletions lib/cable_ready/sanity_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

class CableReady::SanityChecker
LATEST_VERSION_FORMAT = /^(\d+\.\d+\.\d+)$/
NODE_VERSION_FORMAT = /(\d+\.\d+\.\d+.*):/
JSON_VERSION_FORMAT = /(\d+\.\d+\.\d+.*)"/

class << self
def check!
Expand All @@ -13,7 +11,6 @@ def check!
return if called_by_rake?

instance = new
instance.check_package_versions_match
instance.check_new_version_available
end

Expand All @@ -28,28 +25,6 @@ def called_by_rake?
end
end

def check_package_versions_match
if npm_version.nil?
warn_and_exit <<~WARN
👉 Can't locate the cable_ready npm package.

yarn add cable_ready@#{gem_version}

Either add it to your package.json as a dependency or use "yarn link cable_ready" if you are doing development.
WARN
end

if package_version_mismatch?
warn_and_exit <<~WARN
👉 The cable_ready npm package version (#{npm_version}) does not match the Rubygem version (#{gem_version}).

To update the cable_ready npm package:

yarn upgrade cable_ready@#{gem_version}
WARN
end
end

def check_new_version_available
return if CableReady.config.on_new_version_available == :ignore
return if Rails.env.development? == false
Expand All @@ -75,45 +50,12 @@ def check_new_version_available

private

def package_version_mismatch?
npm_version != gem_version
end

def using_preview_release?
preview = CableReady::VERSION.match?(LATEST_VERSION_FORMAT) == false
puts "👉 CableReady #{CableReady::VERSION} update check skipped: pre-release build" if preview
preview
end

def gem_version
@_gem_version ||= CableReady::VERSION.gsub(".pre", "-pre")
end

def npm_version
@_npm_version ||= find_npm_version
end

def find_npm_version
if (match = search_file(package_json_path, regex: /version/))
match[JSON_VERSION_FORMAT, 1]
elsif (match = search_file(yarn_lock_path, regex: /^cable_ready/))
match[NODE_VERSION_FORMAT, 1]
end
end

def search_file(path, regex:)
return if File.exist?(path) == false
File.foreach(path).grep(regex).first
end

def package_json_path
Rails.root.join("node_modules", "cable_ready", "package.json")
end

def yarn_lock_path
Rails.root.join("yarn.lock")
end

def initializer_missing?
File.exist?(Rails.root.join("config", "initializers", "cable_ready.rb")) == false
end
Expand Down