Skip to content

Commit

Permalink
feat: cache date/time descriptor (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
dlockhart authored Sep 11, 2023
1 parent 69a71a7 commit 3dd0747
Show file tree
Hide file tree
Showing 4 changed files with 305 additions and 249 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @BrightspaceUI/gaudi-dev
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 3dd0747

Please sign in to comment.