a demonstration replicated social networking web app built with anproto
wiredove.net/
social
ed25519
protocols
1const listeners = new Set()
2const pending = new Map()
3let nextId = 1
4let lastResult = null
5
6const snapshot = () => ({
7 pendingCount: pending.size,
8 lastResult
9})
10
11const notify = () => {
12 const state = snapshot()
13 listeners.forEach((listener) => {
14 try {
15 listener(state)
16 } catch (err) {
17 console.warn('publish status listener failed', err)
18 }
19 })
20}
21
22export const getPublishStatusSnapshot = () => snapshot()
23
24export const beginPublishVerification = (meta = {}) => {
25 const id = `pub-${Date.now()}-${nextId++}`
26 pending.set(id, {
27 ...meta,
28 startedAt: Date.now()
29 })
30 notify()
31 return id
32}
33
34export const finishPublishVerification = (id, result = {}) => {
35 if (id && pending.has(id)) {
36 pending.delete(id)
37 }
38 lastResult = {
39 ok: Boolean(result.ok),
40 missing: Array.isArray(result.missing) ? result.missing : [],
41 attempts: Number.isFinite(result.attempts) ? result.attempts : 0,
42 reason: result.reason || '',
43 at: Date.now()
44 }
45 notify()
46}
47
48export const subscribePublishStatus = (listener) => {
49 listeners.add(listener)
50 listener(snapshot())
51 return () => {
52 listeners.delete(listener)
53 }
54}