* Attempt to claim a chat for a window. * Returns { ok: true } if claimed, or { ok: false, ownerStableId } if already owned by another window.
(chatId: string, electronId: number)
| 142 | * Returns { ok: true } if claimed, or { ok: false, ownerStableId } if already owned by another window. |
| 143 | */ |
| 144 | claimChat(chatId: string, electronId: number): { ok: true } | { ok: false; ownerStableId: string } { |
| 145 | const existingOwner = this.chatOwnership.get(chatId) |
| 146 | |
| 147 | // Already owned by this same window — idempotent success |
| 148 | if (existingOwner === electronId) { |
| 149 | return { ok: true } |
| 150 | } |
| 151 | |
| 152 | // Owned by another window — check it still exists |
| 153 | if (existingOwner !== undefined) { |
| 154 | const ownerWindow = this.windows.get(existingOwner) |
| 155 | if (ownerWindow && !ownerWindow.isDestroyed()) { |
| 156 | const ownerStableId = this.windowIdMap.get(existingOwner) ?? "unknown" |
| 157 | return { ok: false, ownerStableId } |
| 158 | } |
| 159 | // Owner window is gone — stale entry, clean it up |
| 160 | this.chatOwnership.delete(chatId) |
| 161 | } |
| 162 | |
| 163 | this.chatOwnership.set(chatId, electronId) |
| 164 | return { ok: true } |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Release a chat owned by a specific window. |
no test coverage detected