Skip to content

Commit 349696f

Browse files
authored
Chore: Prefer window.localStorage over localStorage (#6270)
* Motivation for features / changes Internal checks throw JSC_UNDEFINED_VARIABLE errors when using localStorage directly. However, window is defined so window.localStorage does not throw an error. This change helps us pass those checks internally.
1 parent 099ef83 commit 349696f

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

tensorboard/webapp/persistent_settings/_data_source/persistent_settings_data_source.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ export class PersistentSettingsDataSourceImpl<UiSettings, StorageSettings>
245245

246246
return this.getSettings().pipe(
247247
tap((currentPartialSettings) => {
248-
localStorage.setItem(
248+
window.localStorage.setItem(
249249
GLOBAL_LOCAL_STORAGE_KEY,
250250
JSON.stringify(
251251
this.converter.uiToBackend({
@@ -254,8 +254,8 @@ export class PersistentSettingsDataSourceImpl<UiSettings, StorageSettings>
254254
})
255255
)
256256
);
257-
localStorage.removeItem(LEGACY_METRICS_LOCAL_STORAGE_KEY);
258-
localStorage.removeItem(NOTIFICATION_LAST_READ_TIME_KEY);
257+
window.localStorage.removeItem(LEGACY_METRICS_LOCAL_STORAGE_KEY);
258+
window.localStorage.removeItem(NOTIFICATION_LAST_READ_TIME_KEY);
259259
}),
260260
map(() => void null)
261261
);
@@ -270,7 +270,9 @@ export class PersistentSettingsDataSourceImpl<UiSettings, StorageSettings>
270270
}
271271

272272
getSettings(): Observable<Partial<UiSettings>> {
273-
const lastReadTime = localStorage.getItem(NOTIFICATION_LAST_READ_TIME_KEY);
273+
const lastReadTime = window.localStorage.getItem(
274+
NOTIFICATION_LAST_READ_TIME_KEY
275+
);
274276
const notificationSettings = this.converter.backendToUi(
275277
this.deserialize(
276278
lastReadTime
@@ -282,12 +284,14 @@ export class PersistentSettingsDataSourceImpl<UiSettings, StorageSettings>
282284
);
283285
const legacySettings = this.converter.backendToUi(
284286
this.deserialize(
285-
localStorage.getItem(LEGACY_METRICS_LOCAL_STORAGE_KEY) ?? '{}'
287+
window.localStorage.getItem(LEGACY_METRICS_LOCAL_STORAGE_KEY) ?? '{}'
286288
)
287289
);
288290

289291
const settings = this.converter.backendToUi(
290-
this.deserialize(localStorage.getItem(GLOBAL_LOCAL_STORAGE_KEY) ?? '{}')
292+
this.deserialize(
293+
window.localStorage.getItem(GLOBAL_LOCAL_STORAGE_KEY) ?? '{}'
294+
)
291295
);
292296
return of({
293297
...notificationSettings,

tensorboard/webapp/webapp_data_source/tb_feature_flag_data_source.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ export class FeatureFlagOverrideDataSource implements TBFeatureFlagDataSource {
6161
...currentState,
6262
...flags,
6363
};
64-
localStorage.setItem(FEATURE_FLAG_STORAGE_KEY, JSON.stringify(newState));
64+
window.localStorage.setItem(
65+
FEATURE_FLAG_STORAGE_KEY,
66+
JSON.stringify(newState)
67+
);
6568
}
6669

6770
resetPersistedFeatureFlag<K extends keyof FeatureFlags>(featureFlag: K) {
@@ -74,21 +77,21 @@ export class FeatureFlagOverrideDataSource implements TBFeatureFlagDataSource {
7477
// Remove the entire key-value from localStorage when there are no more
7578
// overrides.
7679
if (Object.keys(currentState).length === 0) {
77-
localStorage.removeItem(FEATURE_FLAG_STORAGE_KEY);
80+
window.localStorage.removeItem(FEATURE_FLAG_STORAGE_KEY);
7881
return;
7982
}
80-
localStorage.setItem(
83+
window.localStorage.setItem(
8184
FEATURE_FLAG_STORAGE_KEY,
8285
JSON.stringify(currentState)
8386
);
8487
}
8588

8689
resetAllPersistedFeatureFlags() {
87-
localStorage.removeItem(FEATURE_FLAG_STORAGE_KEY);
90+
window.localStorage.removeItem(FEATURE_FLAG_STORAGE_KEY);
8891
}
8992

9093
getPersistentFeatureFlags(): Partial<FeatureFlags> {
91-
const currentState = localStorage.getItem(FEATURE_FLAG_STORAGE_KEY);
94+
const currentState = window.localStorage.getItem(FEATURE_FLAG_STORAGE_KEY);
9295
if (currentState == null) {
9396
return {};
9497
}

0 commit comments

Comments
 (0)