GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — opencode-mobile/shared/network/data-usage.ts at main · drpr/opencode-mobile Expand file tree/ Copy pathdata-usage.tsMore file actions 135 lines (118 loc) · 3.44 KB / Copy pathdata-usage.tsFile metadata and controls135 lines (118 loc) · 3.44 KB 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 import { useSyncExternalStore } from 'react'; import { DATA_USAGE_STORAGE_KEY } from '@/shared/storage/keys'; // Lightweight network usage counter for diagnosing data-heavy sessions // (issue #68). Every app request funnels through fetchConnection in client.ts, // so counting there captures V1, V2, and Cloud Link traffic. Streaming responses // are counted by requests only; their byte stream is not buffered just to // measure it. Totals are persisted per calendar day so a relaunch does not hide // the day's usage. export type DataUsage = { requests: number; bytes: number; }; function dayKey(date = new Date()) { const month = String(date.getMonth() + 1).padStart(2, '0'); const day = String(date.getDate()).padStart(2, '0'); return `${date.getFullYear()}-${month}-${day}`; } let day = dayKey(); let usage: DataUsage = { requests: 0, bytes: 0 }; const listeners = new Set<() => void>(); let saveTimer: ReturnType<typeof setTimeout> | undefined; let hydrated = false; function emit() { for (const listener of listeners) { listener(); } } function rolloverIfNeeded() { const today = dayKey(); if (today !== day) { day = today; usage = { requests: 0, bytes: 0 }; } } function scheduleSave() { if (saveTimer) { clearTimeout(saveTimer); } saveTimer = setTimeout(() => { saveTimer = undefined; void persist(); }, 1000); } async function persist() { try { const AsyncStorage = (await import('@react-native-async-storage/async-storage')).default; await AsyncStorage.setItem(DATA_USAGE_STORAGE_KEY, JSON.stringify({ day, ...usage })); } catch { // Persistence is best-effort; the in-memory counter still drives the UI. } } async function hydrate() { if (hydrated) { return; } hydrated = true; try { const AsyncStorage = (await import('@react-native-async-storage/async-storage')).default; const raw = await AsyncStorage.getItem(DATA_USAGE_STORAGE_KEY); if (!raw) { return; } const record = JSON.parse(raw) as { day?: unknown; requests?: unknown; bytes?: unknown }; if (record.day !== day) { await AsyncStorage.setItem(DATA_USAGE_STORAGE_KEY, JSON.stringify({ day, requests: 0, bytes: 0 })); return; } if (typeof record.requests === 'number' && typeof record.bytes === 'number') { usage = { requests: record.requests, bytes: record.bytes }; emit(); } } catch { // A malformed or unreadable record falls back to a fresh counter. } } void hydrate(); export function recordRequest() { rolloverIfNeeded(); usage = { ...usage, requests: usage.requests + 1 }; emit(); scheduleSave(); } export function recordBytes(bytes: number) { if (!Number.isFinite(bytes) || bytes <= 0) { return; } rolloverIfNeeded(); usage = { ...usage, bytes: usage.bytes + bytes }; emit(); scheduleSave(); } export function getDataUsage(): DataUsage { return usage; } export function resetDataUsage() { day = dayKey(); usage = { requests: 0, bytes: 0 }; emit(); void persist(); } function subscribe(listener: () => void) { listeners.add(listener); return () => { listeners.delete(listener); }; } export function useDataUsage(): DataUsage { return useSyncExternalStore(subscribe, getDataUsage, getDataUsage); } export function formatDataUsage(bytes: number) { if (bytes < 1024) { return `${bytes} B`; } if (bytes < 1024 * 1024) { return `${(bytes / 1024).toFixed(1)} KB`; } return `${(bytes / (1024 * 1024)).toFixed(1)} MB`; } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |