Skip to content

Commit

Permalink
(fix:@aws-amplify/pubsub) Fix for unsubscription new subscription rac…
Browse files Browse the repository at this point in the history
…e condition (aws-amplify#4956)

* Fix for unsubscription new subscription race condition

* Update packages/pubsub/src/Providers/AWSAppSyncRealTimeProvider.ts
  • Loading branch information
elorzafe authored and Ashish-Nanda committed Feb 20, 2020
1 parent ee535da commit bc238a7
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions packages/pubsub/src/Providers/AWSAppSyncRealTimeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,22 +372,27 @@ export class AWSAppSyncRealTimeProvider extends AbstractPubSubProvider {

private _removeSubscriptionObserver(subscriptionId) {
this.subscriptionObserverMap.delete(subscriptionId);
if (this.subscriptionObserverMap.size === 0) {
// Socket could be sending data to unsubscribe so is required to wait until is flushed
this._closeSocketWhenFlushed();
}

// Verifying 1000ms after removing subscription in case there are new subscription unmount/mount
setTimeout(this._closeSocketIfRequired.bind(this), 1000);
}

private _closeSocketWhenFlushed() {
logger.debug('closing WebSocket...');
clearTimeout(this.keepAliveTimeoutId);
private _closeSocketIfRequired() {
if (this.subscriptionObserverMap.size > 0) {
// Active subscriptions on the WebSocket
return;
}

if (!this.awsRealTimeSocket) {
this.socketStatus = SOCKET_STATUS.CLOSED;
return;
}
if (this.awsRealTimeSocket.bufferedAmount > 0) {
setTimeout(this._closeSocketWhenFlushed.bind(this), 1000);
// Still data on the WebSocket
setTimeout(this._closeSocketIfRequired.bind(this), 1000);
} else {
logger.debug('closing WebSocket...');
clearTimeout(this.keepAliveTimeoutId);
const tempSocket = this.awsRealTimeSocket;
tempSocket.close(1000);
this.awsRealTimeSocket = null;
Expand Down

0 comments on commit bc238a7

Please sign in to comment.