All files / player-terminal/src/node ui-frame-patch.ts

100% Statements 20/20
72.72% Branches 8/11
100% Functions 3/3
100% Lines 18/18

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                                          11x   11x   11x 14x 14x 13x 13x 13x       13x     1x 1x 1x 1x 1x 1x 1x       1x               1x                  
import type { PlayerUIFrameNote, PlayerUIFramePayload } from '@be-music/player-engine/signals';
 
export interface PlayerUIFrameNoteStateUpdate {
  index: number;
  judged: boolean;
  visibleUntilBeat?: number;
}
 
export interface PlayerUIFramePatch extends Omit<PlayerUIFramePayload, 'notes' | 'landmineNotes' | 'invisibleNotes'> {
  notes?: PlayerUIFrameNote[];
  landmineNotes?: PlayerUIFrameNote[];
  invisibleNotes?: PlayerUIFrameNote[];
  noteStateUpdates?: PlayerUIFrameNoteStateUpdate[];
}
 
interface NoteStateSnapshot {
  judged: boolean;
  visibleUntilBeat?: number;
}
 
export function createUIFramePatchBuilder(): (frame: Readonly<PlayerUIFramePayload>) => PlayerUIFramePatch {
  let staticCollectionsSent = false;
  let lastNotes: ReadonlyArray<PlayerUIFrameNote> | undefined;
  let noteStateSnapshots: NoteStateSnapshot[] = [];
 
  return (frame) => {
    const notesChanged = lastNotes !== frame.notes;
    if (!staticCollectionsSent || notesChanged) {
      staticCollectionsSent = true;
      lastNotes = frame.notes;
      noteStateSnapshots = frame.notes.map((note) => ({
        judged: note.judged,
        visibleUntilBeat: note.visibleUntilBeat,
      }));
      return frame;
    }
 
    const noteStateUpdates: PlayerUIFrameNoteStateUpdate[] = [];
    for (let index = 0; index < frame.notes.length; index += 1) {
      const note = frame.notes[index]!;
      const previous = noteStateSnapshots[index];
      const nextVisibleUntilBeat = note.visibleUntilBeat;
      Eif (!previous || previous.judged !== note.judged || previous.visibleUntilBeat !== nextVisibleUntilBeat) {
        noteStateSnapshots[index] = {
          judged: note.judged,
          visibleUntilBeat: nextVisibleUntilBeat,
        };
        noteStateUpdates.push({
          index,
          judged: note.judged,
          visibleUntilBeat: nextVisibleUntilBeat,
        });
      }
    }
 
    return {
      ...frame,
      notes: undefined,
      landmineNotes: undefined,
      invisibleNotes: undefined,
      noteStateUpdates: noteStateUpdates.length > 0 ? noteStateUpdates : undefined,
    };
  };
}