Read the curated photo feeds from wss://nostr.kosmos.org, verify events, cache originals and thumbnails, and let users pick a wallpaper from the Photos section. Nature is the default feed; City Nights is opt-in. Adds the manual publishing scripts, real event fixtures, protocol documentation, and tests with stubbed network connections.
107 lines
2.6 KiB
JavaScript
107 lines
2.6 KiB
JavaScript
import Service from '@ember/service';
|
|
import { tracked } from '@glimmer/tracking';
|
|
import localforage from 'localforage';
|
|
|
|
export default class StorageService extends Service {
|
|
@tracked cache = new Map();
|
|
|
|
databases = new Map();
|
|
|
|
constructor() {
|
|
super(...arguments);
|
|
this.initializeDatabases();
|
|
}
|
|
|
|
initializeDatabases() {
|
|
const dbConfigs = [
|
|
{ name: 'preferences', storeName: 'prefs' },
|
|
{ name: 'integrations', storeName: 'integrations' },
|
|
{ name: 'settings', storeName: 'settings' },
|
|
{ name: 'icons', storeName: 'icons' },
|
|
{ name: 'events', storeName: 'events' },
|
|
{ name: 'photos', storeName: 'photos' },
|
|
];
|
|
|
|
dbConfigs.forEach(({ name, storeName }) => {
|
|
const db = localforage.createInstance({
|
|
name: 'dashtab',
|
|
storeName: storeName,
|
|
driver: [
|
|
localforage.INDEXEDDB,
|
|
localforage.WEBSQL,
|
|
localforage.LOCALSTORAGE,
|
|
],
|
|
});
|
|
this.databases.set(name, db);
|
|
});
|
|
}
|
|
|
|
getDatabase(dbName = 'preferences') {
|
|
if (!this.databases.has(dbName)) {
|
|
throw new Error(
|
|
`Database "${dbName}" not initialized. Available: ${Array.from(this.databases.keys()).join(', ')}`
|
|
);
|
|
}
|
|
return this.databases.get(dbName);
|
|
}
|
|
|
|
getCacheKey(dbName, key) {
|
|
return `${dbName}:${key}`;
|
|
}
|
|
|
|
async get(dbName = 'preferences', key) {
|
|
const cacheKey = this.getCacheKey(dbName, key);
|
|
if (this.cache.has(cacheKey)) {
|
|
return this.cache.get(cacheKey);
|
|
}
|
|
|
|
const db = this.getDatabase(dbName);
|
|
const value = await db.getItem(key);
|
|
this.cache.set(cacheKey, value);
|
|
return value;
|
|
}
|
|
|
|
async set(dbName = 'preferences', key, value) {
|
|
const cacheKey = this.getCacheKey(dbName, key);
|
|
this.cache.set(cacheKey, value);
|
|
|
|
const db = this.getDatabase(dbName);
|
|
return db.setItem(key, value);
|
|
}
|
|
|
|
async remove(dbName = 'preferences', key) {
|
|
const cacheKey = this.getCacheKey(dbName, key);
|
|
this.cache.delete(cacheKey);
|
|
|
|
const db = this.getDatabase(dbName);
|
|
return db.removeItem(key);
|
|
}
|
|
|
|
async clear(dbName = 'preferences') {
|
|
const db = this.getDatabase(dbName);
|
|
const keys = await db.keys();
|
|
|
|
keys.forEach((key) => {
|
|
const cacheKey = this.getCacheKey(dbName, key);
|
|
this.cache.delete(cacheKey);
|
|
});
|
|
|
|
return db.clear();
|
|
}
|
|
|
|
async keys(dbName = 'preferences') {
|
|
const db = this.getDatabase(dbName);
|
|
return db.keys();
|
|
}
|
|
|
|
async length(dbName = 'preferences') {
|
|
const db = this.getDatabase(dbName);
|
|
return db.length();
|
|
}
|
|
|
|
async iterate(dbName = 'preferences', callback) {
|
|
const db = this.getDatabase(dbName);
|
|
return db.iterate(callback);
|
|
}
|
|
}
|