litecable 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +20 -1
  3. data/LICENSE.txt +1 -1
  4. data/README.md +24 -22
  5. data/lib/lite_cable.rb +22 -2
  6. data/lib/lite_cable/anycable.rb +14 -12
  7. data/lib/lite_cable/broadcast_adapters.rb +35 -0
  8. data/lib/lite_cable/broadcast_adapters/any_cable.rb +11 -0
  9. data/lib/lite_cable/broadcast_adapters/base.rb +17 -0
  10. data/lib/lite_cable/broadcast_adapters/memory.rb +11 -0
  11. data/lib/lite_cable/config.rb +5 -4
  12. data/lib/lite_cable/connection/subscriptions.rb +2 -2
  13. data/lib/lite_cable/server.rb +0 -6
  14. data/lib/lite_cable/server/client_socket/base.rb +9 -15
  15. data/lib/lite_cable/version.rb +1 -1
  16. metadata +18 -76
  17. data/.gem_release.yml +0 -3
  18. data/.gitignore +0 -40
  19. data/.rubocop.yml +0 -53
  20. data/.travis.yml +0 -16
  21. data/Gemfile +0 -4
  22. data/Rakefile +0 -10
  23. data/bin/console +0 -14
  24. data/bin/setup +0 -8
  25. data/examples/sinatra/Gemfile +0 -19
  26. data/examples/sinatra/Procfile +0 -3
  27. data/examples/sinatra/README.md +0 -35
  28. data/examples/sinatra/app.rb +0 -53
  29. data/examples/sinatra/assets/app.css +0 -169
  30. data/examples/sinatra/assets/cable.js +0 -584
  31. data/examples/sinatra/assets/reset.css +0 -223
  32. data/examples/sinatra/chat.rb +0 -40
  33. data/examples/sinatra/config.ru +0 -21
  34. data/examples/sinatra/config/environment.rb +0 -18
  35. data/examples/sinatra/views/index.slim +0 -8
  36. data/examples/sinatra/views/layout.slim +0 -15
  37. data/examples/sinatra/views/login.slim +0 -8
  38. data/examples/sinatra/views/resetcss.slim +0 -224
  39. data/examples/sinatra/views/room.slim +0 -68
  40. data/litecable.gemspec +0 -39
@@ -1,3 +0,0 @@
1
- bump:
2
- file: lib/lite_cable/version.rb
3
- skip_ci: true
data/.gitignore DELETED
@@ -1,40 +0,0 @@
1
- # Numerous always-ignore extensions
2
- *.diff
3
- *.err
4
- *.orig
5
- *.log
6
- *.rej
7
- *.swo
8
- *.swp
9
- *.vi
10
- *~
11
- *.sass-cache
12
- *.iml
13
- .idea/
14
-
15
- # Sublime
16
- *.sublime-project
17
- *.sublime-workspace
18
-
19
- # OS or Editor folders
20
- .DS_Store
21
- .cache
22
- .project
23
- .settings
24
- .tmproj
25
- Thumbs.db
26
-
27
- .bundle/
28
- log/*.log
29
- *.gz
30
- pkg/
31
- spec/dummy/db/*.sqlite3
32
- spec/dummy/db/*.sqlite3-journal
33
- spec/dummy/tmp/
34
-
35
- Gemfile.lock
36
- Gemfile.local
37
- .rspec
38
- *.gem
39
- tmp/
40
- coverage/
@@ -1,53 +0,0 @@
1
- require:
2
- - standard/cop/semantic_blocks
3
- - rubocop-md
4
-
5
- inherit_gem:
6
- standard: config/base.yml
7
-
8
- AllCops:
9
- Exclude:
10
- - 'bin/*'
11
- - 'tmp/**/*'
12
- - 'Gemfile'
13
- - 'vendor/**/*'
14
- - 'gemfiles/**/*'
15
- DisplayCopNames: true
16
- TargetRubyVersion: 2.4
17
-
18
- Standard/SemanticBlocks:
19
- Enabled: false
20
-
21
- Style/FrozenStringLiteralComment:
22
- Enabled: true
23
-
24
- Style/TrailingCommaInArrayLiteral:
25
- EnforcedStyleForMultiline: no_comma
26
-
27
- Style/TrailingCommaInHashLiteral:
28
- EnforcedStyleForMultiline: no_comma
29
-
30
- Layout/AlignParameters:
31
- EnforcedStyle: with_first_parameter
32
-
33
- Lint/Void:
34
- Exclude:
35
- - '**/*.md'
36
-
37
- # See https://github.com/rubocop-hq/rubocop/issues/4222
38
- Lint/AmbiguousBlockAssociation:
39
- Exclude:
40
- - 'spec/**/*'
41
- - '**/*.md'
42
-
43
- Lint/DuplicateMethods:
44
- Exclude:
45
- - '**/*.md'
46
-
47
- Naming/FileName:
48
- Exclude:
49
- - '**/*.md'
50
-
51
- Layout/InitialIndentation:
52
- Exclude:
53
- - 'CHANGELOG.md'
@@ -1,16 +0,0 @@
1
- sudo: false
2
- language: ruby
3
- cache: bundler
4
-
5
- before_install:
6
- - gem update --system
7
- - gem install bundler -v '< 2'
8
-
9
- matrix:
10
- include:
11
- - rvm: 2.4
12
- - rvm: 2.5
13
- - rvm: 2.6
14
- - rvm: ruby-head
15
- allow_failures:
16
- - rvm: ruby-head
data/Gemfile DELETED
@@ -1,4 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in litecable.gemspec
4
- gemspec
data/Rakefile DELETED
@@ -1,10 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "bundler/gem_tasks"
4
- require "rspec/core/rake_task"
5
- require "rubocop/rake_task"
6
-
7
- RSpec::Core::RakeTask.new(:spec)
8
- RuboCop::RakeTask.new
9
-
10
- task default: [:rubocop, :spec]
@@ -1,14 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require "bundler/setup"
4
- require "litecable"
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 DELETED
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here
@@ -1,19 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "sinatra"
6
- gem "sinatra-contrib"
7
- gem "slim"
8
-
9
- gem "puma"
10
-
11
- gem "pry-byebug"
12
-
13
- # litecable deps
14
- gem "anyway_config", "~> 1.0"
15
- gem "websocket", "~> 1.2.4"
16
-
17
- # anycable
18
- gem "anycable", "~> 0.6.0"
19
- gem "redis"
@@ -1,3 +0,0 @@
1
- web: ANYCABLE=1 bundle exec puma
2
- rpc: ANYCABLE=1 bundle exec anycable
3
- ws: anycable-go --debug --host localhost --port 9293
@@ -1,35 +0,0 @@
1
- # Lite Cable Sinatra Demo
2
-
3
- Sample chat application built with [Sinatra](http://www.sinatrarb.com) and Lite Cable.
4
-
5
- ## Usage
6
-
7
- Install dependencies:
8
-
9
- ```sh
10
- bundle install
11
- ```
12
-
13
- Run server:
14
-
15
- ```sh
16
- bundle exec puma
17
- ```
18
-
19
- Open your browser at [localhost:9292](http://localhost:9292), enter your name and a chat room ID (anything you want).
20
-
21
- Then open another session (another browser, incognito window) and repeat all steps using the same room ID.
22
-
23
- Now you can chat with yourself!
24
-
25
- ## AnyCable usage
26
-
27
- This example also can be used with [AnyCable](http://anycable.io).
28
-
29
- You need [`anycable-go`](https://github.com/anycable/anycable-go) installed.
30
-
31
- Just run `Procfile` with your favourite tool ([hivemind](https://github.com/DarthSim/hivemind) or [Foreman](http://ddollar.github.io/foreman/)):
32
-
33
- ```sh
34
- hivemind
35
- ```
@@ -1,53 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "sinatra"
4
- require "sinatra/cookies"
5
-
6
- CABLE_URL = ENV["ANYCABLE"] ? "ws://localhost:9293/cable" : "/cable"
7
-
8
- class App < Sinatra::Application # :nodoc:
9
- set :public_folder, "assets"
10
-
11
- enable :sessions
12
- set :session_secret, "my_secrets"
13
-
14
- get "/" do
15
- if session[:user]
16
- slim :index
17
- else
18
- slim :login
19
- end
20
- end
21
-
22
- get "/sign_in" do
23
- slim :login
24
- end
25
-
26
- post "/sign_in" do
27
- if params["user"]
28
- session[:user] = params["user"]
29
- cookies["user"] = params["user"]
30
- redirect "/"
31
- else
32
- slim :login
33
- end
34
- end
35
-
36
- post "/rooms" do
37
- if params["id"]
38
- redirect "/rooms/#{params["id"]}"
39
- else
40
- slim :index
41
- end
42
- end
43
-
44
- get "/rooms/:id" do
45
- if session[:user]
46
- @room_id = params["id"]
47
- @user = session[:user]
48
- slim :room
49
- else
50
- slim :login
51
- end
52
- end
53
- end
@@ -1,169 +0,0 @@
1
- html, body {
2
- height: 100%;
3
- width: 100%;
4
- min-width: 300px;
5
- }
6
-
7
- body {
8
- background: #fff;
9
- color: #363636;
10
- font: 18px/30px "Arial", sans-serif;
11
- }
12
-
13
- p, div, span, a, ul, li {
14
- box-sizing: border-box;
15
- }
16
-
17
- a {
18
- text-decoration: none;
19
- color: #ff5e5e;
20
-
21
- &:visited,
22
- &:active {
23
- color: #ff5e5e;
24
- }
25
-
26
- &:hover {
27
- opacity: 0.8;
28
- }
29
- }
30
-
31
- h1 {
32
- font-size: 70px;
33
- line-height: 90px;
34
- letter-spacing: 2px;
35
- font-weight: bold;
36
- }
37
-
38
- h2 {
39
- font-size: 40px;
40
- line-height: 50px;
41
- letter-spacing: 1.5px;
42
- margin: 20px 0;
43
- }
44
-
45
- .main {
46
- height: 100%;
47
- width: 100%;
48
- padding: 0 20%;
49
- }
50
-
51
- .header {
52
- width: 100%;
53
- display: flex;
54
- justify-content: center;
55
- }
56
-
57
- input[type="text"] {
58
- width: 100%;
59
- height: 40px;
60
- line-height: 40px;
61
- display: block;
62
- margin: 0;
63
- font-size: 18px;
64
- appearance: none;
65
- box-shadow: none;
66
- border-radius: none;
67
- }
68
-
69
- button:focus, input[type="text"]:focus {
70
- outline: none;
71
- }
72
-
73
- .btn {
74
- cursor: pointer;
75
- height: 40px;
76
- text-decoration: none;
77
- padding: 0 20px;
78
- text-align: center;
79
- background: #ff5e5e;
80
- transition: opacity 200ms;
81
- color: white;
82
- font-weight: bold;
83
- font-size: 16px;
84
- letter-spacing: 1.5px;
85
- display: flex;
86
- align-items: center;
87
- justify-content: center;
88
- width: 100px;
89
- margin-top: 30px;
90
- }
91
-
92
- .btn:hover {
93
- opacity: 0.8;
94
- }
95
-
96
- .message-form {
97
- position: fixed;
98
- bottom: 0;
99
- left: 0;
100
- width: 100%;
101
- border-top: 1px #e3e3e3 solid;
102
- z-index: 10;
103
- padding: 20px 20% 20px 20%;
104
- background: white;
105
- opacity: 0.9;
106
- }
107
-
108
- .messages {
109
- display: flex;
110
- flex-direction: column;
111
- padding-bottom: 160px;
112
- }
113
-
114
- .message {
115
- display: flex;
116
- flex-direction: column;
117
- }
118
-
119
- .message.me {
120
- align-self: flex-end;
121
- }
122
-
123
- .messages .message .author {
124
- color: #ff5e5e;
125
- }
126
-
127
- .messages .message.me .author {
128
- align-self: flex-end;
129
- color: #7ed321;
130
- }
131
-
132
- .messages .message.system .author {
133
- color: #9e9e9e;
134
- }
135
-
136
- @media (max-width: 800px) and (min-width: 601px) {
137
- body {
138
- font-size: 3vw;
139
- line-height: 5vw;
140
- }
141
-
142
- h1 {
143
- font-size: 14vw;
144
- line-height: 18vw;
145
- }
146
-
147
- h2 {
148
- font-size: 5vw;
149
- line-height: 7vw;
150
- }
151
- }
152
-
153
-
154
- @media (max-width: 600px) {
155
- body {
156
- font-size: 4vw;
157
- line-height: 6vw;
158
- }
159
-
160
- h1 {
161
- font-size: 14vw;
162
- line-height: 18vw;
163
- }
164
-
165
- h2 {
166
- font-size: 10vw;
167
- line-height: 12vw;
168
- }
169
- }
@@ -1,584 +0,0 @@
1
- (function() {
2
- var slice = [].slice;
3
-
4
- this.ActionCable = {
5
- INTERNAL: {
6
- "message_types": {
7
- "welcome": "welcome",
8
- "ping": "ping",
9
- "confirmation": "confirm_subscription",
10
- "rejection": "reject_subscription"
11
- },
12
- "default_mount_path": "/cable",
13
- "protocols": ["actioncable-v1-json", "actioncable-unsupported"]
14
- },
15
- WebSocket: window.WebSocket,
16
- logger: window.console,
17
- createConsumer: function(url) {
18
- var ref;
19
- if (url == null) {
20
- url = (ref = this.getConfig("url")) != null ? ref : this.INTERNAL.default_mount_path;
21
- }
22
- return new ActionCable.Consumer(this.createWebSocketURL(url));
23
- },
24
- getConfig: function(name) {
25
- var element;
26
- element = document.head.querySelector("meta[name='action-cable-" + name + "']");
27
- return element != null ? element.getAttribute("content") : void 0;
28
- },
29
- createWebSocketURL: function(url) {
30
- var a;
31
- if (url && !/^wss?:/i.test(url)) {
32
- a = document.createElement("a");
33
- a.href = url;
34
- a.href = a.href;
35
- a.protocol = a.protocol.replace("http", "ws");
36
- return a.href;
37
- } else {
38
- return url;
39
- }
40
- },
41
- startDebugging: function() {
42
- return this.debugging = true;
43
- },
44
- stopDebugging: function() {
45
- return this.debugging = null;
46
- },
47
- log: function() {
48
- var messages, ref;
49
- messages = 1 <= arguments.length ? slice.call(arguments, 0) : [];
50
- if (this.debugging) {
51
- messages.push(Date.now());
52
- return (ref = this.logger).log.apply(ref, ["[ActionCable]"].concat(slice.call(messages)));
53
- }
54
- }
55
- };
56
-
57
- }).call(this);
58
- (function() {
59
- var bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
60
-
61
- ActionCable.ConnectionMonitor = (function() {
62
- var clamp, now, secondsSince;
63
-
64
- ConnectionMonitor.pollInterval = {
65
- min: 3,
66
- max: 30
67
- };
68
-
69
- ConnectionMonitor.staleThreshold = 6;
70
-
71
- function ConnectionMonitor(connection) {
72
- this.connection = connection;
73
- this.visibilityDidChange = bind(this.visibilityDidChange, this);
74
- this.reconnectAttempts = 0;
75
- }
76
-
77
- ConnectionMonitor.prototype.start = function() {
78
- if (!this.isRunning()) {
79
- this.startedAt = now();
80
- delete this.stoppedAt;
81
- this.startPolling();
82
- document.addEventListener("visibilitychange", this.visibilityDidChange);
83
- return ActionCable.log("ConnectionMonitor started. pollInterval = " + (this.getPollInterval()) + " ms");
84
- }
85
- };
86
-
87
- ConnectionMonitor.prototype.stop = function() {
88
- if (this.isRunning()) {
89
- this.stoppedAt = now();
90
- this.stopPolling();
91
- document.removeEventListener("visibilitychange", this.visibilityDidChange);
92
- return ActionCable.log("ConnectionMonitor stopped");
93
- }
94
- };
95
-
96
- ConnectionMonitor.prototype.isRunning = function() {
97
- return (this.startedAt != null) && (this.stoppedAt == null);
98
- };
99
-
100
- ConnectionMonitor.prototype.recordPing = function() {
101
- return this.pingedAt = now();
102
- };
103
-
104
- ConnectionMonitor.prototype.recordConnect = function() {
105
- this.reconnectAttempts = 0;
106
- this.recordPing();
107
- delete this.disconnectedAt;
108
- return ActionCable.log("ConnectionMonitor recorded connect");
109
- };
110
-
111
- ConnectionMonitor.prototype.recordDisconnect = function() {
112
- this.disconnectedAt = now();
113
- return ActionCable.log("ConnectionMonitor recorded disconnect");
114
- };
115
-
116
- ConnectionMonitor.prototype.startPolling = function() {
117
- this.stopPolling();
118
- return this.poll();
119
- };
120
-
121
- ConnectionMonitor.prototype.stopPolling = function() {
122
- return clearTimeout(this.pollTimeout);
123
- };
124
-
125
- ConnectionMonitor.prototype.poll = function() {
126
- return this.pollTimeout = setTimeout((function(_this) {
127
- return function() {
128
- _this.reconnectIfStale();
129
- return _this.poll();
130
- };
131
- })(this), this.getPollInterval());
132
- };
133
-
134
- ConnectionMonitor.prototype.getPollInterval = function() {
135
- var interval, max, min, ref;
136
- ref = this.constructor.pollInterval, min = ref.min, max = ref.max;
137
- interval = 5 * Math.log(this.reconnectAttempts + 1);
138
- return Math.round(clamp(interval, min, max) * 1000);
139
- };
140
-
141
- ConnectionMonitor.prototype.reconnectIfStale = function() {
142
- if (this.connectionIsStale()) {
143
- 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");
144
- this.reconnectAttempts++;
145
- if (this.disconnectedRecently()) {
146
- return ActionCable.log("ConnectionMonitor skipping reopening recent disconnect");
147
- } else {
148
- ActionCable.log("ConnectionMonitor reopening");
149
- return this.connection.reopen();
150
- }
151
- }
152
- };
153
-
154
- ConnectionMonitor.prototype.connectionIsStale = function() {
155
- var ref;
156
- return secondsSince((ref = this.pingedAt) != null ? ref : this.startedAt) > this.constructor.staleThreshold;
157
- };
158
-
159
- ConnectionMonitor.prototype.disconnectedRecently = function() {
160
- return this.disconnectedAt && secondsSince(this.disconnectedAt) < this.constructor.staleThreshold;
161
- };
162
-
163
- ConnectionMonitor.prototype.visibilityDidChange = function() {
164
- if (document.visibilityState === "visible") {
165
- return setTimeout((function(_this) {
166
- return function() {
167
- if (_this.connectionIsStale() || !_this.connection.isOpen()) {
168
- ActionCable.log("ConnectionMonitor reopening stale connection on visibilitychange. visbilityState = " + document.visibilityState);
169
- return _this.connection.reopen();
170
- }
171
- };
172
- })(this), 200);
173
- }
174
- };
175
-
176
- now = function() {
177
- return new Date().getTime();
178
- };
179
-
180
- secondsSince = function(time) {
181
- return (now() - time) / 1000;
182
- };
183
-
184
- clamp = function(number, min, max) {
185
- return Math.max(min, Math.min(max, number));
186
- };
187
-
188
- return ConnectionMonitor;
189
-
190
- })();
191
-
192
- }).call(this);
193
- (function() {
194
- var i, message_types, protocols, ref, supportedProtocols, unsupportedProtocol,
195
- slice = [].slice,
196
- bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
197
- 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; };
198
-
199
- ref = ActionCable.INTERNAL, message_types = ref.message_types, protocols = ref.protocols;
200
-
201
- supportedProtocols = 2 <= protocols.length ? slice.call(protocols, 0, i = protocols.length - 1) : (i = 0, []), unsupportedProtocol = protocols[i++];
202
-
203
- ActionCable.Connection = (function() {
204
- Connection.reopenDelay = 500;
205
-
206
- function Connection(consumer) {
207
- this.consumer = consumer;
208
- this.open = bind(this.open, this);
209
- this.subscriptions = this.consumer.subscriptions;
210
- this.monitor = new ActionCable.ConnectionMonitor(this);
211
- this.disconnected = true;
212
- }
213
-
214
- Connection.prototype.send = function(data) {
215
- if (this.isOpen()) {
216
- this.webSocket.send(JSON.stringify(data));
217
- return true;
218
- } else {
219
- return false;
220
- }
221
- };
222
-
223
- Connection.prototype.open = function() {
224
- if (this.isActive()) {
225
- ActionCable.log("Attempted to open WebSocket, but existing socket is " + (this.getState()));
226
- return false;
227
- } else {
228
- ActionCable.log("Opening WebSocket, current state is " + (this.getState()) + ", subprotocols: " + protocols);
229
- if (this.webSocket != null) {
230
- this.uninstallEventHandlers();
231
- }
232
- this.webSocket = new ActionCable.WebSocket(this.consumer.url, protocols);
233
- this.installEventHandlers();
234
- this.monitor.start();
235
- return true;
236
- }
237
- };
238
-
239
- Connection.prototype.close = function(arg) {
240
- var allowReconnect, ref1;
241
- allowReconnect = (arg != null ? arg : {
242
- allowReconnect: true
243
- }).allowReconnect;
244
- if (!allowReconnect) {
245
- this.monitor.stop();
246
- }
247
- if (this.isActive()) {
248
- return (ref1 = this.webSocket) != null ? ref1.close() : void 0;
249
- }
250
- };
251
-
252
- Connection.prototype.reopen = function() {
253
- var error;
254
- ActionCable.log("Reopening WebSocket, current state is " + (this.getState()));
255
- if (this.isActive()) {
256
- try {
257
- return this.close();
258
- } catch (error1) {
259
- error = error1;
260
- return ActionCable.log("Failed to reopen WebSocket", error);
261
- } finally {
262
- ActionCable.log("Reopening WebSocket in " + this.constructor.reopenDelay + "ms");
263
- setTimeout(this.open, this.constructor.reopenDelay);
264
- }
265
- } else {
266
- return this.open();
267
- }
268
- };
269
-
270
- Connection.prototype.getProtocol = function() {
271
- var ref1;
272
- return (ref1 = this.webSocket) != null ? ref1.protocol : void 0;
273
- };
274
-
275
- Connection.prototype.isOpen = function() {
276
- return this.isState("open");
277
- };
278
-
279
- Connection.prototype.isActive = function() {
280
- return this.isState("open", "connecting");
281
- };
282
-
283
- Connection.prototype.isProtocolSupported = function() {
284
- var ref1;
285
- return ref1 = this.getProtocol(), indexOf.call(supportedProtocols, ref1) >= 0;
286
- };
287
-
288
- Connection.prototype.isState = function() {
289
- var ref1, states;
290
- states = 1 <= arguments.length ? slice.call(arguments, 0) : [];
291
- return ref1 = this.getState(), indexOf.call(states, ref1) >= 0;
292
- };
293
-
294
- Connection.prototype.getState = function() {
295
- var ref1, state, value;
296
- for (state in WebSocket) {
297
- value = WebSocket[state];
298
- if (value === ((ref1 = this.webSocket) != null ? ref1.readyState : void 0)) {
299
- return state.toLowerCase();
300
- }
301
- }
302
- return null;
303
- };
304
-
305
- Connection.prototype.installEventHandlers = function() {
306
- var eventName, handler;
307
- for (eventName in this.events) {
308
- handler = this.events[eventName].bind(this);
309
- this.webSocket["on" + eventName] = handler;
310
- }
311
- };
312
-
313
- Connection.prototype.uninstallEventHandlers = function() {
314
- var eventName;
315
- for (eventName in this.events) {
316
- this.webSocket["on" + eventName] = function() {};
317
- }
318
- };
319
-
320
- Connection.prototype.events = {
321
- message: function(event) {
322
- var identifier, message, ref1, type;
323
- if (!this.isProtocolSupported()) {
324
- return;
325
- }
326
- ref1 = JSON.parse(event.data), identifier = ref1.identifier, message = ref1.message, type = ref1.type;
327
- switch (type) {
328
- case message_types.welcome:
329
- this.monitor.recordConnect();
330
- return this.subscriptions.reload();
331
- case message_types.ping:
332
- return this.monitor.recordPing();
333
- case message_types.confirmation:
334
- return this.subscriptions.notify(identifier, "connected");
335
- case message_types.rejection:
336
- return this.subscriptions.reject(identifier);
337
- default:
338
- return this.subscriptions.notify(identifier, "received", message);
339
- }
340
- },
341
- open: function() {
342
- ActionCable.log("WebSocket onopen event, using '" + (this.getProtocol()) + "' subprotocol");
343
- this.disconnected = false;
344
- if (!this.isProtocolSupported()) {
345
- ActionCable.log("Protocol is unsupported. Stopping monitor and disconnecting.");
346
- return this.close({
347
- allowReconnect: false
348
- });
349
- }
350
- },
351
- close: function(event) {
352
- ActionCable.log("WebSocket onclose event");
353
- if (this.disconnected) {
354
- return;
355
- }
356
- this.disconnected = true;
357
- this.monitor.recordDisconnect();
358
- return this.subscriptions.notifyAll("disconnected", {
359
- willAttemptReconnect: this.monitor.isRunning()
360
- });
361
- },
362
- error: function() {
363
- return ActionCable.log("WebSocket onerror event");
364
- }
365
- };
366
-
367
- return Connection;
368
-
369
- })();
370
-
371
- }).call(this);
372
- (function() {
373
- var slice = [].slice;
374
-
375
- ActionCable.Subscriptions = (function() {
376
- function Subscriptions(consumer) {
377
- this.consumer = consumer;
378
- this.subscriptions = [];
379
- }
380
-
381
- Subscriptions.prototype.create = function(channelName, mixin) {
382
- var channel, params, subscription;
383
- channel = channelName;
384
- params = typeof channel === "object" ? channel : {
385
- channel: channel
386
- };
387
- subscription = new ActionCable.Subscription(this.consumer, params, mixin);
388
- return this.add(subscription);
389
- };
390
-
391
- Subscriptions.prototype.add = function(subscription) {
392
- this.subscriptions.push(subscription);
393
- this.consumer.ensureActiveConnection();
394
- this.notify(subscription, "initialized");
395
- this.sendCommand(subscription, "subscribe");
396
- return subscription;
397
- };
398
-
399
- Subscriptions.prototype.remove = function(subscription) {
400
- this.forget(subscription);
401
- if (!this.findAll(subscription.identifier).length) {
402
- this.sendCommand(subscription, "unsubscribe");
403
- }
404
- return subscription;
405
- };
406
-
407
- Subscriptions.prototype.reject = function(identifier) {
408
- var i, len, ref, results, subscription;
409
- ref = this.findAll(identifier);
410
- results = [];
411
- for (i = 0, len = ref.length; i < len; i++) {
412
- subscription = ref[i];
413
- this.forget(subscription);
414
- this.notify(subscription, "rejected");
415
- results.push(subscription);
416
- }
417
- return results;
418
- };
419
-
420
- Subscriptions.prototype.forget = function(subscription) {
421
- var s;
422
- this.subscriptions = (function() {
423
- var i, len, ref, results;
424
- ref = this.subscriptions;
425
- results = [];
426
- for (i = 0, len = ref.length; i < len; i++) {
427
- s = ref[i];
428
- if (s !== subscription) {
429
- results.push(s);
430
- }
431
- }
432
- return results;
433
- }).call(this);
434
- return subscription;
435
- };
436
-
437
- Subscriptions.prototype.findAll = function(identifier) {
438
- var i, len, ref, results, s;
439
- ref = this.subscriptions;
440
- results = [];
441
- for (i = 0, len = ref.length; i < len; i++) {
442
- s = ref[i];
443
- if (s.identifier === identifier) {
444
- results.push(s);
445
- }
446
- }
447
- return results;
448
- };
449
-
450
- Subscriptions.prototype.reload = function() {
451
- var i, len, ref, results, subscription;
452
- ref = this.subscriptions;
453
- results = [];
454
- for (i = 0, len = ref.length; i < len; i++) {
455
- subscription = ref[i];
456
- results.push(this.sendCommand(subscription, "subscribe"));
457
- }
458
- return results;
459
- };
460
-
461
- Subscriptions.prototype.notifyAll = function() {
462
- var args, callbackName, i, len, ref, results, subscription;
463
- callbackName = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : [];
464
- ref = this.subscriptions;
465
- results = [];
466
- for (i = 0, len = ref.length; i < len; i++) {
467
- subscription = ref[i];
468
- results.push(this.notify.apply(this, [subscription, callbackName].concat(slice.call(args))));
469
- }
470
- return results;
471
- };
472
-
473
- Subscriptions.prototype.notify = function() {
474
- var args, callbackName, i, len, results, subscription, subscriptions;
475
- subscription = arguments[0], callbackName = arguments[1], args = 3 <= arguments.length ? slice.call(arguments, 2) : [];
476
- if (typeof subscription === "string") {
477
- subscriptions = this.findAll(subscription);
478
- } else {
479
- subscriptions = [subscription];
480
- }
481
- results = [];
482
- for (i = 0, len = subscriptions.length; i < len; i++) {
483
- subscription = subscriptions[i];
484
- results.push(typeof subscription[callbackName] === "function" ? subscription[callbackName].apply(subscription, args) : void 0);
485
- }
486
- return results;
487
- };
488
-
489
- Subscriptions.prototype.sendCommand = function(subscription, command) {
490
- var identifier;
491
- identifier = subscription.identifier;
492
- return this.consumer.send({
493
- command: command,
494
- identifier: identifier
495
- });
496
- };
497
-
498
- return Subscriptions;
499
-
500
- })();
501
-
502
- }).call(this);
503
- (function() {
504
- ActionCable.Subscription = (function() {
505
- var extend;
506
-
507
- function Subscription(consumer, params, mixin) {
508
- this.consumer = consumer;
509
- if (params == null) {
510
- params = {};
511
- }
512
- this.identifier = JSON.stringify(params);
513
- extend(this, mixin);
514
- }
515
-
516
- Subscription.prototype.perform = function(action, data) {
517
- if (data == null) {
518
- data = {};
519
- }
520
- data.action = action;
521
- return this.send(data);
522
- };
523
-
524
- Subscription.prototype.send = function(data) {
525
- return this.consumer.send({
526
- command: "message",
527
- identifier: this.identifier,
528
- data: JSON.stringify(data)
529
- });
530
- };
531
-
532
- Subscription.prototype.unsubscribe = function() {
533
- return this.consumer.subscriptions.remove(this);
534
- };
535
-
536
- extend = function(object, properties) {
537
- var key, value;
538
- if (properties != null) {
539
- for (key in properties) {
540
- value = properties[key];
541
- object[key] = value;
542
- }
543
- }
544
- return object;
545
- };
546
-
547
- return Subscription;
548
-
549
- })();
550
-
551
- }).call(this);
552
- (function() {
553
- ActionCable.Consumer = (function() {
554
- function Consumer(url) {
555
- this.url = url;
556
- this.subscriptions = new ActionCable.Subscriptions(this);
557
- this.connection = new ActionCable.Connection(this);
558
- }
559
-
560
- Consumer.prototype.send = function(data) {
561
- return this.connection.send(data);
562
- };
563
-
564
- Consumer.prototype.connect = function() {
565
- return this.connection.open();
566
- };
567
-
568
- Consumer.prototype.disconnect = function() {
569
- return this.connection.close({
570
- allowReconnect: false
571
- });
572
- };
573
-
574
- Consumer.prototype.ensureActiveConnection = function() {
575
- if (!this.connection.isActive()) {
576
- return this.connection.open();
577
- }
578
- };
579
-
580
- return Consumer;
581
-
582
- })();
583
-
584
- }).call(this);