From b11cee7ef0cc47718a9c71a597e45dee422ff709 Mon Sep 17 00:00:00 2001 From: ddelange <14880945+ddelange@users.noreply.github.com> Date: Mon, 5 Aug 2024 13:06:46 +0200 Subject: [PATCH] Simplify delay behaviour, add JSDoc comments --- client/src/utils/download.js | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/client/src/utils/download.js b/client/src/utils/download.js index c234b3f..1fd6739 100644 --- a/client/src/utils/download.js +++ b/client/src/utils/download.js @@ -1,9 +1,17 @@ -// adapted from https://github.com/sindresorhus/multi-download/blob/v4.0.0/index.js +// Adapted from https://github.com/sindresorhus/multi-download/blob/v4.0.0/index.js // to take File as input https://developer.mozilla.org/en-US/docs/Web/API/File + +/** + * Creates a promise that resolves after the specified number of milliseconds + */ const delay = milliseconds => new Promise(resolve => { setTimeout(resolve, milliseconds); }); +/** + * Downloads a single file + * @param file - An instance of the File type representing the file to download + */ const download = async (file) => { const a = document.createElement('a'); const url = URL.createObjectURL(file); @@ -12,21 +20,22 @@ const download = async (file) => { a.style.display = 'none'; document.body.append(a); a.click(); - - // Chrome requires the timeout - await delay(100); + await delay(100); // for Chrome a.remove(); - URL.revokeObjectURL(url); }; +/** + * Initiates multiple file downloads with a constant delay between each one + * @param files - An array of instances of the File type representing the files to download + */ const multiDownload = async (files) => { if (!files) { throw new Error('`files` required'); - } + }; - for (const [index, file] of files.entries()) { - await delay(index * 1000); // eslint-disable-line no-await-in-loop + for (const file of files) { + await delay(1000); download(file); } -} +};