perf: ️ delay the $loading state change for quick loadings

This commit is contained in:
Christian Kaisermann 2019-11-20 22:20:15 -03:00
parent 20e5a00e2e
commit 6573f51e9b

View File

@ -1,7 +1,11 @@
import merge from 'deepmerge' import merge from 'deepmerge'
import { LocaleLoader } from '../types' import { LocaleLoader } from '../types'
import { hasLocaleDictionary, $dictionary } from '../stores/dictionary' import {
hasLocaleDictionary,
$dictionary,
addMessagesTo,
} from '../stores/dictionary'
import { getCurrentLocale } from '../stores/locale' import { getCurrentLocale } from '../stores/locale'
import { $loading } from '../stores/loading' import { $loading } from '../stores/loading'
@ -45,13 +49,6 @@ export function addLoaderToQueue(locale: string, loader: LocaleLoader) {
} }
export async function flushQueue(locale: string = getCurrentLocale()) { 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 if (!hasLocaleQueue(locale)) return
// get queue of XX-YY and XX locales // get queue of XX-YY and XX locales
@ -59,7 +56,7 @@ export async function flushQueue(locale: string = getCurrentLocale()) {
if (queue.length === 0) return if (queue.length === 0) return
removeLocaleFromQueue(locale) removeLocaleFromQueue(locale)
$loading.set(true) const loadingDelay = setTimeout(() => $loading.set(true), 200)
// todo what happens if some loader fails? // todo what happens if some loader fails?
return Promise.all(queue.map(loader => loader())) 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) partials = partials.map(partial => partial.default || partial)
removeFromLookupCache(locale) removeFromLookupCache(locale)
addMessagesTo(locale, ...partials)
$dictionary.update(d => { })
d[locale] = merge.all<any>([d[locale] || {}].concat(partials)) .then(() => {
return d clearTimeout(loadingDelay)
}) $loading.set(false)
}) })
.then(() => $loading.set(false))
} }
export function registerLocaleLoader(locale: string, loader: LocaleLoader) { export function registerLocaleLoader(locale: string, loader: LocaleLoader) {