Refactor AccountStorage to use private fields for state management

5085f6cdc9f68dc702ab3a78e5dc7f0efe30c770

Cohee <18619528+Cohee1207@users.noreply.github.com>

2 files changed, +30 -17Showing whitespace changes
public/script.js+1 -1
@@ -7098,7 +7098,7 @@ export async function saveSettings(loopCounter = 0) {
70987098 url: '/api/settings/save',
70997099 data: JSON.stringify({
71007100 firstRun: firstRun,
71017101 accountStorage: accountStorage.stategetState(),
71027102 currentVersion: currentVersion,
71037103 username: name1,
71047104 active_character: active_character,
public/scripts/util/AccountStorage.js+29 -16
@@ -34,10 +34,15 @@ const MIGRATABLE_KEYS = [
3434 * Provides access to account storage of arbitrary key-value pairs.
3535 */
3636class AccountStorage {
37- constructor() {
37+ /**
38- this.state = {};
38+ * @type {Record<string, string>} Storage state
39- this.ready = false;
39+ */
40- }
40+ #state = {};
41+
42+ /**
43+ * @type {boolean} If the storage was initialized
44+ */
45+ #ready = false;
4146
4247 #migrateLocalStorage() {
4348 for (let i = 0; i < globalThis.localStorage.length; i++) {
@@ -45,7 +50,7 @@ class AccountStorage {
4550 const value = globalThis.localStorage.getItem(key);
4651
4752 if (MIGRATABLE_KEYS.some(k => k.test(key))) {
4853 this.#state[key] = value;
4954 globalThis.localStorage.removeItem(key);
5055 }
5156 }
@@ -57,16 +62,16 @@ class AccountStorage {
5762 */
5863 init(state) {
5964 if (state && typeof state === 'object') {
6065 this.#state = Object.assign(this.#state, state);
6166 }
6267
6368 if (!Object.hasOwn(this.#state, MIGRATED_MARKER)) {
6469 this.#migrateLocalStorage();
6570 this.#state[MIGRATED_MARKER] = '1';
6671 saveSettingsDebounced();
6772 }
6873
6974 this.#ready = true;
7075 }
7176
7277 /**
@@ -75,11 +80,11 @@ class AccountStorage {
7580 * @returns {string|null} Value of the key
7681 */
7782 getItem(key) {
7883 if (!this.#ready) {
7984 console.warn(`AccountStorage not ready (trying to read from ${key})`);
8085 }
8186
8287 return Object.hasOwn(this.#state, key) ? String(this.#state[key]) : null;
8388 }
8489
8590 /**
@@ -88,11 +93,11 @@ class AccountStorage {
8893 * @param {string} value Value to set
8994 */
9095 setItem(key, value) {
9196 if (!this.#ready) {
9297 console.warn(`AccountStorage not ready (trying to write to ${key})`);
9398 }
9499
95100 this.#state[key] = String(value);
96101 saveSettingsDebounced();
97102 }
98103
@@ -101,17 +106,25 @@ class AccountStorage {
101106 * @param {string} key Key to remove
102107 */
103108 removeItem(key) {
104109 if (!this.#ready) {
105110 console.warn(`AccountStorage not ready (trying to remove ${key})`);
106111 }
107112
108113 if (!Object.hasOwn(this.#state, key)) {
109114 return;
110115 }
111116
112117 delete this.#state[key];
113118 saveSettingsDebounced();
114119 }
120+
121+ /**
122+ * Gets a snapshot of the storage state.
123+ * @returns {Record<string, string>} A deep clone of the storage state
124+ */
125+ getState() {
126+ return structuredClone(this.#state);
127+ }
115128}
116129
117130/**