Refactor AccountStorage to use private fields for state management
| @@ -7098,7 +7098,7 @@ export async function saveSettings(loopCounter = 0) { | ||
| 7098 | 7098 | url: '/api/settings/save', |
| 7099 | 7099 | data: JSON.stringify({ |
| 7100 | 7100 | firstRun: firstRun, |
| 7101 | 7101 | accountStorage: accountStorage.stategetState(), |
| 7102 | 7102 | currentVersion: currentVersion, |
| 7103 | 7103 | username: name1, |
| 7104 | 7104 | active_character: active_character, |
| @@ -34,10 +34,15 @@ const MIGRATABLE_KEYS = [ | ||
| 34 | 34 | * Provides access to account storage of arbitrary key-value pairs. |
| 35 | 35 | */ |
| 36 | 36 | class 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; | |
| 41 | 46 | |
| 42 | 47 | #migrateLocalStorage() { |
| 43 | 48 | for (let i = 0; i < globalThis.localStorage.length; i++) { |
| @@ -45,7 +50,7 @@ class AccountStorage { | ||
| 45 | 50 | const value = globalThis.localStorage.getItem(key); |
| 46 | 51 | |
| 47 | 52 | if (MIGRATABLE_KEYS.some(k => k.test(key))) { |
| 48 | 53 | this.#state[key] = value; |
| 49 | 54 | globalThis.localStorage.removeItem(key); |
| 50 | 55 | } |
| 51 | 56 | } |
| @@ -57,16 +62,16 @@ class AccountStorage { | ||
| 57 | 62 | */ |
| 58 | 63 | init(state) { |
| 59 | 64 | if (state && typeof state === 'object') { |
| 60 | 65 | this.#state = Object.assign(this.#state, state); |
| 61 | 66 | } |
| 62 | 67 | |
| 63 | 68 | if (!Object.hasOwn(this.#state, MIGRATED_MARKER)) { |
| 64 | 69 | this.#migrateLocalStorage(); |
| 65 | 70 | this.#state[MIGRATED_MARKER] = '1'; |
| 66 | 71 | saveSettingsDebounced(); |
| 67 | 72 | } |
| 68 | 73 | |
| 69 | 74 | this.#ready = true; |
| 70 | 75 | } |
| 71 | 76 | |
| 72 | 77 | /** |
| @@ -75,11 +80,11 @@ class AccountStorage { | ||
| 75 | 80 | * @returns {string|null} Value of the key |
| 76 | 81 | */ |
| 77 | 82 | getItem(key) { |
| 78 | 83 | if (!this.#ready) { |
| 79 | 84 | console.warn(`AccountStorage not ready (trying to read from ${key})`); |
| 80 | 85 | } |
| 81 | 86 | |
| 82 | 87 | return Object.hasOwn(this.#state, key) ? String(this.#state[key]) : null; |
| 83 | 88 | } |
| 84 | 89 | |
| 85 | 90 | /** |
| @@ -88,11 +93,11 @@ class AccountStorage { | ||
| 88 | 93 | * @param {string} value Value to set |
| 89 | 94 | */ |
| 90 | 95 | setItem(key, value) { |
| 91 | 96 | if (!this.#ready) { |
| 92 | 97 | console.warn(`AccountStorage not ready (trying to write to ${key})`); |
| 93 | 98 | } |
| 94 | 99 | |
| 95 | 100 | this.#state[key] = String(value); |
| 96 | 101 | saveSettingsDebounced(); |
| 97 | 102 | } |
| 98 | 103 | |
| @@ -101,17 +106,25 @@ class AccountStorage { | ||
| 101 | 106 | * @param {string} key Key to remove |
| 102 | 107 | */ |
| 103 | 108 | removeItem(key) { |
| 104 | 109 | if (!this.#ready) { |
| 105 | 110 | console.warn(`AccountStorage not ready (trying to remove ${key})`); |
| 106 | 111 | } |
| 107 | 112 | |
| 108 | 113 | if (!Object.hasOwn(this.#state, key)) { |
| 109 | 114 | return; |
| 110 | 115 | } |
| 111 | 116 | |
| 112 | 117 | delete this.#state[key]; |
| 113 | 118 | saveSettingsDebounced(); |
| 114 | 119 | } |
| 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 | + } | |
| 115 | 128 | } |
| 116 | 129 | |
| 117 | 130 | /** |