Skip to content

Commit

Permalink
feat: cache date/time descriptor
Browse files Browse the repository at this point in the history
  • Loading branch information
dlockhart committed Sep 8, 2023
1 parent 69a71a7 commit 3cd7d1b
Show file tree
Hide file tree
Showing 3 changed files with 304 additions and 249 deletions.
12 changes: 12 additions & 0 deletions lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export function validateFormatValue(value) {
class DocumentLocaleSettings {

constructor() {
this._cache = new Map();
this._htmlElem = window.document.getElementsByTagName('html')[0];
this._listeners = [];
this._overrides = {};
Expand All @@ -101,6 +102,7 @@ class DocumentLocaleSettings {
set fallbackLanguage(val) {
const normalized = this._normalize(val);
if (normalized === this._fallbackLanguage) return;
this._cache.clear();
this._fallbackLanguage = normalized;
this._listeners.forEach((cb) => cb());
}
Expand All @@ -109,6 +111,7 @@ class DocumentLocaleSettings {
set language(val) {
const normalized = this._normalize(val);
if (normalized === this._language) return;
this._cache.clear();
this._language = normalized;
this._listeners.forEach((cb) => cb());
}
Expand All @@ -123,6 +126,7 @@ class DocumentLocaleSettings {
delete val.date.formats.timeFormats;
}
}
this._cache.clear();
this._overrides = val;
this._listeners.forEach((cb) => cb());
}
Expand All @@ -131,13 +135,21 @@ class DocumentLocaleSettings {
this._listeners.push(cb);
}

getCacheItem(key, provider) {
if (!this._cache.has(key)) {
this._cache.set(key, provider());
}
return this._cache.get(key);
}

removeChangeListener(cb) {
const index = this._listeners.indexOf(cb);
if (index < 0) return;
this._listeners.splice(index, 1);
}

reset() {
this._cache.clear();
this._language = this._languageInitial;
this._fallbackLanguage = null;
this.overrides = {};
Expand Down
Loading

0 comments on commit 3cd7d1b

Please sign in to comment.