view-context.ts (6149B)
1 import { 2 createIdentitySession, 3 identitySessionSnapshot, 4 connectIdentity, 5 disconnectIdentity, 6 probeIdentityMessaging, 7 invalidateIdentityOperations, 8 type IdentitySession, 9 type IdentitySnapshot 10 } from './identity-session.ts'; 11 declare const viewBrand: unique symbol; 12 export type IdentityViewContext = Readonly<{ [viewBrand]: true }>; 13 export type IdentityViewSnapshot = Readonly<{ 14 mounted: boolean; 15 identity: IdentitySnapshot; 16 }>; 17 export type IdentityViewActionResult = IdentityViewSnapshot & 18 Readonly<{ current(): boolean }>; 19 export const IDENTITY_VIEW_CONTEXT = 'harvestcircle_identity_view'; 20 type Listener = (state: IdentityViewSnapshot) => void; 21 type Controller = { 22 session: IdentitySession; 23 snapshot(): IdentityViewSnapshot; 24 mount(): boolean; 25 connect(): Promise<IdentityViewActionResult>; 26 probe(review: unknown): Promise<IdentityViewActionResult>; 27 disconnect(): void; 28 close(): void; 29 subscribe(listener: Listener): () => void; 30 invalidate(): void; 31 }; 32 const contexts = new WeakMap<IdentityViewContext, Controller>(); 33 const unavailable = (): IdentityViewSnapshot => ({ 34 mounted: false, 35 identity: { state: 'guest', reason: 'unavailable' } 36 }); 37 // Per-client context construction is SSR-pure. Observed key/capability never 38 // authorizes private content, installed storage, inbox readiness or publication. 39 export function createIdentityViewContext(): IdentityViewContext { 40 const token = Object.freeze({}) as IdentityViewContext; 41 const session = createIdentitySession(); 42 const listeners = new Map<symbol, Listener>(); 43 let mounted = false, 44 closed = false, 45 busy = false, 46 generation = Symbol(), 47 viewGeneration = Symbol(), 48 actionGeneration = Symbol(); 49 const snapshot = (): IdentityViewSnapshot => 50 closed 51 ? unavailable() 52 : { 53 mounted, 54 identity: busy 55 ? { ...identitySessionSnapshot(session), admission: 'busy' } 56 : identitySessionSnapshot(session) 57 }; 58 function notify() { 59 for (const listener of listeners.values()) { 60 try { 61 listener(snapshot()); 62 } catch { 63 /* A presentation observer cannot interrupt owned SDK settlement. */ 64 } 65 } 66 } 67 async function action(work: () => Promise<IdentitySnapshot>) { 68 if (!mounted || closed) return { ...snapshot(), current: () => false }; 69 busy = false; 70 actionGeneration = Symbol(); 71 const originalAction = actionGeneration, 72 original = generation, 73 originalView = viewGeneration, 74 pending = work(); 75 notify(); 76 const result = await pending; 77 if (!closed && original === generation) { 78 busy = result.admission === 'busy'; 79 notify(); 80 } 81 return { 82 ...snapshot(), 83 current: () => 84 !closed && 85 mounted && 86 actionGeneration === originalAction && 87 generation === original && 88 viewGeneration === originalView 89 }; 90 } 91 contexts.set(token, { 92 session, 93 snapshot, 94 invalidate() { 95 viewGeneration = Symbol(); 96 invalidateIdentityOperations(session); 97 }, 98 mount() { 99 if (closed || typeof window === 'undefined') return false; 100 mounted = true; 101 notify(); 102 return true; 103 }, 104 connect: () => action(() => connectIdentity(session)), 105 probe: (review) => action(() => probeIdentityMessaging(session, review)), 106 disconnect() { 107 if (closed) return; 108 generation = Symbol(); 109 busy = false; 110 disconnectIdentity(session); 111 notify(); 112 }, 113 close() { 114 if (closed) return; 115 generation = Symbol(); 116 busy = false; 117 disconnectIdentity(session); 118 closed = true; 119 mounted = false; 120 notify(); 121 listeners.clear(); 122 }, 123 subscribe(listener) { 124 if (closed) return () => {}; 125 const subscription = Symbol(); 126 listeners.set(subscription, listener); 127 try { 128 listener(snapshot()); 129 } catch { 130 /* Isolate presentation observers. */ 131 } 132 return () => { 133 listeners.delete(subscription); 134 }; 135 } 136 }); 137 return token; 138 } 139 export function identityViewSnapshot( 140 context: IdentityViewContext 141 ): IdentityViewSnapshot { 142 return contexts.get(context)?.snapshot() ?? unavailable(); 143 } 144 export function mountIdentityView(context: IdentityViewContext): boolean { 145 return contexts.get(context)?.mount() ?? false; 146 } 147 export function connectIdentityView( 148 context: IdentityViewContext 149 ): Promise<IdentityViewActionResult> { 150 return ( 151 contexts.get(context)?.connect() ?? 152 Promise.resolve({ ...unavailable(), current: () => false }) 153 ); 154 } 155 export function probeIdentityViewMessaging( 156 context: IdentityViewContext, 157 review: unknown 158 ): Promise<IdentityViewActionResult> { 159 return ( 160 contexts.get(context)?.probe(review) ?? 161 Promise.resolve({ ...unavailable(), current: () => false }) 162 ); 163 } 164 export function disconnectIdentityView(context: IdentityViewContext): void { 165 contexts.get(context)?.disconnect(); 166 } 167 export function closeIdentityView(context: IdentityViewContext): void { 168 contexts.get(context)?.close(); 169 } 170 export function subscribeIdentityView( 171 context: IdentityViewContext, 172 listener: Listener 173 ): () => void { 174 return contexts.get(context)?.subscribe(listener) ?? (() => {}); 175 } 176 export function invalidateIdentityView(context: IdentityViewContext): void { 177 const owned = contexts.get(context); 178 owned?.invalidate(); 179 } 180 // Trusted client orchestration receives only the original opaque session. This 181 // is not effect permission; each workflow still rechecks its genuine owner. 182 export function identityViewSession( 183 context: IdentityViewContext 184 ): IdentitySession | undefined { 185 const owner = contexts.get(context); 186 return owner?.snapshot().mounted ? owner.session : undefined; 187 } 188 // Mechanical observed-owner comparison only, never private authorization. 189 export function identityViewMatchesOwner( 190 context: IdentityViewContext, 191 owner: unknown 192 ): boolean { 193 const state = contexts.get(context)?.snapshot(); 194 return ( 195 state?.mounted === true && 196 state.identity.state !== 'guest' && 197 state.identity.state !== 'pending' && 198 state.identity.publicKey === owner 199 ); 200 }