@vibedev-si/dsh-island 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/usage-host.js ADDED
@@ -0,0 +1,93 @@
1
+ import { usageSnapshot, usageUser } from './account-usage.js'
2
+
3
+ const ACCOUNT_PATH = '/api/dsh-vibedev/account'
4
+ const STATS_PATH = '/api/dsh-vibedev/account-center/usage/dashboard/stats'
5
+ const REFRESH_MS = 45_000
6
+ const RECHECK_MS = 10_000
7
+
8
+ /** Read account data through the Host's own authenticated route dispatcher. */
9
+ export function readAccountUsageRoute(connection, path, signal, user) {
10
+ const headers = user === undefined ? undefined : { 'X-VibeDev-Expected-User': user }
11
+ const request = new Request(new URL(path, 'http://localhost'), { signal, headers })
12
+ return connection.createSharedFetchHandler('/api').fetch(request, connection.browserPeer)
13
+ }
14
+
15
+ /** The island receives display fields only. Switches and shutdown cancel in-flight reads. */
16
+ export function createAccountUsagePoll({ read, publish, later, cancel }) {
17
+ let active
18
+ let timer
19
+ let publishedIdentity
20
+ let lastSnapshot = 'null'
21
+
22
+ const update = (snapshot) => {
23
+ const serialized = JSON.stringify(snapshot)
24
+ if (serialized === lastSnapshot) return
25
+ lastSnapshot = serialized
26
+ publish(snapshot)
27
+ }
28
+
29
+ const clear = () => {
30
+ publishedIdentity = undefined
31
+ update(null)
32
+ }
33
+
34
+ const identityOf = (account) => {
35
+ const user = usageUser(account)
36
+ if (user === undefined) return undefined
37
+ return { user, fingerprint: `${account.source}:${user}` }
38
+ }
39
+
40
+ async function refresh(lifetime) {
41
+ let delay = RECHECK_MS
42
+ const signal = AbortSignal.any([lifetime.signal, AbortSignal.timeout(30_000)])
43
+ const current = () => active === lifetime && !lifetime.signal.aborted
44
+ const getAccount = async () => {
45
+ const response = await read(ACCOUNT_PATH, signal)
46
+ return response?.ok ? response.json() : undefined
47
+ }
48
+ try {
49
+ const initial = await getAccount()
50
+ if (!current() || signal.aborted) return
51
+ const identity = identityOf(initial)
52
+ if (identity?.fingerprint !== publishedIdentity) clear()
53
+ if (identity === undefined) return
54
+
55
+ const response = await read(STATS_PATH, signal, identity.user)
56
+ if (!current() || signal.aborted) return
57
+ if ([401, 403, 409].includes(response?.status)) {
58
+ clear()
59
+ return
60
+ }
61
+ const stats = response?.ok ? await response.json().catch(() => undefined) : undefined
62
+ const latest = await getAccount()
63
+ if (!current() || signal.aborted) return
64
+ if (identityOf(latest)?.fingerprint !== identity.fingerprint) {
65
+ clear()
66
+ return
67
+ }
68
+
69
+ const snapshot = usageSnapshot(latest.balance, stats?.code === 0 ? stats.data : undefined)
70
+ publishedIdentity = identity.fingerprint
71
+ update(snapshot ?? null)
72
+ delay = REFRESH_MS
73
+ } catch {
74
+ if (current()) clear()
75
+ } finally {
76
+ if (current()) timer = later(() => void refresh(lifetime), delay)
77
+ }
78
+ }
79
+
80
+ return {
81
+ setEnabled(enabled) {
82
+ if (enabled && active === undefined) {
83
+ active = new AbortController()
84
+ void refresh(active)
85
+ } else if (!enabled && active !== undefined) {
86
+ active.abort()
87
+ active = undefined
88
+ cancel(timer)
89
+ clear()
90
+ }
91
+ },
92
+ }
93
+ }