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

96.61% Statements 57/59
92.85% Branches 26/28
100% Functions 21/21
96.42% Lines 54/56

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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157                                                                                    3x 3x 3x 3x 3x       3x   3x 3x 4x   3x 4x   3x 4x   3x 4x 4x   3x 8x 8x 8x 8x 8x 5x         3x 8x 8x 4x   4x     3x 1x   3x 3x 2x 2x   1x 3x 1x 1x 1x 1x     1x 1x 1x         1x   1x                 2x     2x     2x           2x       2x             2x     2x       1x     3x        
import { effect, effectScope, signal } from 'alien-signals';
import type { PlayerJudgeComboSignalState, PlayerUIFramePayload } from '@be-music/player-engine/signals';
import type { PlayerUIFramePatch } from './ui-frame-patch.ts';
 
type WritableSignal<T> = {
  (): T;
  (value: T): void;
};
 
interface TerminalSizeState {
  columns: number | undefined;
  rows: number | undefined;
}
 
export interface CreateUIWorkerFrameStateOptions {
  initialPaused: boolean;
  initialHighSpeed: number;
  initialJudgeCombo: PlayerJudgeComboSignalState;
  applyPaused: (value: boolean) => void;
  applyHighSpeed: (value: number) => void;
  applyJudgeCombo: (state: Readonly<PlayerJudgeComboSignalState>) => void;
  applyTerminalSize: (columns: number | undefined, rows: number | undefined) => void;
  syncFrameLayout: (
    frame: PlayerUIFramePayload | undefined,
    columns: number | undefined,
    rows: number | undefined,
  ) => void;
  requestFrameRender: () => void;
}
 
export interface UIWorkerFrameState {
  getFrame: () => PlayerUIFramePayload | undefined;
  setFrame: (frame: PlayerUIFramePatch) => void;
  setPaused: (value: boolean) => void;
  setHighSpeed: (value: number) => void;
  setJudgeCombo: (state: PlayerJudgeComboSignalState) => void;
  setTerminalSize: (columns: number | undefined, rows: number | undefined) => void;
  invalidateFrame: () => void;
  dispose: () => void;
}
 
export function createUIWorkerFrameState(options: CreateUIWorkerFrameStateOptions): UIWorkerFrameState {
  const frame = signal<PlayerUIFramePayload | undefined>(undefined);
  const paused = signal(options.initialPaused);
  const highSpeed = signal(options.initialHighSpeed);
  const judgeCombo = signal(options.initialJudgeCombo);
  const terminalSize = signal<TerminalSizeState>({
    columns: undefined,
    rows: undefined,
  });
  const refreshTick = signal(0);
 
  const stopEffects = effectScope(() => {
    effect(() => {
      options.applyPaused(paused());
    });
    effect(() => {
      options.applyHighSpeed(highSpeed());
    });
    effect(() => {
      options.applyJudgeCombo(judgeCombo());
    });
    effect(() => {
      const size = terminalSize();
      options.applyTerminalSize(size.columns, size.rows);
    });
    effect(() => {
      const currentFrame = frame();
      const size = terminalSize();
      refreshTick();
      options.syncFrameLayout(currentFrame, size.columns, size.rows);
      if (currentFrame) {
        options.requestFrameRender();
      }
    });
  });
 
  const updateSignal = <T>(target: WritableSignal<T>, next: T, equals: (left: T, right: T) => boolean): void => {
    const current = target();
    if (equals(current, next)) {
      return;
    }
    target(next);
  };
 
  return {
    getFrame: () => frame(),
    setFrame: (nextFrame) => {
      const current = frame();
      if (!current) {
        frame(nextFrame as PlayerUIFramePayload);
        return;
      }
      let nextNotes = nextFrame.notes ?? current.notes;
      if (nextFrame.noteStateUpdates && nextNotes.length > 0) {
        const mutableNotes = nextNotes.map((note) => ({ ...note }));
        for (const update of nextFrame.noteStateUpdates) {
          const target = mutableNotes[update.index];
          Iif (!target) {
            continue;
          }
          target.judged = update.judged;
          if (typeof update.visibleUntilBeat === 'number' && Number.isFinite(update.visibleUntilBeat)) {
            target.visibleUntilBeat = update.visibleUntilBeat;
          } else E{
            delete target.visibleUntilBeat;
          }
        }
        nextNotes = mutableNotes;
      }
      frame({
        ...current,
        ...nextFrame,
        notes: nextNotes,
        landmineNotes: nextFrame.landmineNotes ?? current.landmineNotes,
        invisibleNotes: nextFrame.invisibleNotes ?? current.invisibleNotes,
      });
    },
    setPaused: (value) => {
      updateSignal(paused, value, (left, right) => left === right);
    },
    setHighSpeed: (value) => {
      updateSignal(highSpeed, value, (left, right) => Math.abs(left - right) < 1e-9);
    },
    setJudgeCombo: (state) => {
      const nextState: PlayerJudgeComboSignalState = {
        judge: state.judge,
        combo: state.combo,
        channel: state.channel,
        updatedAtMs: state.updatedAtMs,
      };
      updateSignal(
        judgeCombo,
        nextState,
        (left, right) =>
          left.judge === right.judge &&
          left.combo === right.combo &&
          left.channel === right.channel &&
          left.updatedAtMs === right.updatedAtMs,
      );
    },
    setTerminalSize: (columns, rows) => {
      updateSignal(
        terminalSize,
        { columns, rows },
        (left, right) => left.columns === right.columns && left.rows === right.rows,
      );
    },
    invalidateFrame: () => {
      refreshTick(refreshTick() + 1);
    },
    dispose: () => {
      stopEffects();
    },
  };
}