Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/core/src/input/ChordMatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface Chord {

export interface ChordMatcherOptions {
timeoutMs?: number;
maxBufferSize?: number;
}

function getKeyEventToken(event: KeyEvent): string {
Expand All @@ -27,10 +28,12 @@ export class ChordMatcher {
private _nextId = 0;
private _buffer: string[] = [];
private _timeoutMs: number;
private _maxBufferSize: number;
private _timer: ReturnType<typeof setTimeout> | null = null;

constructor(opts?: ChordMatcherOptions) {
this._timeoutMs = opts?.timeoutMs ?? 800;
this._maxBufferSize = opts?.maxBufferSize ?? 10;
}

bind(keys: string[], handler: () => void): () => void {
Expand All @@ -49,6 +52,12 @@ export class ChordMatcher {

const token = getKeyEventToken(event);
let candidate = [...this._buffer, token];

if (candidate.length > this._maxBufferSize) {
this._buffer = [];
candidate = [token];
}

let matchingBindings = this._getMatchingBindings(candidate);

if (matchingBindings.length === 0) {
Expand Down
Loading