The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import type { SharePayload } from "expo-sharing"; |
| 2 | import { msg } from "gt-react-native"; |
| 3 | |
| 4 | import { m } from "../../i18n/messages"; |
| 5 | |
| 6 | import { SerializedAsyncQueue } from "../../lib/serialized-async-queue"; |
| 7 | import { |
| 8 | hasIncomingShareContent, |
| 9 | type IncomingShareDestination, |
| 10 | type IncomingShareDraft, |
| 11 | } from "./incoming-share-model"; |
| 12 | |
| 13 | export interface IncomingShareInboxDependencies { |
| 14 | readonly loadDrafts: () => Promise<ReadonlyArray<IncomingShareDraft>>; |
| 15 | readonly writeDraft: (draft: IncomingShareDraft) => Promise<void>; |
| 16 | readonly removeDraft: (shareId: string) => Promise<void>; |
| 17 | readonly getPayloads: () => ReadonlyArray<SharePayload>; |
| 18 | readonly clearPayloads: () => void; |
| 19 | readonly buildDraft: (input: { |
| 20 | readonly payloads: ReadonlyArray<SharePayload>; |
| 21 | readonly id: string; |
| 22 | readonly createdAt: string; |
| 23 | }) => Promise<{ |
| 24 | readonly draft: IncomingShareDraft; |
| 25 | readonly cleanup: () => Promise<void>; |
| 26 | readonly rollback?: () => Promise<void>; |
| 27 | }>; |
| 28 | readonly cleanupReplayedPayloads?: (payloads: ReadonlyArray<SharePayload>) => Promise<void>; |
| 29 | readonly idForPayloads: (payloads: ReadonlyArray<SharePayload>) => Promise<string>; |
| 30 | readonly now: () => string; |
| 31 | readonly onClearError?: (error: unknown) => void; |
| 32 | readonly onCleanupError?: (error: unknown) => void; |
| 33 | } |
| 34 | |
| 35 | function sortAndDedupeIncomingShares( |
| 36 | drafts: ReadonlyArray<IncomingShareDraft>, |
| 37 | ): ReadonlyArray<IncomingShareDraft> { |
| 38 | const ids = new Set<string>(); |
| 39 | return [...drafts] |
| 40 | .sort((left, right) => right.createdAt.localeCompare(left.createdAt)) |
| 41 | .filter((draft) => { |
| 42 | if (ids.has(draft.id)) { |
| 43 | return false; |
| 44 | } |
| 45 | ids.add(draft.id); |
| 46 | return true; |
| 47 | }); |
| 48 | } |
| 49 | |
| 50 | /** Serialization prevents stale loads or refreshes from restoring consumed items. */ |
| 51 | export class IncomingShareInbox { |
| 52 | private readonly operations = new SerializedAsyncQueue(); |
| 53 | |
| 54 | constructor(private readonly dependencies: IncomingShareInboxDependencies) {} |
| 55 | |
| 56 | private runExclusive<T>(operation: () => Promise<T>): Promise<T> { |
| 57 | return this.operations.run(operation); |
| 58 | } |
| 59 | |
| 60 | private clearNativePayloads(): void { |
| 61 | try { |
| 62 | this.dependencies.clearPayloads(); |
| 63 | } catch (error) { |
| 64 | this.dependencies.onClearError?.(error); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | private async cleanup(operation: () => Promise<void>): Promise<void> { |
| 69 | try { |
| 70 | await operation(); |
| 71 | } catch (error) { |
| 72 | this.dependencies.onCleanupError?.(error); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | refresh(options: { readonly ingestNative: boolean }): Promise<ReadonlyArray<IncomingShareDraft>> { |
| 77 | return this.runExclusive(async () => { |
| 78 | const loaded = await this.dependencies.loadDrafts(); |
| 79 | const persisted = sortAndDedupeIncomingShares(loaded); |
| 80 | if (!options.ingestNative) { |
| 81 | return persisted; |
| 82 | } |
| 83 | |
| 84 | const payloads = this.dependencies.getPayloads(); |
| 85 | if (payloads.length === 0) { |
| 86 | return persisted; |
| 87 | } |
| 88 | |
| 89 | // Payload-derived ids reuse the inbox item after a crash between durable write and native acknowledgement. |
| 90 | const shareId = await this.dependencies.idForPayloads(payloads); |
| 91 | if (loaded.some((draft) => draft.id === shareId)) { |
| 92 | if (this.dependencies.cleanupReplayedPayloads) { |
| 93 | await this.cleanup(() => this.dependencies.cleanupReplayedPayloads!(payloads)); |
| 94 | } |
| 95 | this.clearNativePayloads(); |
| 96 | return persisted; |
| 97 | } |
| 98 | |
| 99 | const built = await this.dependencies.buildDraft({ |
| 100 | payloads, |
| 101 | id: shareId, |
| 102 | createdAt: this.dependencies.now(), |
| 103 | }); |
| 104 | const { draft } = built; |
| 105 | if (!hasIncomingShareContent(draft)) { |
| 106 | await this.cleanup(built.cleanup); |
| 107 | this.clearNativePayloads(); |
| 108 | throw new Error( |
| 109 | draft.warnings[0] ?? m(msg("The shared content is not supported by the composer.")), |
| 110 | ); |
| 111 | } |
| 112 | |
| 113 | // Persist before clearing the native handoff so termination always leaves a recoverable copy. |
| 114 | try { |
| 115 | await this.dependencies.writeDraft(draft); |
| 116 | } catch (error) { |
| 117 | if (built.rollback) { |
| 118 | await this.cleanup(built.rollback); |
| 119 | } |
| 120 | throw error; |
| 121 | } |
| 122 | await this.cleanup(built.cleanup); |
| 123 | this.clearNativePayloads(); |
| 124 | return sortAndDedupeIncomingShares([draft, ...persisted]); |
| 125 | }); |
| 126 | } |
| 127 | |
| 128 | consume(shareId: string): Promise<ReadonlyArray<IncomingShareDraft>> { |
| 129 | return this.runExclusive(async () => { |
| 130 | // Identical content can be shared intentionally; only stable payload-derived ids identify retries. |
| 131 | await this.dependencies.removeDraft(shareId); |
| 132 | return sortAndDedupeIncomingShares(await this.dependencies.loadDrafts()); |
| 133 | }); |
| 134 | } |
| 135 | |
| 136 | reserve( |
| 137 | shareId: string, |
| 138 | destination: IncomingShareDestination, |
| 139 | ): Promise<ReadonlyArray<IncomingShareDraft>> { |
| 140 | return this.runExclusive(async () => { |
| 141 | const persisted = await this.dependencies.loadDrafts(); |
| 142 | const target = persisted.find((draft) => draft.id === shareId); |
| 143 | if (!target) { |
| 144 | throw new Error(m(msg("The shared content is no longer available."))); |
| 145 | } |
| 146 | if (target.destination) { |
| 147 | if ( |
| 148 | target.destination.environmentId !== destination.environmentId || |
| 149 | target.destination.projectId !== destination.projectId |
| 150 | ) { |
| 151 | throw new Error( |
| 152 | m(msg("The shared content is already reserved for another project draft.")), |
| 153 | ); |
| 154 | } |
| 155 | return sortAndDedupeIncomingShares(persisted); |
| 156 | } |
| 157 | |
| 158 | const reserved = { ...target, destination }; |
| 159 | await this.dependencies.writeDraft(reserved); |
| 160 | return sortAndDedupeIncomingShares( |
| 161 | persisted.map((draft) => (draft.id === shareId ? reserved : draft)), |
| 162 | ); |
| 163 | }); |
| 164 | } |
| 165 | |
| 166 | releaseReservation( |
| 167 | shareId: string, |
| 168 | expectedDestination: IncomingShareDestination, |
| 169 | ): Promise<ReadonlyArray<IncomingShareDraft>> { |
| 170 | return this.runExclusive(async () => { |
| 171 | const persisted = await this.dependencies.loadDrafts(); |
| 172 | const target = persisted.find((draft) => draft.id === shareId); |
| 173 | if (!target) { |
| 174 | return sortAndDedupeIncomingShares(persisted); |
| 175 | } |
| 176 | if (!target.destination) { |
| 177 | return sortAndDedupeIncomingShares(persisted); |
| 178 | } |
| 179 | if ( |
| 180 | target.destination.environmentId !== expectedDestination.environmentId || |
| 181 | target.destination.projectId !== expectedDestination.projectId |
| 182 | ) { |
| 183 | throw new Error( |
| 184 | m(msg("The shared content reservation changed before it could be released.")), |
| 185 | ); |
| 186 | } |
| 187 | |
| 188 | const { destination: _destination, ...unreserved } = target; |
| 189 | await this.dependencies.writeDraft(unreserved); |
| 190 | return sortAndDedupeIncomingShares( |
| 191 | persisted.map((draft) => (draft.id === shareId ? unreserved : draft)), |
| 192 | ); |
| 193 | }); |
| 194 | } |
| 195 | } |