Skip to content

Commit

Permalink
Handle disconnection
Browse files Browse the repository at this point in the history
  • Loading branch information
manueliglesias committed Feb 20, 2020
1 parent d33fb79 commit 84b5ab3
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions packages/pubsub/src/Providers/AWSAppSyncRealTimeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ export class AWSAppSyncRealTimeProvider extends AbstractPubSubProvider {
if (type === MESSAGE_TYPES.GQL_CONNECTION_KEEP_ALIVE) {
clearTimeout(this.keepAliveTimeoutId);
this.keepAliveTimeoutId = setTimeout(
this._timeoutDisconnect.bind(this),
this._errorDisconnect.bind(this, 'Timeout disconnect'),
this.keepAliveTimeout
);
return;
Expand Down Expand Up @@ -492,16 +492,15 @@ export class AWSAppSyncRealTimeProvider extends AbstractPubSubProvider {
}
}

private _timeoutDisconnect() {
private _errorDisconnect(msg: string) {
this.subscriptionObserverMap.forEach(({ observer }) => {
if (!observer.closed) {
observer.error({
errors: [{ ...new GraphQLError(`Timeout disconnect`) }],
errors: [{ ...new GraphQLError(msg) }],
});
observer.complete();
}
});
this.subscriptionObserverMap = new Map();
this.subscriptionObserverMap.clear();
if (this.awsRealTimeSocket) {
this.awsRealTimeSocket.close();
}
Expand Down Expand Up @@ -661,7 +660,10 @@ export class AWSAppSyncRealTimeProvider extends AbstractPubSubProvider {
this.awsRealTimeSocket.onmessage = this._handleIncomingSubscriptionMessage.bind(
this
);
this.awsRealTimeSocket.onerror = logger.debug.bind(logger);
this.awsRealTimeSocket.onerror = err => {
logger.debug(err);
this._errorDisconnect('Connection closed');
};
res('Cool, connected to AWS AppSyncRealTime');
return;
}
Expand Down

0 comments on commit 84b5ab3

Please sign in to comment.