Skip to content

A lightweight Clip Studio Paint alternative with real-time multiplayer collaborative drawing. Built with Tauri and React.

3.0 KB
Raw Blame
import type { ToolType } from '../types/editor';

export interface PeerCursorState {
  peerId: string | number;
  x: number;
  y: number;
  isDrawing: boolean;
  tool: ToolType;
  lastActive: number;
}

export interface PeerCursorPatch {
  x?: number;
  y?: number;
  isDrawing?: boolean;
  tool?: ToolType;
  lastActive?: number;
}

export type PeerCursorListener = (peerKey: string | null) => void;

export interface PeerCursorStore {
  /** Direct mutable map keyed by `String(peerId)` for zero-allocation reads inside rAF loops. */
  readonly cursors: Map<string, PeerCursorState>;
  get: (peerId: string | number) => PeerCursorState | undefined;
  update: (peerId: string | number, patch: PeerCursorPatch) => PeerCursorState | null;
  delete: (peerId: string | number) => void;
  clear: () => void;
  subscribe: (listener: PeerCursorListener) => () => void;
}

/**
 * Lightweight mutable store for high-frequency (40–60 Hz) ephemeral peer cursor kinematics
 * (`x`, `y`, `isDrawing`, `tool`, `lastActive`). Completely decoupled from React state so
 * incoming `CURSOR_MOVE` / `STROKE_CHUNK` bursts trigger zero React re-renders.
 */
export function createPeerCursorStore(): PeerCursorStore {
  const cursors = new Map<string, PeerCursorState>();
  const listeners = new Set<PeerCursorListener>();

  const notify = (peerKey: string | null) => {
    listeners.forEach((listener) => {
      listener(peerKey);
    });
  };

  return {
    cursors,

    get(peerId: string | number): PeerCursorState | undefined {
      return cursors.get(String(peerId));
    },

    update(peerId: string | number, patch: PeerCursorPatch): PeerCursorState | null {
      const key = String(peerId);
      const existing = cursors.get(key);
      const hasX = typeof patch.x === 'number' && Number.isFinite(patch.x);
      const hasY = typeof patch.y === 'number' && Number.isFinite(patch.y);

      if (existing) {
        if (hasX) existing.x = patch.x!;
        if (hasY) existing.y = patch.y!;
        if (patch.isDrawing !== undefined) existing.isDrawing = patch.isDrawing;
        if (patch.tool !== undefined) existing.tool = patch.tool;
        existing.lastActive = patch.lastActive ?? Date.now();
        notify(key);
        return existing;
      }

      if (!hasX || !hasY) {
        return null;
      }

      const created: PeerCursorState = {
        peerId,
        x: patch.x!,
        y: patch.y!,
        isDrawing: Boolean(patch.isDrawing),
        tool: patch.tool ?? 'brush',
        lastActive: patch.lastActive ?? Date.now(),
      };
      cursors.set(key, created);
      notify(key);
      return created;
    },

    delete(peerId: string | number): void {
      const key = String(peerId);
      if (cursors.delete(key)) {
        notify(key);
      }
    },

    clear(): void {
      if (cursors.size === 0) return;
      cursors.clear();
      notify(null);
    },

    subscribe(listener: PeerCursorListener): () => void {
      listeners.add(listener);
      return () => {
        listeners.delete(listener);
      };
    },
  };
}