Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: cache date/time descriptor #124

Merged
merged 2 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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