Skip to content

Commit

Permalink
Address comments from PR #43
Browse files Browse the repository at this point in the history
  • Loading branch information
aomarks committed Aug 15, 2020
1 parent 38dc4bd commit 3ba7e1d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
7 changes: 5 additions & 2 deletions src_client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ let loadLocale: ((locale: string) => Promise<LocaleModule>) | undefined;
let configured = false;
let templates: TemplateMap | undefined;
let loading = new Deferred<void>();
let requestId = 0;

/**
* Set configuration parameters for lit-localize when in runtime mode. Returns
Expand Down Expand Up @@ -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();
Expand All @@ -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;
Expand All @@ -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',
Expand Down

0 comments on commit 3ba7e1d

Please sign in to comment.