mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-05-04 02:21:47 +00:00
fix(tui): isolate turn state from app render
This commit is contained in:
parent
6a3873942f
commit
f6846205cc
3 changed files with 61 additions and 8 deletions
46
ui-tui/src/__tests__/stateIsolation.test.ts
Normal file
46
ui-tui/src/__tests__/stateIsolation.test.ts
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
import { beforeEach, describe, expect, it } from 'vitest'
|
||||
|
||||
import { patchTurnState, resetTurnState } from '../app/turnStore.js'
|
||||
import { $uiState, resetUiState } from '../app/uiStore.js'
|
||||
|
||||
const shallowEqual = <T extends Record<string, unknown>>(a: T, b: T) =>
|
||||
Object.keys(a).length === Object.keys(b).length && Object.keys(a).every(key => Object.is(a[key], b[key]))
|
||||
|
||||
const subscribeSelected = <T extends Record<string, unknown>>(selector: () => T) => {
|
||||
let current = selector()
|
||||
let calls = 0
|
||||
|
||||
const unsubscribe = $uiState.listen(() => {
|
||||
const next = selector()
|
||||
|
||||
if (shallowEqual(next, current)) {
|
||||
return
|
||||
}
|
||||
|
||||
current = next
|
||||
calls++
|
||||
})
|
||||
|
||||
return { calls: () => calls, unsubscribe }
|
||||
}
|
||||
|
||||
describe('TUI state isolation', () => {
|
||||
beforeEach(() => {
|
||||
resetUiState()
|
||||
resetTurnState()
|
||||
})
|
||||
|
||||
it('does not notify ui/composer subscribers for high-frequency turn updates', () => {
|
||||
const composerRelevant = subscribeSelected(() => ({ busy: $uiState.get().busy, sid: $uiState.get().sid }))
|
||||
|
||||
try {
|
||||
for (let i = 0; i < 50; i++) {
|
||||
patchTurnState({ streaming: `token ${i}` })
|
||||
}
|
||||
} finally {
|
||||
composerRelevant.unsubscribe()
|
||||
}
|
||||
|
||||
expect(composerRelevant.calls()).toBe(0)
|
||||
})
|
||||
})
|
||||
Loading…
Add table
Add a link
Reference in a new issue