Skip to content

Commit

Permalink
🎨 Use promise.all to get arweaveId
Browse files Browse the repository at this point in the history
  • Loading branch information
AuroraHuang22 committed Apr 9, 2024
1 parent 79cd883 commit 9cfa8a3
Showing 1 changed file with 30 additions and 32 deletions.
62 changes: 30 additions & 32 deletions components/IscnUploadForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -836,40 +836,38 @@ export default class IscnUploadForm extends Vue {
if (this.epubMetadataList?.find(epubMetadata => epubMetadata.thumbnailArweaveId)) {
return;
}
// eslint-disable-next-line no-restricted-syntax
for (const file of this.fileRecords) {
if (IMAGE_MIME_TYPES.includes(file.fileType)) {
const existingData =
this.sentArweaveTransactionInfo.get(file.ipfsHash) || {}
if (existingData.arweaveId) {
this.epubMetadataList.push({
thumbnailIpfsHash: file.ipfsHash,
thumbnailArweaveId: existingData.arweaveId,
})
return
}
let {transactionHash} = existingData;
if (!transactionHash) {
// eslint-disable-next-line no-await-in-loop
transactionHash = await this.sendArweaveFeeTx(file);
}
// eslint-disable-next-line no-await-in-loop
const arweaveId = await this.uploadFileAndGetArweaveId(
file,
transactionHash,
)
if (arweaveId) {
this.epubMetadataList.push({
thumbnailIpfsHash: file.ipfsHash,
thumbnailArweaveId: arweaveId,
})
this.sentArweaveTransactionInfo.set(file.ipfsHash, { transactionHash, arweaveId });
return
}
return
const uploadPromises = this.fileRecords
.filter(file => IMAGE_MIME_TYPES.includes(file.fileType))
.map(async (file) => {
const existingData = this.sentArweaveTransactionInfo.get(file.ipfsHash) || {};
if (existingData.arweaveId) {
return {
thumbnailIpfsHash: file.ipfsHash,
thumbnailArweaveId: existingData.arweaveId,
};
}
}
let { transactionHash } = existingData;
if (!transactionHash) {
transactionHash = await this.sendArweaveFeeTx(file);
}
const arweaveId = await this.uploadFileAndGetArweaveId(file, transactionHash);
if (arweaveId) {
this.sentArweaveTransactionInfo.set(file.ipfsHash, { transactionHash, arweaveId });
return {
thumbnailIpfsHash: file.ipfsHash,
thumbnailArweaveId: arweaveId,
};
}
return false
});
const results = await Promise.all(uploadPromises);
this.epubMetadataList.push(...results.filter(Boolean));
}
async onSubmit() {
Expand Down

0 comments on commit 9cfa8a3

Please sign in to comment.