feat: initialize learning platform project with React, Vite, and baseline application structure
This commit is contained in:
71
src/lib/storage.js
Normal file
71
src/lib/storage.js
Normal file
@@ -0,0 +1,71 @@
|
||||
/**
|
||||
* Wrapper around window.localStorage for the Respellion platform.
|
||||
* Handles serialization/deserialization, namespaces, and quota limits.
|
||||
*/
|
||||
|
||||
const STORAGE_PREFIX = 'respellion:';
|
||||
|
||||
export const storage = {
|
||||
/**
|
||||
* Get an item from storage
|
||||
* @param {string} key - e.g. 'kb:topics'
|
||||
* @returns {any}
|
||||
*/
|
||||
get(key) {
|
||||
try {
|
||||
const item = window.localStorage.getItem(STORAGE_PREFIX + key);
|
||||
return item ? JSON.parse(item) : null;
|
||||
} catch (e) {
|
||||
console.error(`Error reading ${key} from storage:`, e);
|
||||
return null;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Set an item in storage
|
||||
* @param {string} key
|
||||
* @param {any} value
|
||||
*/
|
||||
set(key, value) {
|
||||
try {
|
||||
const serialized = JSON.stringify(value);
|
||||
window.localStorage.setItem(STORAGE_PREFIX + key, serialized);
|
||||
} catch (e) {
|
||||
if (e.name === 'QuotaExceededError' || e.code === 22) {
|
||||
console.error(`Storage quota exceeded when setting ${key}.`);
|
||||
// We could implement a strategy here to clear old cached kb:tests
|
||||
} else {
|
||||
console.error(`Error saving ${key} to storage:`, e);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Remove an item from storage
|
||||
* @param {string} key
|
||||
*/
|
||||
remove(key) {
|
||||
try {
|
||||
window.localStorage.removeItem(STORAGE_PREFIX + key);
|
||||
} catch (e) {
|
||||
console.error(`Error removing ${key} from storage:`, e);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Get all keys matching a specific prefix (e.g. 'kb:topics:')
|
||||
* @param {string} prefix
|
||||
* @returns {string[]} Array of matching keys (without the STORAGE_PREFIX)
|
||||
*/
|
||||
getKeysByPrefix(prefix) {
|
||||
const keys = [];
|
||||
const fullPrefix = STORAGE_PREFIX + prefix;
|
||||
for (let i = 0; i < window.localStorage.length; i++) {
|
||||
const key = window.localStorage.key(i);
|
||||
if (key && key.startsWith(fullPrefix)) {
|
||||
keys.push(key.substring(STORAGE_PREFIX.length));
|
||||
}
|
||||
}
|
||||
return keys;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user