Skip to content

Commit 3ba7e1d

Browse files
committed
Address comments from PR #43
1 parent 38dc4bd commit 3ba7e1d

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,10 @@ A new locale has started to load. The `detail` object also contains:
182182
- `loadingLocale: string`: Code of the locale that has started loading.
183183

184184
A `loading` status can be followed by a `ready`, `error`, or `loading` status.
185-
It will be followed by another `loading` status in the case that a second locale
186-
was requested before the first one finished loading.
185+
186+
In the case that a second locale is requested before the first one finishes
187+
loading, a new `loading` event is dispatched, and no `ready` or `error` event
188+
will be dispatched for the first request, because it is now stale.
187189

188190
#### `ready`
189191

src_client/index.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ let loadLocale: ((locale: string) => Promise<LocaleModule>) | undefined;
172172
let configured = false;
173173
let templates: TemplateMap | undefined;
174174
let loading = new Deferred<void>();
175+
let requestId = 0;
175176

176177
/**
177178
* Set configuration parameters for lit-localize when in runtime mode. Returns
@@ -254,6 +255,8 @@ const setLocale: ((newLocale: string) => void) & {
254255
if (!validLocales.has(newLocale)) {
255256
throw new Error('Invalid locale code');
256257
}
258+
requestId++;
259+
const thisRequestId = requestId;
257260
loadingLocale = newLocale;
258261
if (loading.settled) {
259262
loading = new Deferred();
@@ -268,7 +271,7 @@ const setLocale: ((newLocale: string) => void) & {
268271
} else {
269272
loadLocale(newLocale).then(
270273
(mod) => {
271-
if (newLocale === loadingLocale) {
274+
if (requestId === thisRequestId) {
272275
activeLocale = newLocale;
273276
loadingLocale = undefined;
274277
templates = mod.templates;
@@ -281,7 +284,7 @@ const setLocale: ((newLocale: string) => void) & {
281284
// need to check if the locale is still the one they expected to load.
282285
},
283286
(err) => {
284-
if (newLocale === loadingLocale) {
287+
if (requestId === thisRequestId) {
285288
loading.reject(err);
286289
dispatchStatusEvent({
287290
status: 'error',

0 commit comments

Comments
 (0)