| 15 | export type NormalizedSequenceString = string & {[sequenceBrand]: true} |
| 16 | |
| 17 | export class SequenceTracker { |
| 18 | static readonly CHORD_TIMEOUT = 1500 |
| 19 | |
| 20 | private _path: readonly NormalizedHotkeyString[] = [] |
| 21 | private timer: number | null = null |
| 22 | private onReset |
| 23 | |
| 24 | constructor({onReset}: SequenceTrackerOptions = {}) { |
| 25 | this.onReset = onReset |
| 26 | } |
| 27 | |
| 28 | get path(): readonly NormalizedHotkeyString[] { |
| 29 | return this._path |
| 30 | } |
| 31 | |
| 32 | get sequence(): NormalizedSequenceString { |
| 33 | return this._path.join(SEQUENCE_DELIMITER) as NormalizedSequenceString |
| 34 | } |
| 35 | |
| 36 | registerKeypress(event: KeyboardEvent): void { |
| 37 | this._path = [...this._path, eventToHotkeyString(event)] |
| 38 | this.startTimer() |
| 39 | } |
| 40 | |
| 41 | reset(): void { |
| 42 | this.killTimer() |
| 43 | this._path = [] |
| 44 | this.onReset?.() |
| 45 | } |
| 46 | |
| 47 | private killTimer(): void { |
| 48 | if (this.timer != null) { |
| 49 | window.clearTimeout(this.timer) |
| 50 | } |
| 51 | this.timer = null |
| 52 | } |
| 53 | |
| 54 | private startTimer(): void { |
| 55 | this.killTimer() |
| 56 | this.timer = window.setTimeout(() => this.reset(), SequenceTracker.CHORD_TIMEOUT) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | export function normalizeSequence(sequence: string): NormalizedSequenceString { |
| 61 | return sequence |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…