playback-storage.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /**
  2. * Playback Storage - Persist playback engine state to IndexedDB
  3. *
  4. * Stores minimal state needed to resume playback from a breakpoint:
  5. * position (sceneIndex + actionIndex) and consumed discussions.
  6. */
  7. import { db } from './database';
  8. export interface PlaybackSnapshot {
  9. sceneIndex: number;
  10. actionIndex: number;
  11. consumedDiscussions: string[];
  12. sceneId?: string; // Scene this snapshot belongs to; discard on mismatch
  13. }
  14. /**
  15. * Save playback state for a stage.
  16. * Each stage has at most one playback state record.
  17. */
  18. export async function savePlaybackState(
  19. stageId: string,
  20. snapshot: PlaybackSnapshot,
  21. ): Promise<void> {
  22. await db.playbackState.put({
  23. stageId,
  24. sceneIndex: snapshot.sceneIndex,
  25. actionIndex: snapshot.actionIndex,
  26. consumedDiscussions: snapshot.consumedDiscussions,
  27. sceneId: snapshot.sceneId,
  28. updatedAt: Date.now(),
  29. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  30. } as any);
  31. }
  32. /**
  33. * Load playback state for a stage.
  34. * Returns null if no saved state exists.
  35. */
  36. export async function loadPlaybackState(stageId: string): Promise<PlaybackSnapshot | null> {
  37. const record = await db.playbackState.get(stageId);
  38. if (!record) return null;
  39. return {
  40. sceneIndex: record.sceneIndex,
  41. actionIndex: record.actionIndex,
  42. consumedDiscussions: record.consumedDiscussions,
  43. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  44. sceneId: (record as any).sceneId as string | undefined,
  45. };
  46. }
  47. /**
  48. * Clear playback state for a stage (e.g. on playback complete or stop).
  49. */
  50. export async function clearPlaybackState(stageId: string): Promise<void> {
  51. await db.playbackState.delete(stageId);
  52. }