Skip to content

Commit ee92455

Browse files
committed
switched remaining QPixel API to Storage
1 parent 3d67579 commit ee92455

2 files changed

Lines changed: 15 additions & 17 deletions

File tree

app/assets/javascripts/qpixel_api.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -194,17 +194,15 @@ window.QPixel = {
194194
if (QPixel._preferences != null) {
195195
return QPixel._preferences;
196196
}
197-
// Early return the preferences from localStorage unless null or undefined
197+
198+
// Early return the preferences from storage unless null or undefined
198199
const key = QPixel._preferencesLocalStorageKey();
199-
const localStoragePreferences = (key in localStorage)
200-
? JSON.parse(localStorage[key])
201-
: null;
202-
if (localStoragePreferences != null) {
203-
QPixel._preferences = localStoragePreferences;
204-
return QPixel._preferences;
200+
const storedPreferences = QPixel.Storage?.get(key, { parse: true });
201+
if (storedPreferences) {
202+
return (QPixel._preferences = /** @type {UserPreferences} */(storedPreferences));
205203
}
206-
// Preferences are still null (or undefined) after loading from localStorage, so we're probably on a site we
207-
// haven't loaded them for yet. Load from Redis via AJAX.
204+
205+
// If preferences are absent in storage, load them via AJAX
208206
await QPixel._cachedFetchPreferences();
209207
return QPixel._preferences;
210208
},
@@ -322,7 +320,7 @@ window.QPixel = {
322320

323321
_preferencesLocalStorageKey: () => {
324322
const id = document.body.dataset.userId;
325-
const key = `qpixel.user_${id}_preferences`;
323+
const key = `user_${id}_preferences`;
326324
QPixel._preferencesLocalStorageKey = () => key;
327325
return key;
328326
},
@@ -353,7 +351,7 @@ window.QPixel = {
353351
_updatePreferencesLocally: (data) => {
354352
QPixel._preferences = data;
355353
const key = QPixel._preferencesLocalStorageKey();
356-
localStorage[key] = JSON.stringify(QPixel._preferences);
354+
QPixel.Storage?.set(key, QPixel._preferences);
357355
},
358356

359357
currentCaretSequence: (splat, posIdx) => {

global.d.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ interface QPixel {
229229
_cachedFetchPreferences?: () => Promise<void>;
230230

231231
/**
232-
* Update local variable _preferences and localStorage with an AJAX call for the user preferences
232+
* Update local variable _preferences and storage with an AJAX call for the user preferences
233233
*/
234234
_fetchPreferences?: () => Promise<void>;
235235

@@ -240,19 +240,19 @@ interface QPixel {
240240

241241
/**
242242
* Get an object containing the current user's preferences. Loads, in order of precedence, from local variable,
243-
* {@link localStorage}, or Redis via AJAX.
243+
* {@link QPixelStorage}, or Redis via AJAX.
244244
* @returns user preferences or `null` on failure
245245
*/
246246
_getPreferences?: () => Promise<UserPreferences | null>;
247247

248248
/**
249-
* Get the key to use for storing user preferences in localStorage, to avoid conflating users
250-
* @returns string the localStorage key
249+
* Get the key to use for storing user preferences in storage, to avoid conflating users
250+
* @returns string the storage key
251251
*/
252252
_preferencesLocalStorageKey?: () => string;
253253

254254
/**
255-
* Set local variable _preferences and localStorage to new preferences data
255+
* Set local variable _preferences and storage to new preferences data
256256
* @param data an object, containing the new preferences data
257257
*/
258258
_updatePreferencesLocally?: (data: UserPreferences) => void;
@@ -338,7 +338,7 @@ interface QPixel {
338338
/**
339339
* Set a user preference by name to the value provided.
340340
* @param name the name of the preference to set
341-
* @param value the value to set to - must respond to toString() for {@link localStorage} and Redis
341+
* @param value the value to set to - must respond to toString() for {@link QPixelStorage} and Redis
342342
* @param community is this preference community-local (true), or network-wide (false)?
343343
*/
344344
setPreference?: (name: string, value: unknown, community?: boolean) => Promise<void>;

0 commit comments

Comments
 (0)