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

85% Statements 34/40
82.35% Branches 14/17
75% Functions 3/4
90.62% Lines 29/32

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      17x           17x                                         10x 10x 10x 10x 13x 13x         10x         8x 8x 8x 5x 5x 5x         8x         4x 4x 4x 4x 4x 11x 11x   4x 5x 5x 5x 5x 4x            
// Storage data shared by settings services and the Options dialog; no UI dependencies.
 
/** Prefix every `localStorage` key the player writes starts with. */
export const PLAYER_STORAGE_PREFIX = 'be-music.';
 
/**
 * Stored keys "Reset settings" leaves alone: the best records are play data, not settings, and have their own
 * "Clear best records" button.
 */
export const PLAYER_STORAGE_KEYS_KEPT_ON_RESET: readonly string[] = [`${PLAYER_STORAGE_PREFIX}chart-best-records`];
 
/** Minimal storage surface, so tests can hand in a stub and blocked storage can throw. */
export interface PlayerStorage {
  getItem(key: string): string | null;
  setItem(key: string, value: string): void;
  removeItem(key: string): void;
  readonly length: number;
  key(index: number): string | null;
}
 
export function defaultStorage(): PlayerStorage | undefined {
  try {
    return globalThis.localStorage;
  } catch {
    return undefined;
  }
}
 
/** The player's stored keys a settings reset removes: every `be-music.*` key except the kept ones. */
export function playerSettingKeys(storage: PlayerStorage | undefined = defaultStorage()): string[] {
  const keys: string[] = [];
  try {
    Iif (!storage) return keys;
    for (let index = 0; index < storage.length; index++) {
      const key = storage.key(index);
      if (key?.startsWith(PLAYER_STORAGE_PREFIX) && !PLAYER_STORAGE_KEYS_KEPT_ON_RESET.includes(key)) keys.push(key);
    }
  } catch {
    // Blocked storage holds nothing to reset.
  }
  return keys;
}
 
/** Removes every player setting (see {@link playerSettingKeys}); returns how many keys were removed. */
export function clearPlayerSettings(storage: PlayerStorage | undefined = defaultStorage()): number {
  const keys = playerSettingKeys(storage);
  let removed = 0;
  for (const key of keys) {
    try {
      storage?.removeItem(key);
      removed++;
    } catch {
      // Keep going: one blocked key shouldn't stop the rest.
    }
  }
  return removed;
}
 
/** Moves the former player namespace once, keeping newer values and removing an old key only after copying it. */
export function migratePlayerStorage(storage: PlayerStorage | undefined = defaultStorage()): void {
  Iif (!storage) return;
  const previousPrefix = 'be-music-demo.';
  try {
    const keys: string[] = [];
    for (let index = 0; index < storage.length; index++) {
      const key = storage.key(index);
      if (key?.startsWith(previousPrefix)) keys.push(key);
    }
    for (const key of keys) {
      const target = `${PLAYER_STORAGE_PREFIX}${key.slice(previousPrefix.length)}`;
      const value = storage.getItem(key);
      Iif (value === null) continue;
      if (storage.getItem(target) === null) storage.setItem(target, value);
      storage.removeItem(key);
    }
  } catch {
    // Leave uncopied data in place when storage is blocked or full; retry on the next visit.
  }
}