From 66972d4b1536b53d33c7974eb0fc059c0d0cc46c Mon Sep 17 00:00:00 2001 From: Christian Kaisermann Date: Thu, 21 Nov 2019 23:33:31 -0300 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20flush=20use=20the=20same?= =?UTF-8?q?=20promise=20if=20it=20wasn't=20resolved=20yet?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/client/includes/loaderQueue.ts | 55 ++++++++++++++++-------------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/src/client/includes/loaderQueue.ts b/src/client/includes/loaderQueue.ts index 4db9bab..988c541 100644 --- a/src/client/includes/loaderQueue.ts +++ b/src/client/includes/loaderQueue.ts @@ -7,10 +7,10 @@ import { import { getCurrentLocale } from '../stores/locale' import { $isLoading } from '../stores/loading' -import { removeFromLookupCache } from './lookup' -import { getLocalesFrom } from './utils' +import { getAllFallbackLocales } from './utils' -const loaderQueue: Record> = {} +type Queue = Set +const loaderQueue: Record = {} function createLocaleQueue(locale: string) { loaderQueue[locale] = new Set() @@ -24,20 +24,18 @@ function getLocaleQueue(locale: string) { return loaderQueue[locale] } -function getLocalesQueue(locale: string) { - return getLocalesFrom(locale) +function getLocalesQueues(locale: string) { + return getAllFallbackLocales(locale) .reverse() - .reduce( - (acc, localeItem) => - getLocaleQueue(localeItem) - ? acc.concat([...getLocaleQueue(localeItem)]) - : acc, - [] - ) + .map<[string, MessagesLoader[]]>(localeItem => { + const queue = getLocaleQueue(localeItem) + return [localeItem, queue ? [...queue] : []] + }) + .filter(([, queue]) => queue.length > 0) } export function hasLocaleQueue(locale: string) { - return getLocalesFrom(locale) + return getAllFallbackLocales(locale) .reverse() .some(getLocaleQueue) } @@ -46,28 +44,33 @@ export function addLoaderToQueue(locale: string, loader: MessagesLoader) { loaderQueue[locale].add(loader) } +const activeLocaleFlushes: { [key: string]: Promise } = {} export async function flushQueue(locale: string = getCurrentLocale()) { if (!hasLocaleQueue(locale)) return + if (activeLocaleFlushes[locale]) return activeLocaleFlushes[locale] // get queue of XX-YY and XX locales - const queue = getLocalesQueue(locale) - if (queue.length === 0) return + const queues = getLocalesQueues(locale) + if (queues.length === 0) return removeLocaleFromQueue(locale) const loadingDelay = setTimeout(() => $isLoading.set(true), 200) - // todo what happens if some loader fails? - return Promise.all(queue.map(loader => loader())) - .then(partials => { - partials = partials.map(partial => partial.default || partial) + // TODO what happens if some loader fails + activeLocaleFlushes[locale] = Promise.all( + queues.map(([locale, queue]) => { + return Promise.all(queue.map(loader => loader())).then(partials => { + partials = partials.map(partial => partial.default || partial) + addMessages(locale, ...partials) + }) + }) + ).then(() => { + clearTimeout(loadingDelay) + $isLoading.set(false) + delete activeLocaleFlushes[locale] + }) - removeFromLookupCache(locale) - addMessages(locale, ...partials) - }) - .then(() => { - clearTimeout(loadingDelay) - $isLoading.set(false) - }) + return activeLocaleFlushes[locale] } export function registerLocaleLoader(locale: string, loader: MessagesLoader) {