All files / app-play/src/services replay-slot-database.ts

92.1% Statements 70/76
71.42% Branches 15/21
83.33% Functions 25/30
96.87% Lines 62/64

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128        1x   1x   1x   1x   1x 1x       38x 38x 38x 4x 12x     38x 38x         70x 70x 70x         38x 38x 38x   38x         18x 18x   18x 18x           10x 10x 10x           10x 10x 10x     9x 10x   10x 2x 2x   10x 10x 10x           10x 38x   38x       38x 38x   10x   8x     18x 18x     12x 12x     12x     8x 8x 8x 8x 8x 8x 1x 1x   8x 7x 7x   8x        
import { planReplaySlotRevision } from './replay-slot-revision.ts';
import type { ReplaySlotBackend } from './replay-slots.ts';
 
/** The replay slots' database. */
export const REPLAY_SLOT_DB_NAME = 'be-music-replays';
/** Bumped when a store is added; the shape of what the stores hold is the storage revision's. */
const REPLAY_SLOT_DB_VERSION = 2;
/** Replay metadata and the slots selecting each replay. */
const REPLAY_SLOTS_STORE = 'replaySlots';
/** The gzip-compressed replay of each history entry, by its record key. */
const REPLAY_SLOT_DATA_STORE = 'replaySlotData';
/** The storage revision, under {@link REVISION_KEY}. */
const META_STORE = 'meta';
const REVISION_KEY = 'revision';
 
/** Opens the replay slots' database, creating its stores. Rejects where IndexedDB is unavailable or blocked. */
function openReplaySlotDatabase(): Promise<IDBDatabase> {
  return new Promise((resolve, reject) => {
    const request = indexedDB.open(REPLAY_SLOT_DB_NAME, REPLAY_SLOT_DB_VERSION);
    request.onupgradeneeded = () => {
      for (const store of [REPLAY_SLOTS_STORE, REPLAY_SLOT_DATA_STORE, META_STORE]) {
        if (!request.result.objectStoreNames.contains(store)) request.result.createObjectStore(store);
      }
    };
    request.onsuccess = () => resolve(request.result);
    request.onerror = () => reject(request.error ?? new Error('indexedDB open failed'));
  });
}
 
function request<T>(target: IDBRequest<T>): Promise<T> {
  return new Promise((resolve, reject) => {
    target.onsuccess = () => resolve(target.result);
    target.onerror = () => reject(target.error ?? new Error('indexedDB request failed'));
  });
}
 
async function withOpenDatabase<T>(action: (db: IDBDatabase) => Promise<T>): Promise<T> {
  const db = await openReplaySlotDatabase();
  try {
    return await action(db);
  } finally {
    db.close();
  }
}
 
function complete(transaction: IDBTransaction): Promise<void> {
  return new Promise((resolve, reject) => {
    transaction.oncomplete = () => resolve();
    // A quota error aborts the transaction: nothing it wrote or removed is kept.
    transaction.onabort = () => reject(transaction.error ?? new Error('indexedDB write aborted'));
    transaction.onerror = () => undefined;
  });
}
 
/** Brings the stored slots to this build's storage revision, in one transaction. */
async function upgradeStoredSlots(db: IDBDatabase): Promise<void> {
  const transaction = db.transaction([REPLAY_SLOTS_STORE, REPLAY_SLOT_DATA_STORE, META_STORE], 'readwrite');
  const done = complete(transaction);
  const requests = Promise.all([
    request(transaction.objectStore(REPLAY_SLOTS_STORE).getAllKeys()),
    request(transaction.objectStore(REPLAY_SLOTS_STORE).getAll()),
    request(transaction.objectStore(REPLAY_SLOT_DATA_STORE).getAllKeys()),
    request(transaction.objectStore(META_STORE).get(REVISION_KEY)),
  ]);
  void done.catch(() => undefined);
  const [keys, values, dataKeys, revision] = await requests;
  const plan = planReplaySlotRevision({
    storedRevision: typeof revision === 'number' ? revision : undefined,
    // Slot keys are strings; a key of any other type is not one this player wrote, and is left alone.
    records: keys.flatMap((key, index) => (typeof key === 'string' ? [{ key, value: values[index] }] : [])),
    dataKeys: dataKeys.filter((key): key is string => typeof key === 'string'),
  });
  for (const key of plan.remove) {
    transaction.objectStore(REPLAY_SLOTS_STORE).delete(key);
    transaction.objectStore(REPLAY_SLOT_DATA_STORE).delete(key);
  }
  for (const record of plan.put) transaction.objectStore(REPLAY_SLOTS_STORE).put(record, record.key);
  if (plan.revision !== undefined) transaction.objectStore(META_STORE).put(plan.revision, REVISION_KEY);
  await done;
}
 
/** The replay slots kept in their IndexedDB database, at the storage revision this build writes. */
export function createIndexedDBReplaySlotBackend(): ReplaySlotBackend {
  let upgraded: Promise<void> | undefined;
  const withDatabase = async <T>(action: (db: IDBDatabase) => Promise<T>): Promise<T> =>
    withOpenDatabase(async (db) => {
      // A failed upgrade is tried again on the next use.
      upgraded ??= upgradeStoredSlots(db).catch((error: unknown) => {
        upgraded = undefined;
        throw error;
      });
      await upgraded;
      return action(db);
    });
  return {
    ...(typeof navigator.locks?.request === 'function'
      ? { exclusive: <T>(action: () => Promise<T>) => navigator.locks.request(REPLAY_SLOT_DB_NAME, action) }
      : {}),
    listRecords: () =>
      withDatabase((db) =>
        request(db.transaction(REPLAY_SLOTS_STORE, 'readonly').objectStore(REPLAY_SLOTS_STORE).getAll()),
      ),
    readData: (key) =>
      withDatabase(async (db) => {
        const value: unknown = await request(
          db.transaction(REPLAY_SLOT_DATA_STORE, 'readonly').objectStore(REPLAY_SLOT_DATA_STORE).get(key),
        );
        return value instanceof Blob ? value : undefined;
      }),
    write: (entry, remove, update = []) =>
      withDatabase((db) => {
        const transaction = db.transaction([REPLAY_SLOTS_STORE, REPLAY_SLOT_DATA_STORE], 'readwrite');
        const records = transaction.objectStore(REPLAY_SLOTS_STORE);
        const data = transaction.objectStore(REPLAY_SLOT_DATA_STORE);
        for (const record of update) records.put(record, record.key);
        for (const key of remove) {
          records.delete(key);
          data.delete(key);
        }
        if (entry !== undefined) {
          records.put(entry.record, entry.record.key);
          data.put(entry.data, entry.record.key);
        }
        return complete(transaction);
      }),
  };
}