Add the Sandbar mobile companion PWA
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
ad165ef6a5
18 files changed
+1143
No files matched your search
@@ -0,0 +1,41 @@
|
||||
// Settings shared by the page and the service worker. The service worker cannot read
|
||||
// localStorage, so the sign-in lives in IndexedDB; the page keeps a copy in localStorage
|
||||
// only for quick startup and falls back to IndexedDB.
|
||||
|
||||
const DB = 'sandbar-mobile';
|
||||
|
||||
function open() {
|
||||
return new Promise((resolve, reject) => {
|
||||
const r = indexedDB.open(DB, 1);
|
||||
r.onupgradeneeded = () => r.result.createObjectStore('kv');
|
||||
r.onsuccess = () => resolve(r.result);
|
||||
r.onerror = () => reject(r.error);
|
||||
});
|
||||
}
|
||||
|
||||
export async function kvGet(key) {
|
||||
const db = await open();
|
||||
return new Promise((resolve, reject) => {
|
||||
const r = db.transaction('kv').objectStore('kv').get(key);
|
||||
r.onsuccess = () => resolve(r.result);
|
||||
r.onerror = () => reject(r.error);
|
||||
});
|
||||
}
|
||||
|
||||
export async function kvSet(key, value) {
|
||||
const db = await open();
|
||||
return new Promise((resolve, reject) => {
|
||||
const tx = db.transaction('kv', 'readwrite');
|
||||
if (value === undefined) tx.objectStore('kv').delete(key);
|
||||
else tx.objectStore('kv').put(value, key);
|
||||
tx.oncomplete = () => resolve();
|
||||
tx.onerror = () => reject(tx.error);
|
||||
});
|
||||
}
|
||||
|
||||
// session: { server, token, keyId, user }
|
||||
export const getSession = () => kvGet('session');
|
||||
export const setSession = (s) => kvSet('session', s);
|
||||
// topics: the topics this device follows
|
||||
export async function getTopics() { return (await kvGet('topics')) || ['alerts']; }
|
||||
export const setTopics = (t) => kvSet('topics', t);
|
||||
Reference in new issue
Block a user