diff --git a/README.md b/README.md index 37d3a01c..a97a3e6a 100644 --- a/README.md +++ b/README.md @@ -182,8 +182,10 @@ A new locale has started to load. The `detail` object also contains: - `loadingLocale: string`: Code of the locale that has started loading. A `loading` status can be followed by a `ready`, `error`, or `loading` status. -It will be followed by another `loading` status in the case that a second locale -was requested before the first one finished loading. + +In the case that a second locale is requested before the first one finishes +loading, a new `loading` event is dispatched, and no `ready` or `error` event +will be dispatched for the first request, because it is now stale. #### `ready` diff --git a/src_client/index.ts b/src_client/index.ts index d9ccb0e3..e40a36d9 100644 --- a/src_client/index.ts +++ b/src_client/index.ts @@ -172,6 +172,7 @@ let loadLocale: ((locale: string) => Promise) | undefined; let configured = false; let templates: TemplateMap | undefined; let loading = new Deferred(); +let requestId = 0; /** * Set configuration parameters for lit-localize when in runtime mode. Returns @@ -254,6 +255,8 @@ const setLocale: ((newLocale: string) => void) & { if (!validLocales.has(newLocale)) { throw new Error('Invalid locale code'); } + requestId++; + const thisRequestId = requestId; loadingLocale = newLocale; if (loading.settled) { loading = new Deferred(); @@ -268,7 +271,7 @@ const setLocale: ((newLocale: string) => void) & { } else { loadLocale(newLocale).then( (mod) => { - if (newLocale === loadingLocale) { + if (requestId === thisRequestId) { activeLocale = newLocale; loadingLocale = undefined; templates = mod.templates; @@ -281,7 +284,7 @@ const setLocale: ((newLocale: string) => void) & { // need to check if the locale is still the one they expected to load. }, (err) => { - if (newLocale === loadingLocale) { + if (requestId === thisRequestId) { loading.reject(err); dispatchStatusEvent({ status: 'error',