🐛 Surface storage errors and ignore a late gate resolution
When IndexedDB rejected or blocked the open, resolveGate() rejected, the gate stayed null and the app showed "Loading…" forever. A failed open now cleans up and rejects with a reason, a blocked open gives up after eight seconds, and resolveGate() turns that into an error gate: the layout shows the message with a Retry button instead of the loading line, and the sign-in itself is kept. logout() clears the auth store and the session before it closes and wipes the local data, and swallows a failing wipe, so the user is never left half logged out. activate() sets the session only after the database is open and only if the auth store is still valid with the same token, so a resolution that finishes after a logout or another sign-in no longer reopens the app for the wrong user. openDb() hands out an in-flight open only to the same user; another user's open waits for it and then replaces it.
This commit is contained in:
@@ -41,6 +41,8 @@ const SYNC_ID = 'wotra';
|
||||
const SCHEMA_VERSION = 1;
|
||||
/** Changes per push request; the server rejects more than 500. */
|
||||
const PUSH_BATCH = 200;
|
||||
/** Upper bound for opening the local database in milliseconds. */
|
||||
const OPEN_TIMEOUT = 8000;
|
||||
|
||||
const NUMBER_FIELDS: Record<CollectionName, string[]> = {
|
||||
entries: ['start_time', 'end_time'],
|
||||
@@ -264,14 +266,29 @@ let syncing: Promise<void> | null = null;
|
||||
let beforeSync: (() => Promise<void>) | null = null;
|
||||
const cleanups: (() => void)[] = [];
|
||||
const roundListeners = new Set<() => void>();
|
||||
let opening: Promise<void> | null = null;
|
||||
/** An open in flight and the user it is for; an open for another user waits for it. */
|
||||
let opening: { uid: string; done: Promise<void> } | null = null;
|
||||
|
||||
export function openDb(uid: string): Promise<void> {
|
||||
if (opening) return opening;
|
||||
opening = doOpenDb(uid).finally(() => {
|
||||
opening = null;
|
||||
if (opening?.uid === uid) return opening.done;
|
||||
const settled = opening?.done.catch(() => {}) ?? Promise.resolve();
|
||||
const done = settled
|
||||
.then(() => doOpenDb(uid))
|
||||
.finally(() => {
|
||||
if (opening?.done === done) opening = null;
|
||||
});
|
||||
opening = { uid, done };
|
||||
return done;
|
||||
}
|
||||
|
||||
/** Resolves once every collection is ready; rejects when the database cannot be opened or a blocked open does not answer in time. */
|
||||
function ready(c: Record<CollectionName, AnyCollection>): Promise<void> {
|
||||
let timer: ReturnType<typeof setTimeout> | undefined;
|
||||
const timeout = new Promise<never>((_, reject) => {
|
||||
timer = setTimeout(() => reject(new Error('the browser blocked it or did not answer')), OPEN_TIMEOUT);
|
||||
});
|
||||
return opening;
|
||||
const all = Promise.all(COLLECTIONS.map((n) => c[n].ready())).then(() => undefined);
|
||||
return Promise.race([all, timeout]).finally(() => clearTimeout(timer));
|
||||
}
|
||||
|
||||
async function doOpenDb(uid: string): Promise<void> {
|
||||
@@ -284,10 +301,23 @@ async function doOpenDb(uid: string): Promise<void> {
|
||||
pushedChanges.clear();
|
||||
applying.clear();
|
||||
const idb = prepareIndexedDB({ databaseName: localDbName(uid), version: SCHEMA_VERSION, schema: schema() });
|
||||
const adapter = new DefaultDataAdapter({ storage: (name) => idb(name) });
|
||||
let opened = false;
|
||||
const adapter = new DefaultDataAdapter({
|
||||
storage: (name) => idb(name),
|
||||
onError: (e) => {
|
||||
if (opened) console.error(e);
|
||||
}
|
||||
});
|
||||
const c = {} as Record<CollectionName, AnyCollection>;
|
||||
for (const name of COLLECTIONS) c[name] = new Collection<AnyDoc, string>(name, adapter, { reactivity });
|
||||
await Promise.all(COLLECTIONS.map((n) => c[n].ready()));
|
||||
try {
|
||||
await ready(c);
|
||||
} catch (e) {
|
||||
await Promise.all(Object.values(c).map((col) => col.dispose().catch(() => {})));
|
||||
userId = '';
|
||||
throw e;
|
||||
}
|
||||
opened = true;
|
||||
if (gen !== epoch) {
|
||||
await Promise.all(Object.values(c).map((col) => col.dispose().catch(() => {})));
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user