All files / app-play/src/services folder-snapshots.ts

61.11% Statements 88/144
55.38% Branches 36/65
47.05% Functions 16/34
64.75% Lines 79/122

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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297                                                                                    2x   2x   2x                                                         2x   1x 2x 2x 2x       2x       2x 2x   2x 2x     1x         1x           1x   1x                                 1x                                                                                           15x       15x   15x         15x 15x       7x       16x 15x 15x           10x 10x   1x   10x 4x 4x 4x 4x   4x 4x         12x 12x 2x 2x   10x 8x 12x 12x 12x 12x 16x 16x 16x           2x     10x 10x 10x 10x                 6x 6x 4x   2x 2x 2x   4x 2x 2x               2x 2x 2x 2x 2x   2x 2x 2x 2x         15x 12x 12x 12x         17x 17x          
// A remembered folder as it was last loaded, kept across page loads in IndexedDB (`be-music-folder-snapshots`): its
// file listing and the parsed-chart cache key of each chart file. The next visit lists the folder's charts from the
// snapshot at once, without walking the folder or reading a chart file, and then walks the folder in the background to
// see whether it changed (`RememberedFolderSession`).
 
import type { BrowserDroppedFile, ChartCacheKeyBook } from '@be-music/player-browser/collection';
import {
  openFolderListing,
  sameFolderListing,
  type FolderChartStat,
  type FolderDirectoryLike,
  type FolderListing,
} from './folder-listing.ts';
import { isSameFolder } from './folder-handle.ts';
 
/** A chart file of a snapshot: how the walk found it, and its parsed-chart cache key when the load worked one out. */
export interface FolderSnapshotChart extends FolderChartStat {
  readonly key?: string;
}
 
/** A folder's listing ({@link FolderListing}) and its charts' cache keys, as of the last load. */
export interface FolderSnapshot {
  readonly version: 1;
  readonly text: string;
  readonly fileCount: number;
  readonly charts: readonly FolderSnapshotChart[];
}
 
/** A folder a snapshot is kept for: a directory handle, compared with `isSameEntry`. */
export interface SnapshotFolder extends FolderDirectoryLike {
  isSameEntry(other: FileSystemHandle): Promise<boolean>;
}
 
/** Where snapshots are kept, one per folder. Reads and writes may fail; the folder is then walked as if it had none. */
export interface FolderSnapshotStore {
  read(folder: SnapshotFolder): Promise<FolderSnapshot | undefined>;
  write(folder: SnapshotFolder, snapshot: FolderSnapshot): Promise<void>;
  /** Forgets the snapshots of every folder not among `folders`. */
  prune(folders: readonly SnapshotFolder[]): Promise<void>;
}
 
/** The snapshots' database. */
export const FOLDER_SNAPSHOT_DB_NAME = 'be-music-folder-snapshots';
/** Each folder's handle, by the id its snapshot is kept under. */
const FOLDERS_STORE = 'folders';
/** Each folder's {@link FolderSnapshot}, by that id. Kept apart so that finding a folder reads no listing. */
const SNAPSHOTS_STORE = 'snapshots';
 
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'));
  });
}
 
function complete(transaction: IDBTransaction): Promise<void> {
  return new Promise((resolve, reject) => {
    transaction.oncomplete = () => resolve();
    transaction.onabort = () => reject(transaction.error ?? new Error('indexedDB write aborted'));
  });
}
 
function isFolderSnapshot(value: unknown): value is FolderSnapshot {
  if (typeof value !== 'object' || value === null) return false;
  const snapshot = value as Partial<FolderSnapshot>;
  return snapshot.version === 1 && typeof snapshot.text === 'string' && Array.isArray(snapshot.charts);
}
 
/**
 * The snapshot store in IndexedDB, or `undefined` where there is none. The database opens on first use and stays open
 * for the page.
 */
export function createIndexedDBFolderSnapshotStore(
  factory: IDBFactory | undefined = globalThis.indexedDB,
): FolderSnapshotStore | undefined {
  if (factory === undefined) return undefined;
  let opened: Promise<IDBDatabase> | undefined;
  const database = (): Promise<IDBDatabase> =>
    (opened ??= new Promise<IDBDatabase>((resolve, reject) => {
      const open = factory.open(FOLDER_SNAPSHOT_DB_NAME, 1);
      open.onupgradeneeded = () => {
        open.result.createObjectStore(FOLDERS_STORE, { autoIncrement: true });
        open.result.createObjectStore(SNAPSHOTS_STORE);
      };
      open.onsuccess = () => {
        open.result.onversionchange = () => open.result.close();
        resolve(open.result);
      };
      open.onerror = () => reject(open.error ?? new Error('indexedDB open failed'));
      open.onblocked = () => reject(new Error('indexedDB open blocked'));
    }).catch((error: unknown) => {
      opened = undefined;
      throw error;
    }));
  /** Every kept folder with its id. */
  const keptFolders = async (db: IDBDatabase): Promise<Array<{ id: IDBValidKey; folder: SnapshotFolder }>> => {
    const store = db.transaction(FOLDERS_STORE, 'readonly').objectStore(FOLDERS_STORE);
    const [ids, folders] = await Promise.all([request(store.getAllKeys()), request(store.getAll())]);
    return ids.map((id, index) => ({ id, folder: folders[index] as SnapshotFolder }));
  };
  const idOf = async (db: IDBDatabase, folder: SnapshotFolder): Promise<IDBValidKey | undefined> => {
    for (const kept of await keptFolders(db)) {
      if (await isSameFolder(kept.folder, folder)) return kept.id;
    }
    return undefined;
  };
  return {
    async read(folder) {
      const db = await database();
      const id = await idOf(db, folder);
      if (id === undefined) return undefined;
      const value: unknown = await request(
        db.transaction(SNAPSHOTS_STORE, 'readonly').objectStore(SNAPSHOTS_STORE).get(id),
      );
      return isFolderSnapshot(value) ? value : undefined;
    },
    async write(folder, snapshot) {
      const db = await database();
      const existing = await idOf(db, folder);
      const transaction = db.transaction([FOLDERS_STORE, SNAPSHOTS_STORE], 'readwrite');
      const id = existing ?? (await request(transaction.objectStore(FOLDERS_STORE).add(folder)));
      transaction.objectStore(SNAPSHOTS_STORE).put(snapshot, id);
      await complete(transaction);
    },
    async prune(folders) {
      const db = await database();
      const stale: IDBValidKey[] = [];
      for (const kept of await keptFolders(db)) {
        let saved = false;
        for (const folder of folders) {
          if (await isSameFolder(kept.folder, folder)) {
            saved = true;
            break;
          }
        }
        if (!saved) stale.push(kept.id);
      }
      if (stale.length === 0) return;
      const transaction = db.transaction([FOLDERS_STORE, SNAPSHOTS_STORE], 'readwrite');
      for (const id of stale) {
        transaction.objectStore(FOLDERS_STORE).delete(id);
        transaction.objectStore(SNAPSHOTS_STORE).delete(id);
      }
      await complete(transaction);
    },
  };
}
 
export interface RememberedFolderSessionDeps {
  /** Walks a folder (`listFolder`, or `listFolderInWorker`). */
  readonly list: (
    folder: FolderDirectoryLike,
    onProgress?: (fileCount: number) => void,
    signal?: AbortSignal,
  ) => Promise<FolderListing>;
  /** Where the folder's snapshot is kept; without one every visit walks the folder. */
  readonly snapshots: FolderSnapshotStore | undefined;
}
 
/**
 * One remembered folder for as long as the page is open: the listing its charts were last loaded from, and the cache
 * key of each of its chart files (the {@link ChartCacheKeyBook} the song collection loads the folder with).
 *
 * A key is offered again only while its file is as large and as old as when the key was worked out, so a chart edited
 * on disk is read again. The first load of a visit takes the folder from its snapshot ({@link restore}), which may be
 * out of date: {@link verify} then walks the folder, and when it changed the library reads it again ({@link read}).
 */
export class RememberedFolderSession implements ChartCacheKeyBook {
  /** The listing the library was last loaded from. */
  private listing: FolderListing | undefined;
  /** The cache keys of the chart files of {@link listing}, by path. */
  private keys = new Map<string, string>();
  /** A walk that found the folder changed, which the next {@link read} takes instead of walking again. */
  private walked: FolderListing | undefined;
  /** The folder could not be walked: the next {@link read} lists nothing. */
  private unreadable = false;
  /** The listing or a key differs from the kept snapshot. */
  private unsaved = false;
  public readonly folder: SnapshotFolder;
  private readonly deps: RememberedFolderSessionDeps;
 
  public constructor(folder: SnapshotFolder, deps: RememberedFolderSessionDeps) {
    this.folder = folder;
    this.deps = deps;
  }
 
  public known(path: string): string | undefined {
    return this.keys.get(path);
  }
 
  public learned(path: string, key: string): void {
    if (this.keys.get(path) === key) return;
    this.keys.set(path, key);
    this.unsaved = true;
  }
 
  /** The folder's files as its snapshot lists them, without touching the folder; `undefined` without a snapshot. */
  public async restore(): Promise<BrowserDroppedFile[] | undefined> {
    let snapshot: FolderSnapshot | undefined;
    try {
      snapshot = await this.deps.snapshots?.read(this.folder);
    } catch {
      snapshot = undefined;
    }
    if (snapshot === undefined) return undefined;
    this.listing = { text: snapshot.text, fileCount: snapshot.fileCount, charts: snapshot.charts };
    this.keys = new Map();
    for (const chart of snapshot.charts) {
      if (chart.key !== undefined) this.keys.set(chart.path, chart.key);
    }
    this.unsaved = false;
    return openFolderListing(this.folder, snapshot.text);
  }
 
  /** The folder's files as they are now: the walk {@link verify} made, or a new one. */
  public async read(onProgress?: (fileCount: number) => void, signal?: AbortSignal): Promise<BrowserDroppedFile[]> {
    signal?.throwIfAborted();
    if (this.unreadable) {
      this.unreadable = false;
      return [];
    }
    const listing = this.walked ?? (await this.deps.list(this.folder, onProgress, signal));
    signal?.throwIfAborted();
    this.walked = undefined;
    const previous = new Map(this.listing?.charts.map((chart) => [chart.path, chart]));
    const keys = new Map<string, string>();
    for (const chart of listing.charts) {
      const before = previous.get(chart.path);
      const key = this.keys.get(chart.path);
      if (
        key !== undefined &&
        before !== undefined &&
        before.size === chart.size &&
        before.lastModified === chart.lastModified
      ) {
        keys.set(chart.path, key);
      }
    }
    Eif (this.listing === undefined || !sameFolderListing(this.listing, listing)) this.unsaved = true;
    this.listing = listing;
    this.keys = keys;
    return openFolderListing(this.folder, listing.text);
  }
 
  /**
   * Walks the folder and tells whether it differs from what the library was loaded from; the library then reads it
   * again ({@link read}). A folder that cannot be walked rejects, and reads as empty from then on.
   */
  public async verify(refreshUnchanged = false, signal?: AbortSignal): Promise<boolean> {
    let listing: FolderListing;
    try {
      listing = await this.deps.list(this.folder, undefined, signal);
      signal?.throwIfAborted();
    } catch (error) {
      signal?.throwIfAborted();
      this.unreadable = true;
      throw error;
    }
    if (!refreshUnchanged && this.listing !== undefined && sameFolderListing(this.listing, listing)) return false;
    this.walked = listing;
    return true;
  }
 
  /**
   * Lists the folder once its charts were loaded another way (a drop's entries, which name the same paths) and keeps
   * its snapshot: the keys that load {@link learned} stay with the chart files the walk finds.
   */
  public async capture(): Promise<void> {
    const listing = await this.deps.list(this.folder);
    const keys = new Map<string, string>();
    for (const chart of listing.charts) {
      const key = this.keys.get(chart.path);
      Eif (key !== undefined) keys.set(chart.path, key);
    }
    this.listing = listing;
    this.keys = keys;
    this.unsaved = true;
    await this.save();
  }
 
  /** Keeps the folder's snapshot, once the library has loaded it, when anything changed since it was last kept. */
  public async save(): Promise<void> {
    if (!this.unsaved || this.listing === undefined || this.deps.snapshots === undefined) return;
    this.unsaved = false;
    const { text, fileCount, charts } = this.listing;
    await this.deps.snapshots.write(this.folder, {
      version: 1,
      text,
      fileCount,
      charts: charts.map((chart) => {
        const key = this.keys.get(chart.path);
        return key === undefined ? { ...chart } : { ...chart, key };
      }),
    });
  }
}