diff --git a/packages/build/src/utils/cachePromises.js b/packages/build/src/utils/cachePromises.js new file mode 100644 index 000000000..1cdf548dd --- /dev/null +++ b/packages/build/src/utils/cachePromises.js @@ -0,0 +1,31 @@ +/* + Copyright 2020-2021 Lowdefy, Inc + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +function cachedPromises(getter) { + const cache = {}; + + function getCachedPromise(key) { + if (cache[key]) { + return Promise.resolve(cache[key]); + } + cache[key] = getter(key); + return Promise.resolve(cache[key]); + } + + return getCachedPromise; +} + +export default cachedPromises; diff --git a/packages/build/src/utils/files/readConfigFile.js b/packages/build/src/utils/files/readConfigFile.js index 0c8f915df..bf24e34e7 100644 --- a/packages/build/src/utils/files/readConfigFile.js +++ b/packages/build/src/utils/files/readConfigFile.js @@ -17,15 +17,13 @@ import path from 'path'; import { readFile } from '@lowdefy/node-utils'; +import cachedPromises from '../cachePromises'; + function createReadConfigFile({ configDirectory }) { - const files = {}; async function readConfigFile(filePath) { - if (files[filePath]) return files[filePath]; - const fileContent = readFile(path.resolve(configDirectory, filePath)); - files[filePath] = fileContent; - return fileContent; + return readFile(path.resolve(configDirectory, filePath)); } - return readConfigFile; + return cachedPromises(readConfigFile); } export default createReadConfigFile; diff --git a/packages/build/src/utils/meta/getMeta.js b/packages/build/src/utils/meta/getMeta.js index 2c0ae8b54..43620f814 100644 --- a/packages/build/src/utils/meta/getMeta.js +++ b/packages/build/src/utils/meta/getMeta.js @@ -26,13 +26,14 @@ Steps to fetch meta */ import { type as typeHelper } from '@lowdefy/helpers'; + +import cachedPromises from '../cachePromises'; import createFetchMetaCache from './fetchMetaCache'; import createWriteMetaCache from './writeMetaCache'; import metaLocations from './metaLocations'; import fetchMetaUrl from './fetchMetaUrl'; function createGetMeta({ blocksServerUrl, cacheDirectory, types }) { - const memoisedMeta = {}; const allMetaLocations = { ...metaLocations({ blocksServerUrl }), ...types, @@ -40,10 +41,6 @@ function createGetMeta({ blocksServerUrl, cacheDirectory, types }) { const fetchMetaCache = createFetchMetaCache({ cacheDirectory }); const writeMetaCache = createWriteMetaCache({ cacheDirectory }); async function getMeta(type) { - if (memoisedMeta[type]) { - return memoisedMeta[type]; - } - const location = allMetaLocations[type]; if (!location) { throw new Error( @@ -57,7 +54,6 @@ function createGetMeta({ blocksServerUrl, cacheDirectory, types }) { if (cacheMeta) { meta = await fetchMetaCache(location); if (meta) { - memoisedMeta[type] = meta; return meta; } } @@ -65,7 +61,6 @@ function createGetMeta({ blocksServerUrl, cacheDirectory, types }) { meta = await fetchMetaUrl({ location, type }); // TODO: implement Ajv schema check. Use testAjvSchema func from @lowdefy/ajv if (meta && typeHelper.isString(meta.category) && meta.moduleFederation) { - memoisedMeta[type] = meta; if (cacheMeta) { await writeMetaCache({ location, meta }); } @@ -76,7 +71,7 @@ function createGetMeta({ blocksServerUrl, cacheDirectory, types }) { ); } - return getMeta; + return cachedPromises(getMeta); } export default createGetMeta; diff --git a/packages/cli/src/commands/dev/shell/index.html b/packages/cli/src/commands/dev/shell/index.html index 116524e9d..6da56b115 100644 --- a/packages/cli/src/commands/dev/shell/index.html +++ b/packages/cli/src/commands/dev/shell/index.html @@ -13,46 +13,46 @@ See the License for the specific language governing permissions and limitations under the License. --> - - -
- - -