Add 'apps/mobile/' from commit '5fa30f365f21531094cd4d2045042bb4f1370ac3'
git-subtree-dir: apps/mobile git-subtree-mainline:86f8987dffgit-subtree-split:5fa30f365f
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
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<void> {
|
||||
memory = shortcut;
|
||||
await AsyncStorage.setItem(STORAGE_KEY, JSON.stringify(shortcut));
|
||||
notify();
|
||||
}
|
||||
|
||||
export async function peekPendingShortcut(): Promise<ParsedShortcut | null> {
|
||||
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<void> {
|
||||
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<boolean> {
|
||||
return (await peekPendingShortcut()) != null;
|
||||
}
|
||||
Reference in New Issue
Block a user