All files / src/composables useSyncQueue.ts

51.68% Statements 46/89
35.71% Branches 10/28
35.71% Functions 5/14
57.33% Lines 43/75

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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180                              1x 1x                   8x     8x         8x           8x 1x 1x     8x                                                                                                                                 7x 7x 7x 7x     7x 7x       7x 7x           7x             7x 7x   7x 7x 7x     6x 10x 10x 10x     6x 6x   6x 6x   6x 9x 9x 8x 7x     7x     2x       6x 5x 5x     6x   7x      
import { watch, onMounted } from 'vue'
import { OperationQueue } from '../crdt/OperationQueue'
import api from '../services/api'
import { useOffline } from './useOffline'
import { useItemsStore } from '../stores/items'
import { useListsStore } from '../stores/lists'
import { onReconnect } from '../services/websocket'
import { listService } from '../services/list'
import { CacheService } from '../services/cache'
import { cacheDb } from '../services/db'
import { LocalClockService } from '../services/clock'
import { getDeviceId } from '../services/device'
import { applyOp } from './useListSync'
import type { CrdtOperation } from '../crdt/types'
 
let flushInProgress = false
let reconnectListenerRegistered = false
 
/**
 * Composable that watches the online state and flushes any queued CRDT
 * operations to the server whenever connectivity is restored.
 *
 * Mount once at the App root. Each list-detail view enqueues operations
 * while offline via OperationQueue.enqueue(); this composable drains them.
 */
export function useSyncQueue() {
  const { isOnline } = useOffline()
 
  // Flush ops queued in a previous session (app opened while already online)
  onMounted(async () => {
    if (isOnline.value) await flushAll()
  })
 
  // Flush when navigator.onLine recovers (full network loss scenario)
  watch(isOnline, async (online) => {
    if (online) await flushAll()
  })
 
  // Also flush when the WebSocket reconnects after a server-side drop
  // (navigator.onLine stays true, but the WS connection was lost temporarily)
  if (!reconnectListenerRegistered) {
    reconnectListenerRegistered = true
    onReconnect(() => flushAll())
  }
 
  return { flushQueue }
}
 
async function flushAll(): Promise<void> {
  await flushPendingLists()
  await flushQueue()
  // No fetchAll here: optimistic state in the store + cache is already correct,
  // and pullRemoteOps (called inside flushQueue) applies any remote changes via applyOp.
  // Calling fetchAll here races with the POST and can overwrite correct local state
  // with stale server data if the response arrives before the server processes our ops.
}
 
/**
 * Creates any lists that were created offline (with a temp UUID) on the server,
 * then remaps the temp UUID to the server-assigned UUID in the Pinia store,
 * IndexedDB cache, and OperationQueue so subsequent CRDT op flush uses the
 * correct listId.
 */
async function flushPendingLists(): Promise<void> {
  const pending = await cacheDb.pendingLists.toArray()
  if (pending.length === 0) return
 
  const listsStore = useListsStore()
  const itemsStore = useItemsStore()
 
  for (const p of pending) {
    try {
      const serverList = await listService.create({ name: p.name, emoji: p.emoji })
      const serverId = serverList.id
 
      // Remap any queued CRDT ops (e.g. ITEM_CREATE) from tempId → serverId
      await OperationQueue.remapListId(p.tempId, serverId)
 
      // Remap cached items from tempId → serverId
      const cachedItems = await CacheService.getItems(p.tempId)
      await cacheDb.lists.delete(p.tempId)
      await cacheDb.items.where('listId').equals(p.tempId).delete()
      await CacheService.saveList(serverList)
      if (cachedItems.length > 0) {
        await CacheService.saveItems(serverId, cachedItems.map(i => ({ ...i, listId: serverId })))
      }
 
      // Remap Pinia store: replace the temp list entry in-place
      const listIdx = listsStore.lists.findIndex(l => l.id === p.tempId)
      if (listIdx !== -1) listsStore.lists[listIdx] = serverList
 
      // Remap items store from tempId → serverId
      const tempItems = itemsStore.itemsByList[p.tempId]
      if (tempItems) {
        itemsStore.itemsByList[serverId] = tempItems.map(i => ({ ...i, listId: serverId }))
        delete itemsStore.itemsByList[p.tempId]
      }
 
      await cacheDb.pendingLists.delete(p.tempId)
    } catch (e) {
      console.warn('[SyncQueue] Failed to flush pending list', p.tempId, e)
    }
  }
}
 
async function pullRemoteOps(
  listId: string,
  myDeviceId: string,
  itemsStore: ReturnType<typeof useItemsStore>,
): Promise<void> {
  try {
    const clock = await LocalClockService.getClock(listId)
    const since = JSON.stringify(clock)
    const { data } = await api.get<CrdtOperation[]>(`/lists/${listId}/crdt/ops`, {
      params: { since },
    })
    const remoteOps = (data ?? []).filter((op) => op.deviceId !== myDeviceId)
    for (const op of remoteOps) {
      applyOp(listId, op, itemsStore)
    }
    // Merge the clocks from received ops into local clock
    const merged: Record<string, number> = { ...clock }
    for (const op of remoteOps) {
      const vc = op.vectorClock as Record<string, number>
      for (const [dev, cnt] of Object.entries(vc)) {
        if ((merged[dev] ?? 0) < cnt) merged[dev] = cnt
      }
    }
    await LocalClockService.mergeClock(listId, merged)
  } catch (e) {
    console.warn('[SyncQueue] Failed to pull remote ops for list', listId, e)
  }
}
 
async function flushQueue(): Promise<string[]> {
  Iif (flushInProgress) return []
  flushInProgress = true
 
  try {
    const pending = await OperationQueue.getAllPending()
    if (pending.length === 0) return []
 
    // Group by listId so we can send one batch per list
    const byList = pending.reduce<Record<string, CrdtOperation[]>>((acc, op) => {
      if (!acc[op.listId]) acc[op.listId] = []
      acc[op.listId]!.push(op)
      return acc
    }, {})
 
    const synced: string[] = []
    const flushedLists: string[] = []
 
    const myDeviceId = await getDeviceId()
    const itemsStore = useItemsStore()
 
    for (const [listId, ops] of Object.entries(byList)) {
      try {
        await api.post(`/lists/${listId}/crdt/ops`, ops)
        synced.push(...ops.map(o => o.id))
        flushedLists.push(listId)
 
        // Pull ops from other devices that arrived while we were offline
        await pullRemoteOps(listId, myDeviceId, itemsStore)
      } catch (e) {
        // Leave failed ops in queue — will retry next reconnect
        console.warn('[SyncQueue] Failed to flush ops for list', listId, e)
      }
    }
 
    if (synced.length > 0) {
      await OperationQueue.markAllSynced(synced)
      await OperationQueue.pruneOld()
    }
 
    return flushedLists
  } finally {
    flushInProgress = false
  }
}