From 6573f51e9b817db0c77f158945572f4ba14c71fc Mon Sep 17 00:00:00 2001 From: Christian Kaisermann Date: Wed, 20 Nov 2019 22:20:15 -0300 Subject: [PATCH] =?UTF-8?q?perf:=20=E2=9A=A1=EF=B8=8F=20delay=20the=20$loa?= =?UTF-8?q?ding=20state=20change=20for=20quick=20loadings?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/client/includes/loaderQueue.ts | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/src/client/includes/loaderQueue.ts b/src/client/includes/loaderQueue.ts index 25bfb87..7cd16d5 100644 --- a/src/client/includes/loaderQueue.ts +++ b/src/client/includes/loaderQueue.ts @@ -1,7 +1,11 @@ import merge from 'deepmerge' import { LocaleLoader } from '../types' -import { hasLocaleDictionary, $dictionary } from '../stores/dictionary' +import { + hasLocaleDictionary, + $dictionary, + addMessagesTo, +} from '../stores/dictionary' import { getCurrentLocale } from '../stores/locale' import { $loading } from '../stores/loading' @@ -45,13 +49,6 @@ export function addLoaderToQueue(locale: string, loader: LocaleLoader) { } export async function flushQueue(locale: string = getCurrentLocale()) { - if (locale == null) { - throw new Error( - `[svelte-i18n] Invalid locale into "waitLocale": ${JSON.stringify( - locale - )}` - ) - } if (!hasLocaleQueue(locale)) return // get queue of XX-YY and XX locales @@ -59,7 +56,7 @@ export async function flushQueue(locale: string = getCurrentLocale()) { if (queue.length === 0) return removeLocaleFromQueue(locale) - $loading.set(true) + const loadingDelay = setTimeout(() => $loading.set(true), 200) // todo what happens if some loader fails? return Promise.all(queue.map(loader => loader())) @@ -67,13 +64,12 @@ export async function flushQueue(locale: string = getCurrentLocale()) { partials = partials.map(partial => partial.default || partial) removeFromLookupCache(locale) - - $dictionary.update(d => { - d[locale] = merge.all([d[locale] || {}].concat(partials)) - return d - }) + addMessagesTo(locale, ...partials) + }) + .then(() => { + clearTimeout(loadingDelay) + $loading.set(false) }) - .then(() => $loading.set(false)) } export function registerLocaleLoader(locale: string, loader: LocaleLoader) {