All files / web/src/lib/arcade yjs-persistence.ts

0% Statements 0/67
0% Branches 0/1
0% Functions 0/1
0% Lines 0/67

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                                                                                                                                       
/**
 * Yjs persistence helpers
 * Sync Y.Doc state with arcade sessions database
 */

import type * as Y from 'yjs'
import type { GridCell } from '@/arcade-games/yjs-demo/types'

/**
 * Extract grid cells from a Y.Doc for persistence
 * @param doc - The Yjs document
 * @param arrayName - Name of the Y.Array containing cells (default: 'cells')
 * @returns Array of grid cells
 */
export function extractCellsFromDoc(doc: any, arrayName = 'cells'): GridCell[] {
  const cellsArray = doc.getArray(arrayName)
  if (!cellsArray) return []

  const cells: GridCell[] = []
  cellsArray.forEach((cell: GridCell) => {
    cells.push(cell)
  })
  return cells
}

/**
 * Populate a Y.Doc with cells from persisted state
 * @param doc - The Yjs document
 * @param cells - Array of grid cells to restore
 * @param arrayName - Name of the Y.Array to populate (default: 'cells')
 */
export function populateDocWithCells(doc: any, cells: GridCell[], arrayName = 'cells'): void {
  const cellsArray = doc.getArray(arrayName)

  // Clear existing cells first
  cellsArray.delete(0, cellsArray.length)

  // Add persisted cells
  if (cells.length > 0) {
    doc.transact(() => {
      cellsArray.push(cells)
    })
  }
}

/**
 * Serialize Y.Doc to a compact format for database storage
 * Uses Yjs's built-in state vector encoding
 * @param doc - The Yjs document
 * @returns Base64-encoded document state
 */
export function serializeDoc(doc: any): string {
  const Y = require('yjs')
  const state = Y.encodeStateAsUpdate(doc)
  return Buffer.from(state).toString('base64')
}

/**
 * Restore Y.Doc from serialized state
 * @param doc - The Yjs document to populate
 * @param serialized - Base64-encoded document state
 */
export function deserializeDoc(doc: any, serialized: string): void {
  const Y = require('yjs')
  const state = Buffer.from(serialized, 'base64')
  Y.applyUpdate(doc, state)
}