(
hotkeys:
| Array<CreateHotkeyDefinition>
| (() => Array<CreateHotkeyDefinition>),
commonOptions: CreateHotkeyOptions | (() => CreateHotkeyOptions) = {},
)
| 64 | * ``` |
| 65 | */ |
| 66 | export function createHotkeys( |
| 67 | hotkeys: |
| 68 | | Array<CreateHotkeyDefinition> |
| 69 | | (() => Array<CreateHotkeyDefinition>), |
| 70 | commonOptions: CreateHotkeyOptions | (() => CreateHotkeyOptions) = {}, |
| 71 | ): void { |
| 72 | type RegistrationRecord = { |
| 73 | handle: HotkeyRegistrationHandle |
| 74 | target: Document | HTMLElement | Window |
| 75 | } |
| 76 | |
| 77 | const defaultOptions = useDefaultHotkeysOptions() |
| 78 | const manager = getHotkeyManager() |
| 79 | |
| 80 | const registrations = new Map<string, RegistrationRecord>() |
| 81 | |
| 82 | // Clean up only when this component/scope disposes — not before every effect |
| 83 | // re-run. An inner `onCleanup` inside `createEffect` runs whenever reactive |
| 84 | // deps change (e.g. dynamic hotkey list), which unregistered every hotkey and |
| 85 | // re-registered them, flooding the manager store and could freeze the tab. |
| 86 | onCleanup(() => { |
| 87 | for (const { handle } of registrations.values()) { |
| 88 | if (handle.isActive) { |
| 89 | handle.unregister() |
| 90 | } |
| 91 | } |
| 92 | registrations.clear() |
| 93 | }) |
| 94 | |
| 95 | createEffect(() => { |
| 96 | const resolvedHotkeys = typeof hotkeys === 'function' ? hotkeys() : hotkeys |
| 97 | const resolvedCommonOptions = |
| 98 | typeof commonOptions === 'function' ? commonOptions() : commonOptions |
| 99 | |
| 100 | const nextKeys = new Set<string>() |
| 101 | const prepared: Array<{ |
| 102 | registrationKey: string |
| 103 | def: CreateHotkeyDefinition |
| 104 | mergedOptions: CreateHotkeyOptions |
| 105 | hotkeyString: Hotkey |
| 106 | resolvedTarget: Document | HTMLElement | Window |
| 107 | }> = [] |
| 108 | |
| 109 | for (let i = 0; i < resolvedHotkeys.length; i++) { |
| 110 | const def = resolvedHotkeys[i]! |
| 111 | const resolvedDefOptions = def.options ?? {} |
| 112 | |
| 113 | const mergedOptions = { |
| 114 | ...defaultOptions.hotkey, |
| 115 | ...resolvedCommonOptions, |
| 116 | ...resolvedDefOptions, |
| 117 | } as CreateHotkeyOptions |
| 118 | |
| 119 | const platform = mergedOptions.platform ?? detectPlatform() |
| 120 | const hotkeyString = normalizeRegisterableHotkey(def.hotkey, platform) |
| 121 | |
| 122 | const resolvedTarget = |
| 123 | 'target' in mergedOptions |
searching dependent graphs…