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 | 3x 3x 3x 3x 3x 44x 3x 44x 2x 40x 12x 12x 12x 3x 3x 3x 3x 6x 6x 6x 2x 4x 6x 5x 5x 6x 6x 6x 5x 5x 5x 3x 3x 3x 3x 5x 48x 6x 11x 6x 4x 4x 3x 3x 2x 2x 1x 4x 4x 5x 5x 4x 4x 4x 4x | // Parsed charts kept across page loads in IndexedDB (`be-music-charts`), keyed by the SHA-256 of each chart file and
// the kind of its name (`parsedChartCacheKey`), so a folder dropped again lists its charts without parsing them again.
// What a parse produces belongs to the build that parsed it: a build with another stamp clears the store first.
import type { ParseChartTaskResult } from '@be-music/player-browser';
import type { ParsedChartCache } from '@be-music/player-browser/collection';
/** The parsed charts' database. */
export const PARSED_CHART_DB_NAME = 'be-music-charts';
const PARSED_CHART_DB_VERSION = 1;
/** Each chart's parse result (`ParseChartTaskResult`), by its cache key. */
const PARSED_CHARTS_STORE = 'parsedCharts';
/** The stamp of the build that wrote the parse results, under {@link BUILD_KEY}. */
const META_STORE = 'meta';
const BUILD_KEY = 'build';
/**
* The stamp a build keeps its parse results under: its source revision and build time, so any parser change a new
* build brings (or a restarted dev server picks up) starts from an empty store. Empty when neither is known, in which
* case nothing is cached.
*/
export function parsedChartBuildStamp(info: { revision: string; builtAt: string } | undefined): string {
if (info === undefined) return '';
const revision = info.revision === 'unknown' ? '' : info.revision;
if (revision.length === 0 && info.builtAt.length === 0) return '';
return `${revision}@${info.builtAt}`;
}
/** This build's stamp, from the build info `vite.config.ts` defines; empty where it defines none (tests). */
export function currentParsedChartBuildStamp(): string {
return parsedChartBuildStamp(
typeof __BE_MUSIC_BUILD_TIME__ === 'string'
? { revision: __BE_MUSIC_BUILD_REVISION__, builtAt: __BE_MUSIC_BUILD_TIME__ }
: undefined,
);
}
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'));
transaction.onerror = () => undefined;
});
}
function openParsedChartDatabase(factory: IDBFactory): Promise<IDBDatabase> {
return new Promise((resolve, reject) => {
const open = factory.open(PARSED_CHART_DB_NAME, PARSED_CHART_DB_VERSION);
open.onupgradeneeded = () => {
for (const store of [PARSED_CHARTS_STORE, META_STORE]) {
Eif (!open.result.objectStoreNames.contains(store)) open.result.createObjectStore(store);
}
};
open.onsuccess = () => {
// Another tab upgrading the database must not wait on this one.
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'));
});
}
/** Opens the database and clears the parse results another build wrote. */
async function openForBuild(factory: IDBFactory, stamp: string): Promise<IDBDatabase> {
const db = await openParsedChartDatabase(factory);
try {
const stored = await request(db.transaction(META_STORE, 'readonly').objectStore(META_STORE).get(BUILD_KEY));
if (stored !== stamp) {
const write = db.transaction([PARSED_CHARTS_STORE, META_STORE], 'readwrite');
write.objectStore(PARSED_CHARTS_STORE).clear();
write.objectStore(META_STORE).put(stamp, BUILD_KEY);
await complete(write);
}
return db;
} catch (error) {
db.close();
throw error;
}
}
/**
* The parsed-chart cache in IndexedDB, or `undefined` where there is no IndexedDB or no build stamp. The database
* opens on the first lookup and stays open for the page; if it cannot be opened, every lookup misses and nothing is
* kept, so loads parse as they would without a cache.
*/
export function createIndexedDBParsedChartCache(
stamp: string,
factory: IDBFactory | undefined = globalThis.indexedDB,
): ParsedChartCache | undefined {
if (factory === undefined || stamp.length === 0) return undefined;
let opened: Promise<IDBDatabase | undefined> | undefined;
const database = (): Promise<IDBDatabase | undefined> =>
(opened ??= openForBuild(factory, stamp).catch(() => undefined));
return {
async get(key) {
const db = await database();
if (db === undefined) return undefined;
const value: unknown = await request(
db.transaction(PARSED_CHARTS_STORE, 'readonly').objectStore(PARSED_CHARTS_STORE).get(key),
);
return value === undefined ? undefined : (value as ParseChartTaskResult);
},
async getMany(keys) {
const db = await database();
if (db === undefined) return keys.map(() => undefined);
// One transaction for the lot: each request still answers on its own, but none waits for another's round trip.
const store = db.transaction(PARSED_CHARTS_STORE, 'readonly').objectStore(PARSED_CHARTS_STORE);
const values: unknown[] = await Promise.all(keys.map((key) => request(store.get(key))));
return values.map((value) => (value === undefined ? undefined : (value as ParseChartTaskResult)));
},
async set(key, value) {
const db = await database();
if (db === undefined) return;
const transaction = db.transaction(PARSED_CHARTS_STORE, 'readwrite');
// `put` copies the value now; the write lands on its own. A quota error only aborts this one entry.
transaction.objectStore(PARSED_CHARTS_STORE).put(value, key);
transaction.onabort = () => undefined;
transaction.onerror = (event) => event.preventDefault();
},
};
}
|