Skip to content

Commit

Permalink
feat: Change Promise.allSettled to Promise.any back.
Browse files Browse the repository at this point in the history
Signed-off-by: Eugene Panteleymonchuk <[email protected]>
  • Loading branch information
panteleymonchuk committed Apr 26, 2024
1 parent cb106e3 commit 0a22aed
Showing 1 changed file with 187 additions and 136 deletions.
323 changes: 187 additions & 136 deletions api/src/utils/nova/searchExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,182 +24,246 @@ export class SearchExecutor {

if (searchQuery.blockId) {
promises.push(
this.executeQuery(
this.apiService.block(searchQuery.blockId),
(response) => {
if (response.block) {
promisesResult = {
block: response.block,
};
}
},
"Block fetch failed",
),
new Promise((resolve, reject) => {
this.apiService
.block(searchQuery.blockId)
.then((response) => {
if (response.block) {
promisesResult = {
block: response.block,
};
resolve();
} else {
reject(new Error("Block fetch failed"));
}
})
.catch((_) => {
reject(new Error("Block fetch failed"));
});
}),
);
}

if (searchQuery.outputId) {
promises.push(
this.executeQuery(
this.apiService.outputDetails(searchQuery.outputId),
(response) => {
if (response.output) {
promisesResult = {
output: response.output,
};
}
},
"Output fetch failed",
),
new Promise((resolve, reject) => {
this.apiService
.outputDetails(searchQuery.outputId)
.then((response) => {
if (response.output) {
promisesResult = {
output: response.output,
};
resolve();
} else {
reject(new Error("Output fetch failed"));
}
})
.catch((_) => {
reject(new Error("Output fetch failed"));
});
}),
);
}

if (searchQuery.accountId) {
promises.push(
this.executeQuery(
this.apiService.accountDetails(searchQuery.accountId),
(response) => {
if (response.accountOutputDetails) {
promisesResult = {
accountId: searchQuery.accountId,
};
}
},
"Account id fetch failed",
),
new Promise((resolve, reject) => {
this.apiService
.accountDetails(searchQuery.accountId)
.then((response) => {
if (response.accountOutputDetails) {
promisesResult = {
accountId: searchQuery.accountId,
};
resolve();
} else {
reject(new Error("Account id fetch failed"));
}
})
.catch((_) => {
reject(new Error("Account id fetch failed"));
});
}),
);
}

if (searchQuery.nftId) {
promises.push(
this.executeQuery(
this.apiService.nftDetails(searchQuery.nftId),
(response) => {
if (response.nftOutputDetails) {
promisesResult = {
nftId: searchQuery.nftId,
};
}
},
"Nft id fetch failed",
),
new Promise((resolve, reject) => {
this.apiService
.nftDetails(searchQuery.nftId)
.then((response) => {
if (response.nftOutputDetails) {
promisesResult = {
nftId: searchQuery.nftId,
};
resolve();
} else {
reject(new Error("Nft id fetch failed"));
}
})
.catch((_) => {
reject(new Error("Nft id fetch failed"));
});
}),
);
}

if (searchQuery.anchorId) {
promises.push(
this.executeQuery(
this.apiService.anchorDetails(searchQuery.anchorId),
(response) => {
if (response.anchorOutputDetails) {
promisesResult = {
anchorId: searchQuery.anchorId,
};
}
},
"Anchor id fetch failed",
),
new Promise((resolve, reject) => {
this.apiService
.anchorDetails(searchQuery.anchorId)
.then((response) => {
if (response.anchorOutputDetails) {
promisesResult = {
anchorId: searchQuery.anchorId,
};
} else {
reject(new Error("Anchor id fetch failed"));
}
})
.catch((_) => {
reject(new Error("Anchor id fetch failed"));
});
}),
);
}

if (searchQuery.delegationId) {
promises.push(
this.executeQuery(
this.apiService.delegationDetails(searchQuery.delegationId),
(response) => {
if (response.output) {
promisesResult = {
output: response.output,
};
return response.output;
}
},
"Delegation id fetch failed",
),
new Promise((resolve, reject) => {
this.apiService
.delegationDetails(searchQuery.delegationId)
.then((response) => {
if (response.output) {
promisesResult = {
output: response.output,
};
resolve();
} else {
reject(new Error("Anchor id fetch failed"));
}
})
.catch((_) => {
reject(new Error("Anchor id fetch failed"));
});
}),
);
}

if (searchQuery.transactionId) {
promises.push(
this.executeQuery(
this.apiService.transactionIncludedBlock(searchQuery.transactionId),
(response) => {
if (response.block) {
promisesResult = {
transactionBlock: response.block,
};
}
},
"Transaction included block fetch failed",
),
new Promise((resolve, reject) => {
this.apiService
.transactionIncludedBlock(searchQuery.transactionId)
.then((response) => {
if (response.block) {
promisesResult = {
transactionBlock: response.block,
};
resolve();
} else {
reject(new Error("Transaction included block fetch failed"));
}
})
.catch((_) => {
reject(new Error("Transaction included block fetch failed"));
});
}),
);
}

if (searchQuery.foundryId) {
promises.push(
this.executeQuery(
this.apiService.foundryDetails(searchQuery.foundryId),
(response) => {
if (response.foundryDetails) {
promisesResult = {
foundryId: searchQuery.foundryId,
};
}
},
"Foundry details fetch failed",
),
new Promise((resolve, reject) => {
this.apiService
.foundryDetails(searchQuery.foundryId)
.then((response) => {
if (response.foundryDetails) {
promisesResult = {
foundryId: searchQuery.foundryId,
};
resolve();
} else {
reject(new Error("Foundry details fetch failed"));
}
})
.catch((_) => {
reject(new Error("Foundry details fetch failed"));
});
}),
);
}

if (searchQuery.slotIndex) {
promises.push(
this.executeQuery(
this.apiService.getSlotCommitment(searchQuery.slotIndex),
(_) => {
if (searchQuery.slotIndex) {
promisesResult = {
slotIndex: String(searchQuery.slotIndex),
};
}
},
"Slot commitment fetch failed",
),
new Promise((resolve, reject) => {
this.apiService
.getSlotCommitment(searchQuery.slotIndex)
.then((response) => {
if (searchQuery.slotIndex) {
promisesResult = {
slotIndex: String(searchQuery.slotIndex),
};
resolve();
} else {
reject(new Error("Slot commitment fetch failed"));
}
})
.catch((_) => {
reject(new Error("Slot commitment fetch failed"));
});
}),
);
}

if (searchQuery.slotCommitmentId) {
promises.push(
this.executeQuery(
this.apiService.getCommitment(searchQuery.slotCommitmentId),
(result) => {
if (result?.slot?.slot) {
promisesResult = {
slotIndex: String(result.slot.slot),
};
}
},
"Slot commitment fetch failed",
),
new Promise((resolve, reject) => {
this.apiService
.getCommitment(searchQuery.slotCommitmentId)
.then((response) => {
if (response?.slot?.slot) {
promisesResult = {
slotIndex: String(response.slot.slot),
};
resolve();
} else {
reject(new Error("Slot commitment fetch failed"));
}
})
.catch((_) => {
reject(new Error("Slot commitment fetch failed"));
});
}),
);
}

if (searchQuery.tag) {
promises.push(
this.executeQuery(
this.apiService.taggedOutputs(searchQuery.tag),
(response) => {
if (!response.basicOutputs.error || !response.nftOutputs.error) {
promisesResult = {
taggedOutputs: response,
};
}
},
"Tagged details fetch failed",
),
new Promise((resolve, reject) => {
this.apiService
.taggedOutputs(searchQuery.tag)
.then((response) => {
if (!response.basicOutputs.error || !response.nftOutputs.error) {
promisesResult = {
taggedOutputs: response,
};
resolve();
} else {
reject(new Error("Tagged details fetch failed"));
}
})
.catch((_) => {
reject(new Error("Tagged details fetch failed"));
});
}),
);
}

await Promise.allSettled(promises);
await Promise.any(promises).catch((_) => {});

if (promisesResult !== null) {
return promisesResult;
Expand All @@ -213,17 +277,4 @@ export class SearchExecutor {

return { message: "Nothing found" };
}

private async executeQuery<T>(query: Promise<T>, successHandler: (result: T) => void, failureMessage: string): Promise<void> {
try {
const result = await query;
if (result) {
return successHandler(result);
}

throw new Error(failureMessage);
} catch {
throw new Error(`${failureMessage}`);
}
}
}

0 comments on commit 0a22aed

Please sign in to comment.