MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / useInputBuffer

Function useInputBuffer

source code/hooks/useInputBuffer.ts:29–134  ·  view source on GitHub ↗
({
  maxBufferSize,
  debounceMs,
}: UseInputBufferProps)

Source from the content-addressed store, hash-verified

27}
28
29export function useInputBuffer({
30 maxBufferSize,
31 debounceMs,
32}: UseInputBufferProps): UseInputBufferResult {
33 const [buffer, setBuffer] = useState<BufferEntry[]>([])
34 const [currentIndex, setCurrentIndex] = useState(-1)
35 const lastPushTime = useRef<number>(0)
36 const pendingPush = useRef<ReturnType<typeof setTimeout> | null>(null)
37
38 const pushToBuffer = useCallback(
39 (
40 text: string,
41 cursorOffset: number,
42 pastedContents: Record<number, PastedContent> = {},
43 ) => {
44 const now = Date.now()
45
46 // Clear any pending push
47 if (pendingPush.current) {
48 clearTimeout(pendingPush.current)
49 pendingPush.current = null
50 }
51
52 // Debounce rapid changes
53 if (now - lastPushTime.current < debounceMs) {
54 pendingPush.current = setTimeout(
55 pushToBuffer,
56 debounceMs,
57 text,
58 cursorOffset,
59 pastedContents,
60 )
61 return
62 }
63
64 lastPushTime.current = now
65
66 setBuffer(prevBuffer => {
67 // If we're not at the end of the buffer, truncate everything after current position
68 const newBuffer =
69 currentIndex >= 0 ? prevBuffer.slice(0, currentIndex + 1) : prevBuffer
70
71 // Don't add if it's the same as the last entry
72 const lastEntry = newBuffer[newBuffer.length - 1]
73 if (lastEntry && lastEntry.text === text) {
74 return newBuffer
75 }
76
77 // Add new entry
78 const updatedBuffer = [
79 ...newBuffer,
80 { text, cursorOffset, pastedContents, timestamp: now },
81 ]
82
83 // Limit buffer size
84 if (updatedBuffer.length > maxBufferSize) {
85 return updatedBuffer.slice(-maxBufferSize)
86 }

Callers 1

PromptInputFunction · 0.85

Calls 1

maxMethod · 0.80

Tested by

no test coverage detected