Simplify perf toggle by deduplicating branching logic
Early-return on invalid input, then unify the GET/POST and display paths so each concern is handled once instead of per-subcommand. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
c3ad78f1be
commit
c42c73fe70
|
|
@ -7,23 +7,18 @@ interface PerfState {
|
||||||
|
|
||||||
export async function perfToggle(onOff?: string) {
|
export async function perfToggle(onOff?: string) {
|
||||||
try {
|
try {
|
||||||
if (onOff === undefined) {
|
if (onOff && !['on', 'off', 'status'].includes(onOff)) {
|
||||||
// Toggle
|
|
||||||
const res = await post<PerfState>('/api/system/perf')
|
|
||||||
if (res) console.log(`Perf timing ${res.perfTiming ? color.green('on') : color.red('off')}`)
|
|
||||||
} else if (onOff === 'on') {
|
|
||||||
const res = await post<PerfState>('/api/system/perf', { on: true })
|
|
||||||
if (res) console.log(`Perf timing ${color.green('on')}`)
|
|
||||||
} else if (onOff === 'off') {
|
|
||||||
const res = await post<PerfState>('/api/system/perf', { on: false })
|
|
||||||
if (res) console.log(`Perf timing ${color.red('off')}`)
|
|
||||||
} else if (onOff === 'status') {
|
|
||||||
const res = await get<PerfState>('/api/system/perf')
|
|
||||||
if (res) console.log(`Perf timing is ${res.perfTiming ? color.green('on') : color.red('off')}`)
|
|
||||||
} else {
|
|
||||||
console.error('Usage: toes perf [on|off|status]')
|
console.error('Usage: toes perf [on|off|status]')
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
}
|
}
|
||||||
|
const body = onOff === 'on' ? { on: true } : onOff === 'off' ? { on: false } : undefined
|
||||||
|
const res = onOff === 'status'
|
||||||
|
? await get<PerfState>('/api/system/perf')
|
||||||
|
: await post<PerfState>('/api/system/perf', body ?? {})
|
||||||
|
if (res) {
|
||||||
|
const label = res.perfTiming ? color.green('on') : color.red('off')
|
||||||
|
console.log(`Perf timing ${onOff === 'status' ? 'is ' : ''}${label}`)
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
handleError(error)
|
handleError(error)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,15 +16,15 @@ import {
|
||||||
infoApp,
|
infoApp,
|
||||||
listApps,
|
listApps,
|
||||||
logApp,
|
logApp,
|
||||||
|
metricsApp,
|
||||||
newApp,
|
newApp,
|
||||||
openApp,
|
openApp,
|
||||||
|
perfToggle,
|
||||||
renameApp,
|
renameApp,
|
||||||
restartApp,
|
restartApp,
|
||||||
rmApp,
|
rmApp,
|
||||||
shareApp,
|
shareApp,
|
||||||
startApp,
|
startApp,
|
||||||
metricsApp,
|
|
||||||
perfToggle,
|
|
||||||
stopApp,
|
stopApp,
|
||||||
unshareApp,
|
unshareApp,
|
||||||
} from './commands'
|
} from './commands'
|
||||||
|
|
|
||||||
|
|
@ -287,7 +287,7 @@ onHostLog(pushHostLog)
|
||||||
// Perf timing toggle
|
// Perf timing toggle
|
||||||
router.get('/perf', c => c.json({ perfTiming }))
|
router.get('/perf', c => c.json({ perfTiming }))
|
||||||
router.post('/perf', async c => {
|
router.post('/perf', async c => {
|
||||||
const body = await c.req.json<{ on?: boolean }>()
|
const body = await c.req.json<{ on?: boolean }>().catch(() => ({}))
|
||||||
const on = body.on ?? !perfTiming
|
const on = body.on ?? !perfTiming
|
||||||
setPerfTiming(on)
|
setPerfTiming(on)
|
||||||
console.log(`[perf] timing ${on ? 'enabled' : 'disabled'}`)
|
console.log(`[perf] timing ${on ? 'enabled' : 'disabled'}`)
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,6 @@ import { getAppBySubdomain } from '$apps'
|
||||||
export type { WsData }
|
export type { WsData }
|
||||||
|
|
||||||
export let perfTiming = false
|
export let perfTiming = false
|
||||||
export const setPerfTiming = (on: boolean) => { perfTiming = on }
|
|
||||||
|
|
||||||
const pendingMessages = new Map<ServerWebSocket<WsData>, (string | ArrayBuffer | Uint8Array)[]>()
|
const pendingMessages = new Map<ServerWebSocket<WsData>, (string | ArrayBuffer | Uint8Array)[]>()
|
||||||
const upstreams = new Map<ServerWebSocket<WsData>, WebSocket>()
|
const upstreams = new Map<ServerWebSocket<WsData>, WebSocket>()
|
||||||
|
|
@ -15,6 +14,8 @@ interface WsData {
|
||||||
protocols: string[]
|
protocols: string[]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const setPerfTiming = (on: boolean) => { perfTiming = on }
|
||||||
|
|
||||||
export function extractSubdomain(host: string): string | null {
|
export function extractSubdomain(host: string): string | null {
|
||||||
// Strip port
|
// Strip port
|
||||||
const hostname = host.replace(/:\d+$/, '')
|
const hostname = host.replace(/:\d+$/, '')
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user