feat: 🎸 add $loading indicator store

This commit is contained in:
Christian Kaisermann 2019-11-20 02:05:34 -03:00
parent 5ada5beed3
commit bd2b3501e9
3 changed files with 28 additions and 26 deletions

View File

@ -1,7 +1,5 @@
<script> <script>
import { locale } from './dist/i18n.mjs' import { locale, loading } from './dist/i18n.mjs'
let loading = false
$: { $: {
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
@ -10,4 +8,4 @@
} }
</script> </script>
<slot {loading} /> <slot loading={$loading} locale={$locale} />

View File

@ -9,7 +9,7 @@ import {
lower, lower,
getClientLocale, getClientLocale,
getGenericLocaleFrom, getGenericLocaleFrom,
getGenericLocalesFrom, getLocalesFrom,
} from './utils' } from './utils'
import { MessageObject, Formatter } from './types' import { MessageObject, Formatter } from './types'
import { import {
@ -19,9 +19,11 @@ import {
getTimeFormatter, getTimeFormatter,
} from './formatters' } from './formatters'
const $loading = writable(false)
let currentLocale: string let currentLocale: string
let currentDictionary: Record<string, Record<string, any>> let currentDictionary: Record<string, Record<string, any>>
const dictQueue: Record<string, any[]> = {} const dictQueue: Record<string, (() => Promise<any>)[]> = {}
const hasLocale = (locale: string) => locale in currentDictionary const hasLocale = (locale: string) => locale in currentDictionary
@ -36,7 +38,7 @@ async function registerLocaleLoader(locale: string, loader: any) {
dictQueue[locale].push(loader) dictQueue[locale].push(loader)
} }
function getAvailableLocale(locale: string): string | null { function getAvailableLocale(locale: string): string | null {
if (locale in currentDictionary || locale in dictQueue || locale == null) return locale if (locale in currentDictionary || locale == null) return locale
return getAvailableLocale(getGenericLocaleFrom(locale)) return getAvailableLocale(getGenericLocaleFrom(locale))
} }
@ -83,9 +85,7 @@ const formatMessage: Formatter = (id, options = {}) => {
if (!message) { if (!message) {
console.warn( console.warn(
`[svelte-i18n] The message "${id}" was not found in "${getGenericLocalesFrom(locale).join( `[svelte-i18n] The message "${id}" was not found in "${getLocalesFrom(locale).join('", "')}".`
'", "'
)}".`
) )
return defaultValue || id return defaultValue || id
} }
@ -107,7 +107,7 @@ $dictionary.subscribe(newDictionary => (currentDictionary = newDictionary))
function loadLocale(localeToLoad: string) { function loadLocale(localeToLoad: string) {
return Promise.all( return Promise.all(
getGenericLocalesFrom(localeToLoad).map(localeItem => getLocalesFrom(localeToLoad).map(localeItem =>
flushLocaleQueue(localeItem) flushLocaleQueue(localeItem)
.then(() => [localeItem, { err: undefined }]) .then(() => [localeItem, { err: undefined }])
.catch(e => [localeItem, { err: e }]) .catch(e => [localeItem, { err: e }])
@ -117,16 +117,21 @@ function loadLocale(localeToLoad: string) {
async function flushLocaleQueue(locale: string = currentLocale) { async function flushLocaleQueue(locale: string = currentLocale) {
if (!(locale in dictQueue)) return if (!(locale in dictQueue)) return
return Promise.all(dictQueue[locale].map((loader: any) => loader())).then(partials => {
dictQueue[locale] = []
partials = partials.map(partial => partial.default || partial) $loading.set(true)
invalidateLookupCache(locale)
$dictionary.update(d => { return Promise.all(dictQueue[locale].map((loader: any) => loader()))
d[locale] = merge.all<any>([d[locale] || {}].concat(partials)) .then(partials => {
return d dictQueue[locale] = []
partials = partials.map(partial => partial.default || partial)
invalidateLookupCache(locale)
$dictionary.update(d => {
d[locale] = merge.all<any>([d[locale] || {}].concat(partials))
return d
})
}) })
}) .then(() => $loading.set(false))
} }
const $locale = writable(null) const $locale = writable(null)
@ -134,10 +139,7 @@ const localeSet = $locale.set
$locale.set = (newLocale: string): void | Promise<void> => { $locale.set = (newLocale: string): void | Promise<void> => {
const locale = getAvailableLocale(newLocale) const locale = getAvailableLocale(newLocale)
if (locale) { if (locale) {
if (locale in dictQueue && dictQueue[locale].length > 0) { return flushLocaleQueue(newLocale).then(() => localeSet(newLocale))
return flushLocaleQueue(locale).then(() => localeSet(newLocale))
}
return localeSet(newLocale)
} }
throw Error(`[svelte-i18n] Locale "${newLocale}" not found.`) throw Error(`[svelte-i18n] Locale "${newLocale}" not found.`)
@ -153,6 +155,7 @@ const defineMessages = (i: Record<string, MessageObject>) => i
export { customFormats, addCustomFormats } from './formatters' export { customFormats, addCustomFormats } from './formatters'
export { export {
$loading as loading,
$locale as locale, $locale as locale,
$dictionary as dictionary, $dictionary as dictionary,
$format as _, $format as _,
@ -161,6 +164,7 @@ export {
getClientLocale, getClientLocale,
defineMessages, defineMessages,
loadLocale as preloadLocale, loadLocale as preloadLocale,
registerLocaleLoader, registerLocaleLoader as register,
flushLocaleQueue, flushLocaleQueue as waitLocale,
merge,
} }

View File

@ -8,7 +8,7 @@ export function getGenericLocaleFrom(locale: string) {
return index > 0 ? locale.slice(0, index) : null return index > 0 ? locale.slice(0, index) : null
} }
export function getGenericLocalesFrom(locale: string) { export function getLocalesFrom(locale: string) {
return locale.split('-').map((_, i, arr) => arr.slice(0, i + 1).join('-')) return locale.split('-').map((_, i, arr) => arr.slice(0, i + 1).join('-'))
} }