Skip to content

Commit

Permalink
Merge pull request #64 from piyushk96/master
Browse files Browse the repository at this point in the history
Flush queue on refresh token request error
  • Loading branch information
newsiberian authored Jun 22, 2024
2 parents e3c7bf4 + b376ea5 commit 539f93e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/queuing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ export class OperationQueuing {
return requestCopy.observable;
}

public consumeQueue(): void {
public consumeQueue(err?: Error): void {
this.queuedRequests.forEach(request => {
request.forward(request.operation).subscribe(request.subscriber);
if (err) request.subscriber.error(err);
else request.forward(request.operation).subscribe(request.subscriber);
});

this.queuedRequests = [];
Expand Down
14 changes: 9 additions & 5 deletions src/tokenRefreshLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export class TokenRefreshLink<AccessTokenPayloadType = string> extends ApolloLin
if (typeof forward !== 'function') {
throw new Error('[Token Refresh Link]: Token Refresh Link is a non-terminating link and should not be the last in the composed chain');
}

return fromPromise(
this.isTokenValidOrUndefined(operation).then((tokenValidOrUndefined) => {
// If token does not exist, which could mean that this is a not registered
Expand All @@ -184,12 +184,16 @@ export class TokenRefreshLink<AccessTokenPayloadType = string> extends ApolloLin
}
return token;
})
.then((payload) => this.handleFetch(payload, operation))
.catch((error) => this.handleError(error, operation))
.finally(() => {
.then((payload) => {
this.handleFetch(payload, operation);
this.fetching = false;
this.queue.consumeQueue();
});
})
.catch((error) => {
this.handleError(error, operation);
this.fetching = false;
this.queue.consumeQueue(error);
})
}

return this.queue.enqueueRequest({
Expand Down

0 comments on commit 539f93e

Please sign in to comment.