import { ref, onUnmounted, watch, type Ref } from 'vue'; import { useSharedWebSocket } from './plugin'; /** * Two-way state sync across browser tabs. * - Without callback: reactive ref synced across tabs. * - With callback: called when any tab updates this key — side effects. * * @example * // Reactive two-way sync * const cart = useSocketSync('cart', { items: [] }); * cart.value = { items: [1, 2, 3] }; // syncs to all tabs * * @example * // With side effect callback * const cart = useSocketSync('cart', { items: [] }, (cart) => { * document.title = `Cart (${cart.items.length})`; * analytics.track('cart_updated'); * }); */ export function useSocketSync(key: string, initialValue: T, callback?: (value: T) => void): Ref { const socket = useSharedWebSocket(); const value = ref(socket.getSync(key) ?? initialValue) as Ref; const unsub = socket.onSync(key, (v) => { value.value = v; callback?.(v); }); watch( value, (newVal) => { socket.sync(key, newVal); }, { deep: true }, ); onUnmounted(unsub); return value; }