Skip to content

Commit

Permalink
improve hotkey filter performance
Browse files Browse the repository at this point in the history
  • Loading branch information
BenjaminBrandmeier authored and Eugeny committed Jan 8, 2023
1 parent 9fd2377 commit c57a7e7
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions tabby-settings/src/components/hotkeySettingsTab.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
HotkeysService,
HostAppService,
} from 'tabby-core'
import deepEqual from 'deep-equal'

_('Search hotkeys')

Expand All @@ -20,6 +19,7 @@ _('Search hotkeys')
export class HotkeySettingsTabComponent {
hotkeyFilter = ''
hotkeyDescriptions: HotkeyDescription[]
allDuplicateHotkeys = this.getAllDuplicateHotkeys()

constructor (
public config: ConfigService,
Expand Down Expand Up @@ -53,23 +53,31 @@ export class HotkeySettingsTabComponent {
: hotkey.strokes,
)
this.config.save()
this.allDuplicateHotkeys = this.getAllDuplicateHotkeys()
}

hotkeyFilterFn (hotkey: HotkeyDescription, query: string): boolean {
const s = hotkey.name + hotkey.id + this.getHotkeys(hotkey.id).map(h => h.strokes).toString()
return s.toLowerCase().includes(query.toLowerCase())
}

private detectDuplicates (strokes: string[] | string): Hotkey {
private getAllDuplicateHotkeys (): string[] {
const allHotkeys = Object
.values(this.config.store.hotkeys)
.filter((value: unknown) => Array.isArray(value))
.flat()
.map((hotkey: string | string[]) => this.toHotkeyIdentifier(hotkey))

const isDuplicate = allHotkeys
.filter(hotkey => deepEqual(hotkey, strokes))
.length > 1
return allHotkeys.filter(hotkey => allHotkeys.indexOf(hotkey) !== allHotkeys.lastIndexOf(hotkey))
}

private detectDuplicates (strokes: string[] | string): Hotkey {
const hotkeyIdentifier = this.toHotkeyIdentifier(strokes)
const isDuplicate = this.allDuplicateHotkeys.includes(hotkeyIdentifier)
return { strokes, isDuplicate }
}

private toHotkeyIdentifier (hotkey: string[] | string): string {
return Array.isArray(hotkey) ? hotkey.join('$#!') : hotkey
}
}

0 comments on commit c57a7e7

Please sign in to comment.