Skip to content

Commit

Permalink
refactor: moves cleanup in isValidRespond
Browse files Browse the repository at this point in the history
  • Loading branch information
Gancho Radkov committed Dec 21, 2023
1 parent 0731755 commit 8f7b610
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions packages/sign-client/src/controllers/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,13 +381,6 @@ export class Engine extends IEngine {

public respond: IEngine["respond"] = async (params) => {
await this.isInitialized();

// if the session is already disconnected, we can't respond to the request so we need to delete it
await this.isValidSessionTopic(params.topic).catch((error) => {
this.cleanupAfterResponse(params);
throw error;
});

await this.isValidRespond(params);
const { topic, response } = params;
const { id } = response;
Expand Down Expand Up @@ -1051,7 +1044,7 @@ export class Engine extends IEngine {
};

private cleanupAfterResponse = (params: EngineTypes.RespondParams) => {
this.deletePendingSessionRequest(params.response.id, { message: "fulfilled", code: 0 });
this.deletePendingSessionRequest(params.response?.id, { message: "fulfilled", code: 0 });
// intentionally delay the emitting of the next pending request a bit
setTimeout(() => {
this.sessionRequestQueue.state = ENGINE_QUEUE_STATES.idle;
Expand Down Expand Up @@ -1400,7 +1393,11 @@ export class Engine extends IEngine {
throw new Error(message);
}
const { topic, response } = params;
await this.isValidSessionTopic(topic);
// if the session is already disconnected, we can't respond to the request so we need to delete it
await this.isValidSessionTopic(topic).catch((error) => {
this.cleanupAfterResponse(params);
throw error;
});
if (!isValidResponse(response)) {
const { message } = getInternalError(
"MISSING_OR_INVALID",
Expand Down

0 comments on commit 8f7b610

Please sign in to comment.