Skip to content

Commit 531d4cd

Browse files
authored
Merge pull request rails#46125 from mansakondo/fix-pr-45738
Fix rails#45738
2 parents 876977d + 3a6ab41 commit 531d4cd

File tree

5 files changed

+55
-17
lines changed

5 files changed

+55
-17
lines changed

actioncable/app/assets/javascripts/action_cable.js

Lines changed: 14 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

actioncable/app/assets/javascripts/actioncable.esm.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ class Connection {
203203
isActive() {
204204
return this.isState("open", "connecting");
205205
}
206-
reconnectAttempted() {
206+
triedToReconnect() {
207207
return this.monitor.reconnectAttempts > 0;
208208
}
209209
isProtocolSupported() {
@@ -245,6 +245,9 @@ Connection.prototype.events = {
245245
const {identifier: identifier, message: message, reason: reason, reconnect: reconnect, type: type} = JSON.parse(event.data);
246246
switch (type) {
247247
case message_types.welcome:
248+
if (this.triedToReconnect()) {
249+
this.reconnectAttempted = true;
250+
}
248251
this.monitor.recordConnect();
249252
return this.subscriptions.reload();
250253

@@ -259,9 +262,16 @@ Connection.prototype.events = {
259262

260263
case message_types.confirmation:
261264
this.subscriptions.confirmSubscription(identifier);
262-
return this.subscriptions.notify(identifier, "connected", {
263-
reconnected: this.reconnectAttempted()
264-
});
265+
if (this.reconnectAttempted) {
266+
this.reconnectAttempted = false;
267+
return this.subscriptions.notify(identifier, "connected", {
268+
reconnected: true
269+
});
270+
} else {
271+
return this.subscriptions.notify(identifier, "connected", {
272+
reconnected: false
273+
});
274+
}
265275

266276
case message_types.rejection:
267277
return this.subscriptions.reject(identifier);

actioncable/app/assets/javascripts/actioncable.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@
197197
isActive() {
198198
return this.isState("open", "connecting");
199199
}
200-
reconnectAttempted() {
200+
triedToReconnect() {
201201
return this.monitor.reconnectAttempts > 0;
202202
}
203203
isProtocolSupported() {
@@ -237,6 +237,9 @@
237237
const {identifier: identifier, message: message, reason: reason, reconnect: reconnect, type: type} = JSON.parse(event.data);
238238
switch (type) {
239239
case message_types.welcome:
240+
if (this.triedToReconnect()) {
241+
this.reconnectAttempted = true;
242+
}
240243
this.monitor.recordConnect();
241244
return this.subscriptions.reload();
242245

@@ -251,9 +254,16 @@
251254

252255
case message_types.confirmation:
253256
this.subscriptions.confirmSubscription(identifier);
254-
return this.subscriptions.notify(identifier, "connected", {
255-
reconnected: this.reconnectAttempted()
256-
});
257+
if (this.reconnectAttempted) {
258+
this.reconnectAttempted = false;
259+
return this.subscriptions.notify(identifier, "connected", {
260+
reconnected: true
261+
});
262+
} else {
263+
return this.subscriptions.notify(identifier, "connected", {
264+
reconnected: false
265+
});
266+
}
257267

258268
case message_types.rejection:
259269
return this.subscriptions.reject(identifier);

actioncable/app/javascript/action_cable/connection.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class Connection {
8181
return this.isState("open", "connecting")
8282
}
8383

84-
reconnectAttempted() {
84+
triedToReconnect() {
8585
return this.monitor.reconnectAttempts > 0
8686
}
8787

@@ -129,6 +129,9 @@ Connection.prototype.events = {
129129
const {identifier, message, reason, reconnect, type} = JSON.parse(event.data)
130130
switch (type) {
131131
case message_types.welcome:
132+
if (this.triedToReconnect()) {
133+
this.reconnectAttempted = true
134+
}
132135
this.monitor.recordConnect()
133136
return this.subscriptions.reload()
134137
case message_types.disconnect:
@@ -138,7 +141,12 @@ Connection.prototype.events = {
138141
return this.monitor.recordPing()
139142
case message_types.confirmation:
140143
this.subscriptions.confirmSubscription(identifier)
141-
return this.subscriptions.notify(identifier, "connected", {reconnected: this.reconnectAttempted()})
144+
if (this.reconnectAttempted) {
145+
this.reconnectAttempted = false
146+
return this.subscriptions.notify(identifier, "connected", {reconnected: true})
147+
} else {
148+
return this.subscriptions.notify(identifier, "connected", {reconnected: false})
149+
}
142150
case message_types.rejection:
143151
return this.subscriptions.reject(identifier)
144152
default:

actioncable/test/javascript/src/unit/subscription_test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ module("ActionCable.Subscription", () => {
1414

1515
consumerTest("#connected callback", ({server, consumer, assert, done}) => {
1616
const subscription = consumer.subscriptions.create("chat", {
17-
connected() {
17+
connected({reconnected}) {
1818
assert.ok(true)
19+
assert.notOk(reconnected)
1920
done()
2021
}
2122
})
@@ -26,13 +27,12 @@ module("ActionCable.Subscription", () => {
2627
consumerTest("#connected callback (handling reconnects)", ({server, consumer, connection, monitor, assert, done}) => {
2728
const subscription = consumer.subscriptions.create("chat", {
2829
connected({reconnected}) {
29-
assert.ok(true, reconnected)
30+
assert.ok(reconnected)
3031
done()
3132
}
3233
})
3334

3435
monitor.reconnectAttempts = 1
35-
assert.ok(connection.reconnectAttempted())
3636
server.broadcastTo(subscription, {message_type: "welcome"})
3737
server.broadcastTo(subscription, {message_type: "confirmation"})
3838
})

0 commit comments

Comments
 (0)