From ae927972ae21e558749df6dade0f6349bd34b284 Mon Sep 17 00:00:00 2001 From: HF Date: Mon, 27 Jun 2022 12:31:41 +0200 Subject: [PATCH] promisify some Tile creation --- src/core/Tile.js | 46 +++++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/src/core/Tile.js b/src/core/Tile.js index 4549b67..0c8a888 100644 --- a/src/core/Tile.js +++ b/src/core/Tile.js @@ -358,30 +358,38 @@ export async function createZoomedTile( const startTime = Date.now(); const na = []; - for (let dy = 0; dy < TILE_ZOOM_LEVEL; dy += 1) { - for (let dx = 0; dx < TILE_ZOOM_LEVEL; dx += 1) { - // eslint-disable-next-line max-len - const chunkfile = `${canvasTileFolder}/${z + 1}/${x * TILE_ZOOM_LEVEL + dx}/${y * TILE_ZOOM_LEVEL + dy}.png`; + + const prom = async (dx, dy) => { + // eslint-disable-next-line max-len + const chunkfile = `${canvasTileFolder}/${z + 1}/${x * TILE_ZOOM_LEVEL + dx}/${y * TILE_ZOOM_LEVEL + dy}.png`; + try { if (!fs.existsSync(chunkfile)) { na.push([dx, dy]); - continue; - } - try { - const chunk = await sharp(chunkfile).removeAlpha().raw().toBuffer(); - addShrunkenSubtileToTile( - TILE_ZOOM_LEVEL, - [dx, dy], - chunk, - tileRGBBuffer, - ); - } catch (error) { - console.error( - // eslint-disable-next-line max-len - `Tiling: Error on createZoomedTile on chunk ${chunkfile}: ${error.message}`, - ); + return; } + const chunk = await sharp(chunkfile).removeAlpha().raw().toBuffer(); + addShrunkenSubtileToTile( + TILE_ZOOM_LEVEL, + [dx, dy], + chunk, + tileRGBBuffer, + ); + } catch (error) { + na.push([dx, dy]); + console.error( + // eslint-disable-next-line max-len + `Tiling: Error on createZoomedTile on chunk ${chunkfile}: ${error.message}`, + ); + } + }; + + const promises = []; + for (let dy = 0; dy < TILE_ZOOM_LEVEL; dy += 1) { + for (let dx = 0; dx < TILE_ZOOM_LEVEL; dx += 1) { + promises.push(prom(dx, dy)); } } + await Promise.all(promises); if (na.length !== TILE_ZOOM_LEVEL * TILE_ZOOM_LEVEL) { na.forEach((element) => {