No real changes, just renames

This commit is contained in:
binwiederhier 2023-06-09 14:32:34 -04:00
parent 4704b2a0e4
commit 2e8292a65f
10 changed files with 31 additions and 35 deletions

View file

@ -1,4 +1,4 @@
import getDb from "./getDb";
import db from "./db";
class Prefs {
constructor(db) {
@ -42,5 +42,5 @@ class Prefs {
}
}
const prefs = new Prefs(getDb());
const prefs = new Prefs(db());
export default prefs;

View file

@ -1,5 +1,9 @@
import sessionReplica from "./SessionReplica";
/**
* Manages the logged-in user's session and access token.
* The session replica is stored in IndexedDB so that the service worker can access it.
*/
class Session {
constructor(replica) {
this.replica = replica;
@ -8,14 +12,12 @@ class Session {
store(username, token) {
localStorage.setItem("user", username);
localStorage.setItem("token", token);
this.replica.store(username, token);
}
reset() {
localStorage.removeItem("user");
localStorage.removeItem("token");
this.replica.reset();
}

View file

@ -1,24 +1,21 @@
import Dexie from "dexie";
// Store to IndexedDB as well so that the
// service worker can access it
// TODO: Probably make everything depend on this and not use localStorage,
// but that's a larger refactoring effort for another PR
/**
* Replica of the session in IndexedDB. This is used by the service
* worker to access the session. This is a bit of a hack.
*/
class SessionReplica {
constructor() {
const db = new Dexie("session-replica");
db.version(1).stores({
keyValueStore: "&key",
kv: "&key",
});
this.db = db;
}
async store(username, token) {
try {
await this.db.keyValueStore.bulkPut([
await this.db.kv.bulkPut([
{ key: "user", value: username },
{ key: "token", value: token },
]);
@ -36,7 +33,7 @@ class SessionReplica {
}
async username() {
return (await this.db.keyValueStore.get({ key: "user" }))?.value;
return (await this.db.kv.get({ key: "user" }))?.value;
}
}

View file

@ -1,7 +1,7 @@
import api from "./Api";
import notifier from "./Notifier";
import prefs from "./Prefs";
import getDb from "./getDb";
import db from "./db";
import { topicUrl } from "./utils";
class SubscriptionManager {
@ -244,4 +244,4 @@ class SubscriptionManager {
}
}
export default new SubscriptionManager(getDb());
export default new SubscriptionManager(db());

View file

@ -1,4 +1,4 @@
import getDb from "./getDb";
import db from "./db";
import session from "./Session";
class UserManager {
@ -47,4 +47,4 @@ class UserManager {
}
}
export default new UserManager(getDb());
export default new UserManager(db());

View file

@ -26,7 +26,7 @@ export const useWebPushUpdateWorker = () => {
}, [topics, lastTopics]);
};
const intervalMillis = 5 * 60 * 1_000; // 5 minutes
const intervalMillis = 13 * 60 * 1_000; // 13 minutes
const updateIntervalMillis = 60 * 60 * 1_000; // 1 hour
class WebPushRefreshWorker {

View file

@ -8,12 +8,11 @@ import sessionReplica from "./SessionReplica";
// Notes:
// - As per docs, we only declare the indexable columns, not all columns
const getDbBase = (username) => {
// The IndexedDB database name is based on the logged-in user
const dbName = username ? `ntfy-${username}` : "ntfy";
const createDatabase = (username) => {
const dbName = username ? `ntfy-${username}` : "ntfy"; // IndexedDB database is based on the logged-in user
const db = new Dexie(dbName);
db.version(2).stores({
db.version(1).stores({
subscriptions: "&id,baseUrl,[baseUrl+mutedUntil]",
notifications: "&id,subscriptionId,time,new,[subscriptionId+new]", // compound key for query performance
users: "&baseUrl,username",
@ -23,12 +22,11 @@ const getDbBase = (username) => {
return db;
};
export const getDbAsync = async () => {
export const dbAsync = async () => {
const username = await sessionReplica.username();
return getDbBase(username);
return createDatabase(username);
};
const getDb = () => getDbBase(session.username());
export const db = () => createDatabase(session.username());
export default getDb;
export default db;