All files / src/services presence.ts

100% Statements 10/10
83.33% Branches 5/6
100% Functions 6/6
100% Lines 9/9

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      1x   7x     5x       2x 2x       1x       5x       2x     7x    
import { defineStore } from 'pinia'
import { ref } from 'vue'
 
export const usePresenceStore = defineStore('presence', () => {
  // listId → Set of online deviceIds
  const onlineByList = ref<Record<string, Set<string>>>({})
 
  function setSnapshot(listId: string, devices: string[]) {
    onlineByList.value[listId] = new Set(devices)
  }
 
  function addDevice(listId: string, deviceId: string) {
    if (!onlineByList.value[listId]) onlineByList.value[listId] = new Set()
    onlineByList.value[listId]!.add(deviceId)
  }
 
  function removeDevice(listId: string, deviceId: string) {
    onlineByList.value[listId]?.delete(deviceId)
  }
 
  function getCount(listId: string): number {
    return onlineByList.value[listId]?.size ?? 0
  }
 
  function isOnline(listId: string, deviceId: string): boolean {
    return onlineByList.value[listId]?.has(deviceId) ?? false
  }
 
  return { onlineByList, setSnapshot, addDevice, removeDevice, getCount, isOnline }
})