All files / app-play/src storage-persistence.ts

100% Statements 7/7
100% Branches 4/4
100% Functions 1/1
100% Lines 5/5

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                              6x 4x 4x 3x   1x      
// Asks the browser to keep the player's storage (best records, settings, replays, saved courses, the offline caches)
// when it runs short of space. An installed player asks: Chromium grants it to an installed app without a prompt, and
// Firefox and Safari decide on their own terms.
 
/** The part of `navigator.storage` this reads. */
export interface PersistableStorage {
  persisted?(): Promise<boolean>;
  persist?(): Promise<boolean>;
}
 
/**
 * Asks for persistent storage unless it is already granted. Resolves whether the storage is persistent, or `undefined`
 * where the browser has no such API or the call fails.
 */
export async function requestPersistentStorage(storage: PersistableStorage | undefined): Promise<boolean | undefined> {
  if (storage?.persist === undefined) return undefined;
  try {
    if ((await storage.persisted?.()) === true) return true;
    return await storage.persist();
  } catch {
    return undefined;
  }
}