fix: 🐛 ignore loadingDelay for the initialLocale

Fixes #53

 Closes: Closes #53
This commit is contained in:
Christian Kaisermann 2020-03-06 14:08:05 -03:00
parent 9930c1a7c3
commit 9d82a98e8d
4 changed files with 57 additions and 42 deletions

View File

@ -5,8 +5,6 @@ import {
addMessages, addMessages,
} from '../stores/dictionary' } from '../stores/dictionary'
import { getRelatedLocalesOf } from '../stores/locale' import { getRelatedLocalesOf } from '../stores/locale'
import { $isLoading } from '../stores/loading'
import { getOptions } from '../configs'
type Queue = Set<MessagesLoader> type Queue = Set<MessagesLoader>
const loaderQueue: Record<string, Queue> = {} const loaderQueue: Record<string, Queue> = {}
@ -55,11 +53,6 @@ export function flush(locale: string) {
// istanbul ignore if // istanbul ignore if
if (queues.length === 0) return if (queues.length === 0) return
const loadingDelay = setTimeout(
() => $isLoading.set(true),
getOptions().loadingDelay
)
// TODO what happens if some loader fails // TODO what happens if some loader fails
activeLocaleFlushes[locale] = Promise.all( activeLocaleFlushes[locale] = Promise.all(
queues.map(([locale, queue]) => { queues.map(([locale, queue]) => {
@ -70,8 +63,6 @@ export function flush(locale: string) {
}) })
}) })
).then(() => { ).then(() => {
clearTimeout(loadingDelay)
$isLoading.set(false)
delete activeLocaleFlushes[locale] delete activeLocaleFlushes[locale]
}) })

View File

@ -4,6 +4,7 @@ import { flush, hasLocaleQueue } from '../includes/loaderQueue'
import { getOptions } from '../configs' import { getOptions } from '../configs'
import { getClosestAvailableLocale } from './dictionary' import { getClosestAvailableLocale } from './dictionary'
import { $isLoading } from './loading'
let current: string let current: string
const $locale = writable(null) const $locale = writable(null)
@ -59,7 +60,26 @@ $locale.subscribe((newLocale: string) => {
const localeSet = $locale.set const localeSet = $locale.set
$locale.set = (newLocale: string): void | Promise<void> => { $locale.set = (newLocale: string): void | Promise<void> => {
if (getClosestAvailableLocale(newLocale) && hasLocaleQueue(newLocale)) { if (getClosestAvailableLocale(newLocale) && hasLocaleQueue(newLocale)) {
return flush(newLocale).then(() => localeSet(newLocale)) const loadingDelay = getOptions().loadingDelay
let loadingTimer: number
// if there's no current locale, we don't wait to set isLoading to true
// because it would break pages when loading the initial locale
if (getCurrentLocale() != null && loadingDelay) {
loadingTimer = window.setTimeout(() => $isLoading.set(true), loadingDelay)
} else {
$isLoading.set(true)
}
return flush(newLocale)
.then(() => {
localeSet(newLocale)
})
.finally(() => {
clearTimeout(loadingTimer)
$isLoading.set(false)
})
} }
return localeSet(newLocale) return localeSet(newLocale)
} }

View File

@ -1,5 +1,3 @@
import { get } from 'svelte/store'
import { import {
hasLocaleQueue, hasLocaleQueue,
flush, flush,
@ -7,8 +5,6 @@ import {
resetQueues, resetQueues,
} from '../../../src/runtime/includes/loaderQueue' } from '../../../src/runtime/includes/loaderQueue'
import { getMessageFromDictionary } from '../../../src/runtime/stores/dictionary' import { getMessageFromDictionary } from '../../../src/runtime/stores/dictionary'
import { $isLoading } from '../../../src/runtime/stores/loading'
import { getOptions } from '../../../src/runtime/configs'
beforeEach(() => { beforeEach(() => {
resetQueues() resetQueues()
@ -54,30 +50,3 @@ test('consecutive flushes return the same promise', async () => {
expect(flushB).toStrictEqual(flushA) expect(flushB).toStrictEqual(flushA)
expect(flushC).toStrictEqual(flushA) expect(flushC).toStrictEqual(flushA)
}) })
test('should set loading to true if passed min delay and false after loading', () => {
registerLocaleLoader(
'en',
() =>
new Promise(res =>
setTimeout(() => res({}), getOptions().loadingDelay * 2)
)
)
const flushPromise = flush('en')
return new Promise((res, rej) => {
setTimeout(() => {
if (get($isLoading) === true) return res()
return rej('$isLoading should be "true"')
}, getOptions().loadingDelay)
}).then(() => {
flushPromise.then(
() =>
new Promise((res, rej) => {
if (get($isLoading) === false) return res()
return rej('$isLoading should be "false" after loading')
})
)
})
})

View File

@ -10,7 +10,7 @@ import {
isRelatedLocale, isRelatedLocale,
} from '../../../src/runtime/stores/locale' } from '../../../src/runtime/stores/locale'
import { getOptions, init } from '../../../src/runtime/configs' import { getOptions, init } from '../../../src/runtime/configs'
import { register } from '../../../src/runtime' import { register, isLoading } from '../../../src/runtime'
import { hasLocaleQueue } from '../../../src/runtime/includes/loaderQueue' import { hasLocaleQueue } from '../../../src/runtime/includes/loaderQueue'
beforeEach(() => { beforeEach(() => {
@ -132,3 +132,38 @@ test('should flush the queue of the locale when changing the store value', async
expect(hasLocaleQueue('en')).toBe(false) expect(hasLocaleQueue('en')).toBe(false)
expect(lookup('foo', 'en')).toBe('Foo') expect(lookup('foo', 'en')).toBe('Foo')
}) })
test('if no locale is set, ignore the loading delay', async () => {
register(
'en',
() => new Promise(res => setTimeout(() => res({ foo: 'Foo' }), 50))
)
const promise = $locale.set('en')
expect(get(isLoading)).toBe(true)
await promise
expect(get(isLoading)).toBe(false)
})
test("if a locale is set, don't ignore the loading delay", async () => {
register(
'en',
() => new Promise(res => setTimeout(() => res({ foo: 'Foo' }), 50))
)
register(
'pt',
() => new Promise(res => setTimeout(() => res({ foo: 'Foo' }), 50))
)
await $locale.set('en')
const promise = $locale.set('pt')
expect(get(isLoading)).toBe(false)
await promise
expect(get(isLoading)).toBe(false)
})