feat: Enforce exclusive auto-request (one client at a time)

Server is now source of truth for autoRequest. When a client enables it,
all other clients lose it. Broadcast includes autoRequest per client,
frontend syncs from server state on each torch-update.
This commit is contained in:
2026-02-14 23:40:30 -06:00
parent f0d8c84a64
commit 9a636e26a7
5 changed files with 46 additions and 5 deletions

View File

@@ -2,7 +2,7 @@
import { ref, computed, onMounted, onUnmounted, nextTick } from 'vue'
import { useTorchStore } from '../stores/torch'
import { useCanvasStore } from '../stores/canvas'
import { requestTorch, releaseTorch, transferTorch, updateName } from '../services/torch'
import { requestTorch, releaseTorch, transferTorch, updateName, setAutoRequest } from '../services/torch'
const torchStore = useTorchStore()
const canvasStore = useCanvasStore()
@@ -86,7 +86,7 @@ async function handleTransfer(targetId: string) {
}
function toggleAutoRequest() {
torchStore.setAutoRequest(!torchStore.autoRequest)
setAutoRequest(!torchStore.autoRequest)
}
onMounted(() => {

View File

@@ -94,6 +94,12 @@ async function handleMessage(data: any) {
await connectToMCP()
}
// Sync autoRequest from server (server is source of truth)
const myClient = data.clients.find((c: any) => c.id === clientId)
if (myClient && myClient.autoRequest !== torchStore.autoRequest) {
torchStore.setAutoRequest(myClient.autoRequest)
}
// Auto-request: if no one holds the torch and we have autoRequest enabled
if (!hasTorchNow && data.holderId === null && torchStore.autoRequest) {
console.log('[Torch] Auto-requesting torch (no holder)')
@@ -163,6 +169,15 @@ export async function transferTorch(targetId: string): Promise<boolean> {
return true
}
/**
* Toggle auto-request on server (exclusive — only one client at a time)
*/
export function setAutoRequest(value: boolean): void {
if (torchWs?.readyState === WebSocket.OPEN) {
torchWs.send(JSON.stringify({ type: 'set-auto-request', value }))
}
}
/**
* Update client name on server
*/

View File

@@ -4,6 +4,7 @@ import { ref, computed } from 'vue'
export interface TorchClient {
id: string
name: string
autoRequest: boolean
userAgent: string
hostname: string
url: string