Better manage subscriptionCounters (#14608)

Before this change:
- unsubscribe() was not called for a disconnection
- It seems that WebSocketClient calls connected() and reconnected().
  subscriptionCounters were incremented twice for a single reconnection,
  first from connected() and second from reconnected()

This might be a an additional change to
https://github.com/tootsuite/mastodon/pull/14579
to recover subscriptions after a reconnect.
This commit is contained in:
Daigo 3 Dango 2020-08-24 12:06:45 +00:00 committed by GitHub
parent 46210a65d1
commit 9669557be1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 2 additions and 10 deletions

View File

@ -112,11 +112,10 @@ const sharedCallbacks = {
}, },
disconnected () { disconnected () {
subscriptions.forEach(({ onDisconnect }) => onDisconnect()); subscriptions.forEach(subscription => unsubscribe(subscription));
}, },
reconnected () { reconnected () {
subscriptions.forEach(subscription => subscribe(subscription));
}, },
}; };
@ -252,15 +251,8 @@ const createConnection = (streamingAPIBaseURL, accessToken, channelName, { conne
const es = new EventSource(`${streamingAPIBaseURL}/api/v1/streaming/${channelName}?${params.join('&')}`); const es = new EventSource(`${streamingAPIBaseURL}/api/v1/streaming/${channelName}?${params.join('&')}`);
let firstConnect = true;
es.onopen = () => { es.onopen = () => {
if (firstConnect) { connected();
firstConnect = false;
connected();
} else {
reconnected();
}
}; };
KNOWN_EVENT_TYPES.forEach(type => { KNOWN_EVENT_TYPES.forEach(type => {