import AsyncStorage from "@react-native-async-storage/async-storage"; import type { ParsedShortcut } from "@/lib/shortcuts"; const STORAGE_KEY = "beenvoice:pending-shortcut"; let memory: ParsedShortcut | null = null; const listeners = new Set<() => void>(); function notify() { for (const listener of listeners) { listener(); } } export async function enqueueShortcut(shortcut: ParsedShortcut): Promise { memory = shortcut; await AsyncStorage.setItem(STORAGE_KEY, JSON.stringify(shortcut)); notify(); } export async function peekPendingShortcut(): Promise { if (memory) return memory; const raw = await AsyncStorage.getItem(STORAGE_KEY); if (!raw) return null; try { memory = JSON.parse(raw) as ParsedShortcut; return memory; } catch { await AsyncStorage.removeItem(STORAGE_KEY); return null; } } export async function clearPendingShortcut(): Promise { memory = null; await AsyncStorage.removeItem(STORAGE_KEY); } export function subscribeShortcutQueue(listener: () => void): () => void { listeners.add(listener); return () => listeners.delete(listener); } export async function hasPendingShortcut(): Promise { return (await peekPendingShortcut()) != null; }