From 0b7f61c49a1c3206bbb5d9c77dfb5819a85d4bb5 Mon Sep 17 00:00:00 2001 From: Christian Kaisermann Date: Thu, 28 Nov 2019 23:35:48 -0300 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20allow=20to=20wait=20for?= =?UTF-8?q?=20initial=20locale=20load?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/client/includes/loaderQueue.ts | 2 +- src/client/index.ts | 17 +++++++++++++---- src/client/stores/locale.ts | 4 ++-- test/client/includes/loaderQueue.test.ts | 14 +++++++------- 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/client/includes/loaderQueue.ts b/src/client/includes/loaderQueue.ts index 96d1925..9c4b1bf 100644 --- a/src/client/includes/loaderQueue.ts +++ b/src/client/includes/loaderQueue.ts @@ -46,7 +46,7 @@ export function hasLocaleQueue(locale: string) { } const activeLocaleFlushes: { [key: string]: Promise } = {} -export function flushQueue(locale: string = getCurrentLocale()) { +export function flush(locale: string) { if (!hasLocaleQueue(locale)) return if (locale in activeLocaleFlushes) return activeLocaleFlushes[locale] diff --git a/src/client/index.ts b/src/client/index.ts index 861faf3..437a4bf 100644 --- a/src/client/index.ts +++ b/src/client/index.ts @@ -1,10 +1,22 @@ import { MessageObject } from './types' +import { getCurrentLocale } from './stores/locale' +import { getOptions } from './configs' +import { flush } from './includes/loaderQueue' // defineMessages allow us to define and extract dynamic message ids export function defineMessages(i: Record) { return i } +export function waitLocale(locale: string) { + return flush( + locale || + getCurrentLocale() || + getOptions().initialLocale || + getOptions().fallbackLocale + ) +} + export { init } from './configs' export { $locale as locale } from './stores/locale' export { @@ -16,7 +28,4 @@ export { $isLoading as isLoading } from './stores/loading' export { $format as format, $format as _, $format as t } from './stores/format' // utilities -export { - flushQueue as waitLocale, - registerLocaleLoader as register, -} from './includes/loaderQueue' +export { registerLocaleLoader as register } from './includes/loaderQueue' diff --git a/src/client/stores/locale.ts b/src/client/stores/locale.ts index 2983ba2..a18b7f1 100644 --- a/src/client/stores/locale.ts +++ b/src/client/stores/locale.ts @@ -1,6 +1,6 @@ import { writable } from 'svelte/store' -import { flushQueue, hasLocaleQueue } from '../includes/loaderQueue' +import { flush, hasLocaleQueue } from '../includes/loaderQueue' import { getOptions } from '../configs' import { getClosestAvailableLocale } from './dictionary' @@ -58,7 +58,7 @@ $locale.subscribe((newLocale: string) => { const localeSet = $locale.set $locale.set = (newLocale: string): void | Promise => { if (getClosestAvailableLocale(newLocale) && hasLocaleQueue(newLocale)) { - return flushQueue(newLocale).then(() => localeSet(newLocale)) + return flush(newLocale).then(() => localeSet(newLocale)) } return localeSet(newLocale) } diff --git a/test/client/includes/loaderQueue.test.ts b/test/client/includes/loaderQueue.test.ts index a587eb2..7991864 100644 --- a/test/client/includes/loaderQueue.test.ts +++ b/test/client/includes/loaderQueue.test.ts @@ -2,7 +2,7 @@ import { get } from 'svelte/store' import { hasLocaleQueue, - flushQueue, + flush, registerLocaleLoader, resetQueues, } from '../../../src/client/includes/loaderQueue' @@ -28,14 +28,14 @@ test('checks if exist queues of locale and its fallbacks', () => { }) test("does nothing if there's no queue for a locale", () => { - expect(flushQueue('foo')).toBe(undefined) + expect(flush('foo')).toBe(undefined) }) test('flushes the queue of a locale and its fallbacks and merge the result with the dictionary', async () => { registerLocaleLoader('en', loader({ field: 'Name' })) registerLocaleLoader('en-US', loader({ field_2: 'Lastname' })) - await flushQueue('en-US') + await flush('en-US') expect(getMessageFromDictionary('en', 'field')).toBe('Name') expect(getMessageFromDictionary('en-US', 'field_2')).toBe('Lastname') @@ -47,9 +47,9 @@ test('flushes the queue of a locale and its fallbacks and merge the result with test('consecutive flushes return the same promise', async () => { registerLocaleLoader('en', async () => ({})) - const flushA = flushQueue('en') - const flushB = flushQueue('en') - const flushC = flushQueue('en') + const flushA = flush('en') + const flushB = flush('en') + const flushC = flush('en') expect(flushB).toStrictEqual(flushA) expect(flushC).toStrictEqual(flushA) @@ -64,7 +64,7 @@ test('should set loading to true if passed min delay and false after loading', ( ) ) - const flush = flushQueue('en') + const flush = flush('en') return new Promise((res, rej) => { setTimeout(() => {