retry-listing.ts (2963B)
1 import type { RelayPolicy } from '../config/relays.ts'; 2 import type { PublicPool } from '../nostr/public-pool.ts'; 3 import { 4 listingPublicationOwnership, 5 type ListingPublication 6 } from './sign-listing.ts'; 7 import { 8 listingResumeOwnership, 9 type ListingResumption 10 } from './resume-listing.ts'; 11 import { 12 deliverControlledListing, 13 type ListingDeliveryResult 14 } from './deliver-listing.ts'; 15 16 declare const retryBrand: unique symbol; 17 export type ListingRetry = Readonly<{ [retryBrand]: true }>; 18 export type ListingRetrySource = ListingPublication | ListingResumption; 19 type Control = Readonly<{ 20 source: ListingRetrySource; 21 pool: PublicPool; 22 policy: RelayPolicy; 23 consent: 'retry_stored_listing' | 'resume_stored_listing'; 24 controller: AbortController; 25 result: Promise<ListingDeliveryResult>; 26 }>; 27 const controls = new WeakMap<ListingRetry, Control>(); 28 const pending = new WeakMap<ListingRetrySource, ListingRetry>(); 29 // Internal delivery custody is granted only by the original control object. 30 // A copied disclosure cannot provide a signal or cancel somebody else's job. 31 export function listingRetryOwnership(token: ListingRetry) { 32 const saved = controls.get(token); 33 return ( 34 saved && { 35 source: saved.source, 36 pool: saved.pool, 37 policy: saved.policy, 38 consent: saved.consent, 39 signal: saved.controller.signal, 40 current: () => !saved.controller.signal.aborted 41 } 42 ); 43 } 44 export function beginListingRetry( 45 source: ListingRetrySource, 46 pool: PublicPool, 47 policy: RelayPolicy, 48 consent: unknown 49 ): ListingRetry | undefined { 50 const publication = listingPublicationOwnership(source as ListingPublication), 51 resume = listingResumeOwnership(source as ListingResumption), 52 original = publication ?? resume; 53 if ( 54 !original || 55 original.policy !== policy || 56 consent !== 57 (publication ? 'retry_stored_listing' : 'resume_stored_listing') || 58 typeof window === 'undefined' 59 ) 60 return; 61 const active = pending.get(source); 62 if (active) return active; 63 const token = Object.freeze({}) as ListingRetry, 64 controller = new AbortController(); 65 const result = Promise.resolve() 66 .then(() => deliverControlledListing(token)) 67 .then((value): ListingDeliveryResult => ({ 68 status: 69 value.status === 'storage_failed' || value.status === 'unavailable' 70 ? 'needs_action' 71 : value.status 72 })) 73 .finally(() => { 74 if (pending.get(source) === token) pending.delete(source); 75 }); 76 controls.set(token, { 77 source, 78 pool, 79 policy, 80 consent: consent as Control['consent'], 81 controller, 82 result 83 }); 84 pending.set(source, token); 85 return token; 86 } 87 export function listingRetryResult( 88 token: ListingRetry 89 ): Promise<ListingDeliveryResult> { 90 return controls.get(token)?.result ?? Promise.resolve({ status: 'invalid' }); 91 } 92 export function stopListingRetry(token: ListingRetry): void { 93 controls.get(token)?.controller.abort(); 94 }