actioncable 5.2.2.1 → 6.0.2

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.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +150 -20
  3. data/MIT-LICENSE +1 -1
  4. data/README.md +2 -545
  5. data/app/assets/javascripts/action_cable.js +517 -0
  6. data/lib/action_cable.rb +15 -7
  7. data/lib/action_cable/channel.rb +1 -0
  8. data/lib/action_cable/channel/base.rb +7 -1
  9. data/lib/action_cable/channel/broadcasting.rb +18 -8
  10. data/lib/action_cable/channel/streams.rb +1 -1
  11. data/lib/action_cable/channel/test_case.rb +310 -0
  12. data/lib/action_cable/connection.rb +1 -0
  13. data/lib/action_cable/connection/authorization.rb +1 -1
  14. data/lib/action_cable/connection/base.rb +11 -7
  15. data/lib/action_cable/connection/message_buffer.rb +1 -4
  16. data/lib/action_cable/connection/stream.rb +4 -2
  17. data/lib/action_cable/connection/subscriptions.rb +1 -5
  18. data/lib/action_cable/connection/test_case.rb +234 -0
  19. data/lib/action_cable/connection/web_socket.rb +1 -3
  20. data/lib/action_cable/gem_version.rb +3 -3
  21. data/lib/action_cable/server/base.rb +8 -3
  22. data/lib/action_cable/server/worker.rb +5 -7
  23. data/lib/action_cable/subscription_adapter.rb +1 -0
  24. data/lib/action_cable/subscription_adapter/postgresql.rb +26 -8
  25. data/lib/action_cable/subscription_adapter/redis.rb +4 -1
  26. data/lib/action_cable/subscription_adapter/test.rb +40 -0
  27. data/lib/action_cable/test_case.rb +11 -0
  28. data/lib/action_cable/test_helper.rb +133 -0
  29. data/lib/rails/generators/channel/USAGE +4 -5
  30. data/lib/rails/generators/channel/channel_generator.rb +6 -3
  31. data/lib/rails/generators/channel/templates/{assets → javascript}/channel.js.tt +6 -4
  32. data/lib/rails/generators/channel/templates/{assets/cable.js.tt → javascript/consumer.js.tt} +2 -9
  33. data/lib/rails/generators/channel/templates/javascript/index.js.tt +5 -0
  34. data/lib/rails/generators/test_unit/channel_generator.rb +20 -0
  35. data/lib/rails/generators/test_unit/templates/channel_test.rb.tt +8 -0
  36. metadata +23 -13
  37. data/lib/assets/compiled/action_cable.js +0 -601
  38. data/lib/rails/generators/channel/templates/assets/channel.coffee.tt +0 -14
@@ -5,6 +5,8 @@ require "thread"
5
5
  gem "redis", ">= 3", "< 5"
6
6
  require "redis"
7
7
 
8
+ require "active_support/core_ext/hash/except"
9
+
8
10
  module ActionCable
9
11
  module SubscriptionAdapter
10
12
  class Redis < Base # :nodoc:
@@ -13,7 +15,8 @@ module ActionCable
13
15
  # Overwrite this factory method for Redis connections if you want to use a different Redis library than the redis gem.
14
16
  # This is needed, for example, when using Makara proxies for distributed Redis.
15
17
  cattr_accessor :redis_connector, default: ->(config) do
16
- ::Redis.new(config.slice(:url, :host, :port, :db, :password))
18
+ config[:id] ||= "ActionCable-PID-#{$$}"
19
+ ::Redis.new(config.except(:adapter, :channel_prefix))
17
20
  end
18
21
 
19
22
  def initialize(*)
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "async"
4
+
5
+ module ActionCable
6
+ module SubscriptionAdapter
7
+ # == Test adapter for Action Cable
8
+ #
9
+ # The test adapter should be used only in testing. Along with
10
+ # <tt>ActionCable::TestHelper</tt> it makes a great tool to test your Rails application.
11
+ #
12
+ # To use the test adapter set +adapter+ value to +test+ in your +config/cable.yml+ file.
13
+ #
14
+ # NOTE: Test adapter extends the <tt>ActionCable::SubscriptionsAdapter::Async</tt> adapter,
15
+ # so it could be used in system tests too.
16
+ class Test < Async
17
+ def broadcast(channel, payload)
18
+ broadcasts(channel) << payload
19
+ super
20
+ end
21
+
22
+ def broadcasts(channel)
23
+ channels_data[channel] ||= []
24
+ end
25
+
26
+ def clear_messages(channel)
27
+ channels_data[channel] = []
28
+ end
29
+
30
+ def clear
31
+ @channels_data = nil
32
+ end
33
+
34
+ private
35
+ def channels_data
36
+ @channels_data ||= {}
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/test_case"
4
+
5
+ module ActionCable
6
+ class TestCase < ActiveSupport::TestCase
7
+ include ActionCable::TestHelper
8
+
9
+ ActiveSupport.run_load_hooks(:action_cable_test_case, self)
10
+ end
11
+ end
@@ -0,0 +1,133 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionCable
4
+ # Provides helper methods for testing Action Cable broadcasting
5
+ module TestHelper
6
+ def before_setup # :nodoc:
7
+ server = ActionCable.server
8
+ test_adapter = ActionCable::SubscriptionAdapter::Test.new(server)
9
+
10
+ @old_pubsub_adapter = server.pubsub
11
+
12
+ server.instance_variable_set(:@pubsub, test_adapter)
13
+ super
14
+ end
15
+
16
+ def after_teardown # :nodoc:
17
+ super
18
+ ActionCable.server.instance_variable_set(:@pubsub, @old_pubsub_adapter)
19
+ end
20
+
21
+ # Asserts that the number of broadcasted messages to the stream matches the given number.
22
+ #
23
+ # def test_broadcasts
24
+ # assert_broadcasts 'messages', 0
25
+ # ActionCable.server.broadcast 'messages', { text: 'hello' }
26
+ # assert_broadcasts 'messages', 1
27
+ # ActionCable.server.broadcast 'messages', { text: 'world' }
28
+ # assert_broadcasts 'messages', 2
29
+ # end
30
+ #
31
+ # If a block is passed, that block should cause the specified number of
32
+ # messages to be broadcasted.
33
+ #
34
+ # def test_broadcasts_again
35
+ # assert_broadcasts('messages', 1) do
36
+ # ActionCable.server.broadcast 'messages', { text: 'hello' }
37
+ # end
38
+ #
39
+ # assert_broadcasts('messages', 2) do
40
+ # ActionCable.server.broadcast 'messages', { text: 'hi' }
41
+ # ActionCable.server.broadcast 'messages', { text: 'how are you?' }
42
+ # end
43
+ # end
44
+ #
45
+ def assert_broadcasts(stream, number)
46
+ if block_given?
47
+ original_count = broadcasts_size(stream)
48
+ yield
49
+ new_count = broadcasts_size(stream)
50
+ actual_count = new_count - original_count
51
+ else
52
+ actual_count = broadcasts_size(stream)
53
+ end
54
+
55
+ assert_equal number, actual_count, "#{number} broadcasts to #{stream} expected, but #{actual_count} were sent"
56
+ end
57
+
58
+ # Asserts that no messages have been sent to the stream.
59
+ #
60
+ # def test_no_broadcasts
61
+ # assert_no_broadcasts 'messages'
62
+ # ActionCable.server.broadcast 'messages', { text: 'hi' }
63
+ # assert_broadcasts 'messages', 1
64
+ # end
65
+ #
66
+ # If a block is passed, that block should not cause any message to be sent.
67
+ #
68
+ # def test_broadcasts_again
69
+ # assert_no_broadcasts 'messages' do
70
+ # # No job messages should be sent from this block
71
+ # end
72
+ # end
73
+ #
74
+ # Note: This assertion is simply a shortcut for:
75
+ #
76
+ # assert_broadcasts 'messages', 0, &block
77
+ #
78
+ def assert_no_broadcasts(stream, &block)
79
+ assert_broadcasts stream, 0, &block
80
+ end
81
+
82
+ # Asserts that the specified message has been sent to the stream.
83
+ #
84
+ # def test_assert_transmitted_message
85
+ # ActionCable.server.broadcast 'messages', text: 'hello'
86
+ # assert_broadcast_on('messages', text: 'hello')
87
+ # end
88
+ #
89
+ # If a block is passed, that block should cause a message with the specified data to be sent.
90
+ #
91
+ # def test_assert_broadcast_on_again
92
+ # assert_broadcast_on('messages', text: 'hello') do
93
+ # ActionCable.server.broadcast 'messages', text: 'hello'
94
+ # end
95
+ # end
96
+ #
97
+ def assert_broadcast_on(stream, data)
98
+ # Encode to JSON and back–we want to use this value to compare
99
+ # with decoded JSON.
100
+ # Comparing JSON strings doesn't work due to the order if the keys.
101
+ serialized_msg =
102
+ ActiveSupport::JSON.decode(ActiveSupport::JSON.encode(data))
103
+
104
+ new_messages = broadcasts(stream)
105
+ if block_given?
106
+ old_messages = new_messages
107
+ clear_messages(stream)
108
+
109
+ yield
110
+ new_messages = broadcasts(stream)
111
+ clear_messages(stream)
112
+
113
+ # Restore all sent messages
114
+ (old_messages + new_messages).each { |m| pubsub_adapter.broadcast(stream, m) }
115
+ end
116
+
117
+ message = new_messages.find { |msg| ActiveSupport::JSON.decode(msg) == serialized_msg }
118
+
119
+ assert message, "No messages sent with #{data} to #{stream}"
120
+ end
121
+
122
+ def pubsub_adapter # :nodoc:
123
+ ActionCable.server.pubsub
124
+ end
125
+
126
+ delegate :broadcasts, :clear_messages, to: :pubsub_adapter
127
+
128
+ private
129
+ def broadcasts_size(channel)
130
+ broadcasts(channel).size
131
+ end
132
+ end
133
+ end
@@ -1,14 +1,13 @@
1
1
  Description:
2
2
  ============
3
- Stubs out a new cable channel for the server (in Ruby) and client (in CoffeeScript).
3
+ Stubs out a new cable channel for the server (in Ruby) and client (in JavaScript).
4
4
  Pass the channel name, either CamelCased or under_scored, and an optional list of channel actions as arguments.
5
5
 
6
- Note: Turn on the cable connection in app/assets/javascripts/cable.js after generating any channels.
7
-
8
6
  Example:
9
7
  ========
10
8
  rails generate channel Chat speak
11
9
 
12
- creates a Chat channel class and CoffeeScript asset:
10
+ creates a Chat channel class, test and JavaScript asset:
13
11
  Channel: app/channels/chat_channel.rb
14
- Assets: app/assets/javascripts/channels/chat.coffee
12
+ Test: test/channels/chat_channel_test.rb
13
+ Assets: app/javascript/channels/chat_channel.js
@@ -11,15 +11,18 @@ module Rails
11
11
 
12
12
  check_class_collision suffix: "Channel"
13
13
 
14
+ hook_for :test_framework
15
+
14
16
  def create_channel_file
15
17
  template "channel.rb", File.join("app/channels", class_path, "#{file_name}_channel.rb")
16
18
 
17
19
  if options[:assets]
18
20
  if behavior == :invoke
19
- template "assets/cable.js", "app/assets/javascripts/cable.js"
21
+ template "javascript/index.js", "app/javascript/channels/index.js"
22
+ template "javascript/consumer.js", "app/javascript/channels/consumer.js"
20
23
  end
21
24
 
22
- js_template "assets/channel", File.join("app/assets/javascripts/channels", class_path, "#{file_name}")
25
+ js_template "javascript/channel", File.join("app/javascript/channels", class_path, "#{file_name}_channel")
23
26
  end
24
27
 
25
28
  generate_application_cable_files
@@ -27,7 +30,7 @@ module Rails
27
30
 
28
31
  private
29
32
  def file_name
30
- @_file_name ||= super.gsub(/_channel/i, "")
33
+ @_file_name ||= super.sub(/_channel\z/i, "")
31
34
  end
32
35
 
33
36
  # FIXME: Change these files to symlinks once RubyGems 2.5.0 is required.
@@ -1,13 +1,15 @@
1
- App.<%= class_name.underscore %> = App.cable.subscriptions.create("<%= class_name %>Channel", {
2
- connected: function() {
1
+ import consumer from "./consumer"
2
+
3
+ consumer.subscriptions.create("<%= class_name %>Channel", {
4
+ connected() {
3
5
  // Called when the subscription is ready for use on the server
4
6
  },
5
7
 
6
- disconnected: function() {
8
+ disconnected() {
7
9
  // Called when the subscription has been terminated by the server
8
10
  },
9
11
 
10
- received: function(data) {
12
+ received(data) {
11
13
  // Called when there's incoming data on the websocket for this channel
12
14
  }<%= actions.any? ? ",\n" : '' %>
13
15
  <% actions.each do |action| -%>
@@ -1,13 +1,6 @@
1
1
  // Action Cable provides the framework to deal with WebSockets in Rails.
2
2
  // You can generate new channels where WebSocket features live using the `rails generate channel` command.
3
- //
4
- //= require action_cable
5
- //= require_self
6
- //= require_tree ./channels
7
3
 
8
- (function() {
9
- this.App || (this.App = {});
4
+ import { createConsumer } from "@rails/actioncable"
10
5
 
11
- App.cable = ActionCable.createConsumer();
12
-
13
- }).call(this);
6
+ export default createConsumer()
@@ -0,0 +1,5 @@
1
+ // Load all the channels within this directory and all subdirectories.
2
+ // Channel files must be named *_channel.js.
3
+
4
+ const channels = require.context('.', true, /_channel\.js$/)
5
+ channels.keys().forEach(channels)
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TestUnit
4
+ module Generators
5
+ class ChannelGenerator < ::Rails::Generators::NamedBase
6
+ source_root File.expand_path("templates", __dir__)
7
+
8
+ check_class_collision suffix: "ChannelTest"
9
+
10
+ def create_test_files
11
+ template "channel_test.rb", File.join("test/channels", class_path, "#{file_name}_channel_test.rb")
12
+ end
13
+
14
+ private
15
+ def file_name # :doc:
16
+ @_file_name ||= super.sub(/_channel\z/i, "")
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,8 @@
1
+ require "test_helper"
2
+
3
+ class <%= class_name %>ChannelTest < ActionCable::Channel::TestCase
4
+ # test "subscribes" do
5
+ # subscribe
6
+ # assert subscription.confirmed?
7
+ # end
8
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: actioncable
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.2.2.1
4
+ version: 6.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pratik Naik
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2019-03-13 00:00:00.000000000 Z
12
+ date: 2019-12-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: actionpack
@@ -17,14 +17,14 @@ dependencies:
17
17
  requirements:
18
18
  - - '='
19
19
  - !ruby/object:Gem::Version
20
- version: 5.2.2.1
20
+ version: 6.0.2
21
21
  type: :runtime
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
25
  - - '='
26
26
  - !ruby/object:Gem::Version
27
- version: 5.2.2.1
27
+ version: 6.0.2
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: nio4r
30
30
  requirement: !ruby/object:Gem::Requirement
@@ -65,6 +65,7 @@ files:
65
65
  - CHANGELOG.md
66
66
  - MIT-LICENSE
67
67
  - README.md
68
+ - app/assets/javascripts/action_cable.js
68
69
  - lib/action_cable.rb
69
70
  - lib/action_cable/channel.rb
70
71
  - lib/action_cable/channel/base.rb
@@ -73,6 +74,7 @@ files:
73
74
  - lib/action_cable/channel/naming.rb
74
75
  - lib/action_cable/channel/periodic_timers.rb
75
76
  - lib/action_cable/channel/streams.rb
77
+ - lib/action_cable/channel/test_case.rb
76
78
  - lib/action_cable/connection.rb
77
79
  - lib/action_cable/connection/authorization.rb
78
80
  - lib/action_cable/connection/base.rb
@@ -84,6 +86,7 @@ files:
84
86
  - lib/action_cable/connection/stream_event_loop.rb
85
87
  - lib/action_cable/connection/subscriptions.rb
86
88
  - lib/action_cable/connection/tagged_logger_proxy.rb
89
+ - lib/action_cable/connection/test_case.rb
87
90
  - lib/action_cable/connection/web_socket.rb
88
91
  - lib/action_cable/engine.rb
89
92
  - lib/action_cable/gem_version.rb
@@ -104,22 +107,29 @@ files:
104
107
  - lib/action_cable/subscription_adapter/postgresql.rb
105
108
  - lib/action_cable/subscription_adapter/redis.rb
106
109
  - lib/action_cable/subscription_adapter/subscriber_map.rb
110
+ - lib/action_cable/subscription_adapter/test.rb
111
+ - lib/action_cable/test_case.rb
112
+ - lib/action_cable/test_helper.rb
107
113
  - lib/action_cable/version.rb
108
- - lib/assets/compiled/action_cable.js
109
114
  - lib/rails/generators/channel/USAGE
110
115
  - lib/rails/generators/channel/channel_generator.rb
111
116
  - lib/rails/generators/channel/templates/application_cable/channel.rb.tt
112
117
  - lib/rails/generators/channel/templates/application_cable/connection.rb.tt
113
- - lib/rails/generators/channel/templates/assets/cable.js.tt
114
- - lib/rails/generators/channel/templates/assets/channel.coffee.tt
115
- - lib/rails/generators/channel/templates/assets/channel.js.tt
116
118
  - lib/rails/generators/channel/templates/channel.rb.tt
117
- homepage: http://rubyonrails.org
119
+ - lib/rails/generators/channel/templates/javascript/channel.js.tt
120
+ - lib/rails/generators/channel/templates/javascript/consumer.js.tt
121
+ - lib/rails/generators/channel/templates/javascript/index.js.tt
122
+ - lib/rails/generators/test_unit/channel_generator.rb
123
+ - lib/rails/generators/test_unit/templates/channel_test.rb.tt
124
+ homepage: https://rubyonrails.org
118
125
  licenses:
119
126
  - MIT
120
127
  metadata:
121
- source_code_uri: https://github.com/rails/rails/tree/v5.2.2.1/actioncable
122
- changelog_uri: https://github.com/rails/rails/blob/v5.2.2.1/actioncable/CHANGELOG.md
128
+ bug_tracker_uri: https://github.com/rails/rails/issues
129
+ changelog_uri: https://github.com/rails/rails/blob/v6.0.2/actioncable/CHANGELOG.md
130
+ documentation_uri: https://api.rubyonrails.org/v6.0.2/
131
+ mailing_list_uri: https://groups.google.com/forum/#!forum/rubyonrails-talk
132
+ source_code_uri: https://github.com/rails/rails/tree/v6.0.2/actioncable
123
133
  post_install_message:
124
134
  rdoc_options: []
125
135
  require_paths:
@@ -128,14 +138,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
128
138
  requirements:
129
139
  - - ">="
130
140
  - !ruby/object:Gem::Version
131
- version: 2.2.2
141
+ version: 2.5.0
132
142
  required_rubygems_version: !ruby/object:Gem::Requirement
133
143
  requirements:
134
144
  - - ">="
135
145
  - !ruby/object:Gem::Version
136
146
  version: '0'
137
147
  requirements: []
138
- rubygems_version: 3.0.1
148
+ rubygems_version: 3.0.3
139
149
  signing_key:
140
150
  specification_version: 4
141
151
  summary: WebSocket framework for Rails.
@@ -1,601 +0,0 @@
1
- (function() {
2
- var context = this;
3
-
4
- (function() {
5
- (function() {
6
- var slice = [].slice;
7
-
8
- this.ActionCable = {
9
- INTERNAL: {
10
- "message_types": {
11
- "welcome": "welcome",
12
- "ping": "ping",
13
- "confirmation": "confirm_subscription",
14
- "rejection": "reject_subscription"
15
- },
16
- "default_mount_path": "/cable",
17
- "protocols": ["actioncable-v1-json", "actioncable-unsupported"]
18
- },
19
- WebSocket: window.WebSocket,
20
- logger: window.console,
21
- createConsumer: function(url) {
22
- var ref;
23
- if (url == null) {
24
- url = (ref = this.getConfig("url")) != null ? ref : this.INTERNAL.default_mount_path;
25
- }
26
- return new ActionCable.Consumer(this.createWebSocketURL(url));
27
- },
28
- getConfig: function(name) {
29
- var element;
30
- element = document.head.querySelector("meta[name='action-cable-" + name + "']");
31
- return element != null ? element.getAttribute("content") : void 0;
32
- },
33
- createWebSocketURL: function(url) {
34
- var a;
35
- if (url && !/^wss?:/i.test(url)) {
36
- a = document.createElement("a");
37
- a.href = url;
38
- a.href = a.href;
39
- a.protocol = a.protocol.replace("http", "ws");
40
- return a.href;
41
- } else {
42
- return url;
43
- }
44
- },
45
- startDebugging: function() {
46
- return this.debugging = true;
47
- },
48
- stopDebugging: function() {
49
- return this.debugging = null;
50
- },
51
- log: function() {
52
- var messages, ref;
53
- messages = 1 <= arguments.length ? slice.call(arguments, 0) : [];
54
- if (this.debugging) {
55
- messages.push(Date.now());
56
- return (ref = this.logger).log.apply(ref, ["[ActionCable]"].concat(slice.call(messages)));
57
- }
58
- }
59
- };
60
-
61
- }).call(this);
62
- }).call(context);
63
-
64
- var ActionCable = context.ActionCable;
65
-
66
- (function() {
67
- (function() {
68
- var bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
69
-
70
- ActionCable.ConnectionMonitor = (function() {
71
- var clamp, now, secondsSince;
72
-
73
- ConnectionMonitor.pollInterval = {
74
- min: 3,
75
- max: 30
76
- };
77
-
78
- ConnectionMonitor.staleThreshold = 6;
79
-
80
- function ConnectionMonitor(connection) {
81
- this.connection = connection;
82
- this.visibilityDidChange = bind(this.visibilityDidChange, this);
83
- this.reconnectAttempts = 0;
84
- }
85
-
86
- ConnectionMonitor.prototype.start = function() {
87
- if (!this.isRunning()) {
88
- this.startedAt = now();
89
- delete this.stoppedAt;
90
- this.startPolling();
91
- document.addEventListener("visibilitychange", this.visibilityDidChange);
92
- return ActionCable.log("ConnectionMonitor started. pollInterval = " + (this.getPollInterval()) + " ms");
93
- }
94
- };
95
-
96
- ConnectionMonitor.prototype.stop = function() {
97
- if (this.isRunning()) {
98
- this.stoppedAt = now();
99
- this.stopPolling();
100
- document.removeEventListener("visibilitychange", this.visibilityDidChange);
101
- return ActionCable.log("ConnectionMonitor stopped");
102
- }
103
- };
104
-
105
- ConnectionMonitor.prototype.isRunning = function() {
106
- return (this.startedAt != null) && (this.stoppedAt == null);
107
- };
108
-
109
- ConnectionMonitor.prototype.recordPing = function() {
110
- return this.pingedAt = now();
111
- };
112
-
113
- ConnectionMonitor.prototype.recordConnect = function() {
114
- this.reconnectAttempts = 0;
115
- this.recordPing();
116
- delete this.disconnectedAt;
117
- return ActionCable.log("ConnectionMonitor recorded connect");
118
- };
119
-
120
- ConnectionMonitor.prototype.recordDisconnect = function() {
121
- this.disconnectedAt = now();
122
- return ActionCable.log("ConnectionMonitor recorded disconnect");
123
- };
124
-
125
- ConnectionMonitor.prototype.startPolling = function() {
126
- this.stopPolling();
127
- return this.poll();
128
- };
129
-
130
- ConnectionMonitor.prototype.stopPolling = function() {
131
- return clearTimeout(this.pollTimeout);
132
- };
133
-
134
- ConnectionMonitor.prototype.poll = function() {
135
- return this.pollTimeout = setTimeout((function(_this) {
136
- return function() {
137
- _this.reconnectIfStale();
138
- return _this.poll();
139
- };
140
- })(this), this.getPollInterval());
141
- };
142
-
143
- ConnectionMonitor.prototype.getPollInterval = function() {
144
- var interval, max, min, ref;
145
- ref = this.constructor.pollInterval, min = ref.min, max = ref.max;
146
- interval = 5 * Math.log(this.reconnectAttempts + 1);
147
- return Math.round(clamp(interval, min, max) * 1000);
148
- };
149
-
150
- ConnectionMonitor.prototype.reconnectIfStale = function() {
151
- if (this.connectionIsStale()) {
152
- ActionCable.log("ConnectionMonitor detected stale connection. reconnectAttempts = " + this.reconnectAttempts + ", pollInterval = " + (this.getPollInterval()) + " ms, time disconnected = " + (secondsSince(this.disconnectedAt)) + " s, stale threshold = " + this.constructor.staleThreshold + " s");
153
- this.reconnectAttempts++;
154
- if (this.disconnectedRecently()) {
155
- return ActionCable.log("ConnectionMonitor skipping reopening recent disconnect");
156
- } else {
157
- ActionCable.log("ConnectionMonitor reopening");
158
- return this.connection.reopen();
159
- }
160
- }
161
- };
162
-
163
- ConnectionMonitor.prototype.connectionIsStale = function() {
164
- var ref;
165
- return secondsSince((ref = this.pingedAt) != null ? ref : this.startedAt) > this.constructor.staleThreshold;
166
- };
167
-
168
- ConnectionMonitor.prototype.disconnectedRecently = function() {
169
- return this.disconnectedAt && secondsSince(this.disconnectedAt) < this.constructor.staleThreshold;
170
- };
171
-
172
- ConnectionMonitor.prototype.visibilityDidChange = function() {
173
- if (document.visibilityState === "visible") {
174
- return setTimeout((function(_this) {
175
- return function() {
176
- if (_this.connectionIsStale() || !_this.connection.isOpen()) {
177
- ActionCable.log("ConnectionMonitor reopening stale connection on visibilitychange. visbilityState = " + document.visibilityState);
178
- return _this.connection.reopen();
179
- }
180
- };
181
- })(this), 200);
182
- }
183
- };
184
-
185
- now = function() {
186
- return new Date().getTime();
187
- };
188
-
189
- secondsSince = function(time) {
190
- return (now() - time) / 1000;
191
- };
192
-
193
- clamp = function(number, min, max) {
194
- return Math.max(min, Math.min(max, number));
195
- };
196
-
197
- return ConnectionMonitor;
198
-
199
- })();
200
-
201
- }).call(this);
202
- (function() {
203
- var i, message_types, protocols, ref, supportedProtocols, unsupportedProtocol,
204
- slice = [].slice,
205
- bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
206
- indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
207
-
208
- ref = ActionCable.INTERNAL, message_types = ref.message_types, protocols = ref.protocols;
209
-
210
- supportedProtocols = 2 <= protocols.length ? slice.call(protocols, 0, i = protocols.length - 1) : (i = 0, []), unsupportedProtocol = protocols[i++];
211
-
212
- ActionCable.Connection = (function() {
213
- Connection.reopenDelay = 500;
214
-
215
- function Connection(consumer) {
216
- this.consumer = consumer;
217
- this.open = bind(this.open, this);
218
- this.subscriptions = this.consumer.subscriptions;
219
- this.monitor = new ActionCable.ConnectionMonitor(this);
220
- this.disconnected = true;
221
- }
222
-
223
- Connection.prototype.send = function(data) {
224
- if (this.isOpen()) {
225
- this.webSocket.send(JSON.stringify(data));
226
- return true;
227
- } else {
228
- return false;
229
- }
230
- };
231
-
232
- Connection.prototype.open = function() {
233
- if (this.isActive()) {
234
- ActionCable.log("Attempted to open WebSocket, but existing socket is " + (this.getState()));
235
- return false;
236
- } else {
237
- ActionCable.log("Opening WebSocket, current state is " + (this.getState()) + ", subprotocols: " + protocols);
238
- if (this.webSocket != null) {
239
- this.uninstallEventHandlers();
240
- }
241
- this.webSocket = new ActionCable.WebSocket(this.consumer.url, protocols);
242
- this.installEventHandlers();
243
- this.monitor.start();
244
- return true;
245
- }
246
- };
247
-
248
- Connection.prototype.close = function(arg) {
249
- var allowReconnect, ref1;
250
- allowReconnect = (arg != null ? arg : {
251
- allowReconnect: true
252
- }).allowReconnect;
253
- if (!allowReconnect) {
254
- this.monitor.stop();
255
- }
256
- if (this.isActive()) {
257
- return (ref1 = this.webSocket) != null ? ref1.close() : void 0;
258
- }
259
- };
260
-
261
- Connection.prototype.reopen = function() {
262
- var error;
263
- ActionCable.log("Reopening WebSocket, current state is " + (this.getState()));
264
- if (this.isActive()) {
265
- try {
266
- return this.close();
267
- } catch (error1) {
268
- error = error1;
269
- return ActionCable.log("Failed to reopen WebSocket", error);
270
- } finally {
271
- ActionCable.log("Reopening WebSocket in " + this.constructor.reopenDelay + "ms");
272
- setTimeout(this.open, this.constructor.reopenDelay);
273
- }
274
- } else {
275
- return this.open();
276
- }
277
- };
278
-
279
- Connection.prototype.getProtocol = function() {
280
- var ref1;
281
- return (ref1 = this.webSocket) != null ? ref1.protocol : void 0;
282
- };
283
-
284
- Connection.prototype.isOpen = function() {
285
- return this.isState("open");
286
- };
287
-
288
- Connection.prototype.isActive = function() {
289
- return this.isState("open", "connecting");
290
- };
291
-
292
- Connection.prototype.isProtocolSupported = function() {
293
- var ref1;
294
- return ref1 = this.getProtocol(), indexOf.call(supportedProtocols, ref1) >= 0;
295
- };
296
-
297
- Connection.prototype.isState = function() {
298
- var ref1, states;
299
- states = 1 <= arguments.length ? slice.call(arguments, 0) : [];
300
- return ref1 = this.getState(), indexOf.call(states, ref1) >= 0;
301
- };
302
-
303
- Connection.prototype.getState = function() {
304
- var ref1, state, value;
305
- for (state in WebSocket) {
306
- value = WebSocket[state];
307
- if (value === ((ref1 = this.webSocket) != null ? ref1.readyState : void 0)) {
308
- return state.toLowerCase();
309
- }
310
- }
311
- return null;
312
- };
313
-
314
- Connection.prototype.installEventHandlers = function() {
315
- var eventName, handler;
316
- for (eventName in this.events) {
317
- handler = this.events[eventName].bind(this);
318
- this.webSocket["on" + eventName] = handler;
319
- }
320
- };
321
-
322
- Connection.prototype.uninstallEventHandlers = function() {
323
- var eventName;
324
- for (eventName in this.events) {
325
- this.webSocket["on" + eventName] = function() {};
326
- }
327
- };
328
-
329
- Connection.prototype.events = {
330
- message: function(event) {
331
- var identifier, message, ref1, type;
332
- if (!this.isProtocolSupported()) {
333
- return;
334
- }
335
- ref1 = JSON.parse(event.data), identifier = ref1.identifier, message = ref1.message, type = ref1.type;
336
- switch (type) {
337
- case message_types.welcome:
338
- this.monitor.recordConnect();
339
- return this.subscriptions.reload();
340
- case message_types.ping:
341
- return this.monitor.recordPing();
342
- case message_types.confirmation:
343
- return this.subscriptions.notify(identifier, "connected");
344
- case message_types.rejection:
345
- return this.subscriptions.reject(identifier);
346
- default:
347
- return this.subscriptions.notify(identifier, "received", message);
348
- }
349
- },
350
- open: function() {
351
- ActionCable.log("WebSocket onopen event, using '" + (this.getProtocol()) + "' subprotocol");
352
- this.disconnected = false;
353
- if (!this.isProtocolSupported()) {
354
- ActionCable.log("Protocol is unsupported. Stopping monitor and disconnecting.");
355
- return this.close({
356
- allowReconnect: false
357
- });
358
- }
359
- },
360
- close: function(event) {
361
- ActionCable.log("WebSocket onclose event");
362
- if (this.disconnected) {
363
- return;
364
- }
365
- this.disconnected = true;
366
- this.monitor.recordDisconnect();
367
- return this.subscriptions.notifyAll("disconnected", {
368
- willAttemptReconnect: this.monitor.isRunning()
369
- });
370
- },
371
- error: function() {
372
- return ActionCable.log("WebSocket onerror event");
373
- }
374
- };
375
-
376
- return Connection;
377
-
378
- })();
379
-
380
- }).call(this);
381
- (function() {
382
- var slice = [].slice;
383
-
384
- ActionCable.Subscriptions = (function() {
385
- function Subscriptions(consumer) {
386
- this.consumer = consumer;
387
- this.subscriptions = [];
388
- }
389
-
390
- Subscriptions.prototype.create = function(channelName, mixin) {
391
- var channel, params, subscription;
392
- channel = channelName;
393
- params = typeof channel === "object" ? channel : {
394
- channel: channel
395
- };
396
- subscription = new ActionCable.Subscription(this.consumer, params, mixin);
397
- return this.add(subscription);
398
- };
399
-
400
- Subscriptions.prototype.add = function(subscription) {
401
- this.subscriptions.push(subscription);
402
- this.consumer.ensureActiveConnection();
403
- this.notify(subscription, "initialized");
404
- this.sendCommand(subscription, "subscribe");
405
- return subscription;
406
- };
407
-
408
- Subscriptions.prototype.remove = function(subscription) {
409
- this.forget(subscription);
410
- if (!this.findAll(subscription.identifier).length) {
411
- this.sendCommand(subscription, "unsubscribe");
412
- }
413
- return subscription;
414
- };
415
-
416
- Subscriptions.prototype.reject = function(identifier) {
417
- var i, len, ref, results, subscription;
418
- ref = this.findAll(identifier);
419
- results = [];
420
- for (i = 0, len = ref.length; i < len; i++) {
421
- subscription = ref[i];
422
- this.forget(subscription);
423
- this.notify(subscription, "rejected");
424
- results.push(subscription);
425
- }
426
- return results;
427
- };
428
-
429
- Subscriptions.prototype.forget = function(subscription) {
430
- var s;
431
- this.subscriptions = (function() {
432
- var i, len, ref, results;
433
- ref = this.subscriptions;
434
- results = [];
435
- for (i = 0, len = ref.length; i < len; i++) {
436
- s = ref[i];
437
- if (s !== subscription) {
438
- results.push(s);
439
- }
440
- }
441
- return results;
442
- }).call(this);
443
- return subscription;
444
- };
445
-
446
- Subscriptions.prototype.findAll = function(identifier) {
447
- var i, len, ref, results, s;
448
- ref = this.subscriptions;
449
- results = [];
450
- for (i = 0, len = ref.length; i < len; i++) {
451
- s = ref[i];
452
- if (s.identifier === identifier) {
453
- results.push(s);
454
- }
455
- }
456
- return results;
457
- };
458
-
459
- Subscriptions.prototype.reload = function() {
460
- var i, len, ref, results, subscription;
461
- ref = this.subscriptions;
462
- results = [];
463
- for (i = 0, len = ref.length; i < len; i++) {
464
- subscription = ref[i];
465
- results.push(this.sendCommand(subscription, "subscribe"));
466
- }
467
- return results;
468
- };
469
-
470
- Subscriptions.prototype.notifyAll = function() {
471
- var args, callbackName, i, len, ref, results, subscription;
472
- callbackName = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : [];
473
- ref = this.subscriptions;
474
- results = [];
475
- for (i = 0, len = ref.length; i < len; i++) {
476
- subscription = ref[i];
477
- results.push(this.notify.apply(this, [subscription, callbackName].concat(slice.call(args))));
478
- }
479
- return results;
480
- };
481
-
482
- Subscriptions.prototype.notify = function() {
483
- var args, callbackName, i, len, results, subscription, subscriptions;
484
- subscription = arguments[0], callbackName = arguments[1], args = 3 <= arguments.length ? slice.call(arguments, 2) : [];
485
- if (typeof subscription === "string") {
486
- subscriptions = this.findAll(subscription);
487
- } else {
488
- subscriptions = [subscription];
489
- }
490
- results = [];
491
- for (i = 0, len = subscriptions.length; i < len; i++) {
492
- subscription = subscriptions[i];
493
- results.push(typeof subscription[callbackName] === "function" ? subscription[callbackName].apply(subscription, args) : void 0);
494
- }
495
- return results;
496
- };
497
-
498
- Subscriptions.prototype.sendCommand = function(subscription, command) {
499
- var identifier;
500
- identifier = subscription.identifier;
501
- return this.consumer.send({
502
- command: command,
503
- identifier: identifier
504
- });
505
- };
506
-
507
- return Subscriptions;
508
-
509
- })();
510
-
511
- }).call(this);
512
- (function() {
513
- ActionCable.Subscription = (function() {
514
- var extend;
515
-
516
- function Subscription(consumer, params, mixin) {
517
- this.consumer = consumer;
518
- if (params == null) {
519
- params = {};
520
- }
521
- this.identifier = JSON.stringify(params);
522
- extend(this, mixin);
523
- }
524
-
525
- Subscription.prototype.perform = function(action, data) {
526
- if (data == null) {
527
- data = {};
528
- }
529
- data.action = action;
530
- return this.send(data);
531
- };
532
-
533
- Subscription.prototype.send = function(data) {
534
- return this.consumer.send({
535
- command: "message",
536
- identifier: this.identifier,
537
- data: JSON.stringify(data)
538
- });
539
- };
540
-
541
- Subscription.prototype.unsubscribe = function() {
542
- return this.consumer.subscriptions.remove(this);
543
- };
544
-
545
- extend = function(object, properties) {
546
- var key, value;
547
- if (properties != null) {
548
- for (key in properties) {
549
- value = properties[key];
550
- object[key] = value;
551
- }
552
- }
553
- return object;
554
- };
555
-
556
- return Subscription;
557
-
558
- })();
559
-
560
- }).call(this);
561
- (function() {
562
- ActionCable.Consumer = (function() {
563
- function Consumer(url) {
564
- this.url = url;
565
- this.subscriptions = new ActionCable.Subscriptions(this);
566
- this.connection = new ActionCable.Connection(this);
567
- }
568
-
569
- Consumer.prototype.send = function(data) {
570
- return this.connection.send(data);
571
- };
572
-
573
- Consumer.prototype.connect = function() {
574
- return this.connection.open();
575
- };
576
-
577
- Consumer.prototype.disconnect = function() {
578
- return this.connection.close({
579
- allowReconnect: false
580
- });
581
- };
582
-
583
- Consumer.prototype.ensureActiveConnection = function() {
584
- if (!this.connection.isActive()) {
585
- return this.connection.open();
586
- }
587
- };
588
-
589
- return Consumer;
590
-
591
- })();
592
-
593
- }).call(this);
594
- }).call(this);
595
-
596
- if (typeof module === "object" && module.exports) {
597
- module.exports = ActionCable;
598
- } else if (typeof define === "function" && define.amd) {
599
- define(ActionCable);
600
- }
601
- }).call(this);