( options: HotkeyRecorderOptions | (() => HotkeyRecorderOptions), )
| 59 | * ``` |
| 60 | */ |
| 61 | export function createHotkeyRecorder( |
| 62 | options: HotkeyRecorderOptions | (() => HotkeyRecorderOptions), |
| 63 | ): SolidHotkeyRecorder { |
| 64 | const defaultOptions = useDefaultHotkeysOptions() |
| 65 | |
| 66 | const resolvedOptions = typeof options === 'function' ? options() : options |
| 67 | const mergedOptions = { |
| 68 | ...defaultOptions.hotkeyRecorder, |
| 69 | ...resolvedOptions, |
| 70 | } as HotkeyRecorderOptions |
| 71 | |
| 72 | // Create recorder once synchronously (matches React's useRef pattern) |
| 73 | const recorder = new HotkeyRecorder(mergedOptions) |
| 74 | |
| 75 | // Subscribe to recorder state using useSelector (same pattern as useHotkeyRecorder) |
| 76 | const isRecording = useSelector(recorder.store, (state) => state.isRecording) |
| 77 | const recordedHotkey = useSelector( |
| 78 | recorder.store, |
| 79 | (state) => state.recordedHotkey, |
| 80 | ) |
| 81 | |
| 82 | // Sync options on every effect run (matches React's sync on render) |
| 83 | createEffect(() => { |
| 84 | const resolved = typeof options === 'function' ? options() : options |
| 85 | recorder.setOptions({ |
| 86 | ...defaultOptions.hotkeyRecorder, |
| 87 | ...resolved, |
| 88 | } as HotkeyRecorderOptions) |
| 89 | }) |
| 90 | |
| 91 | // Cleanup on unmount |
| 92 | onCleanup(() => { |
| 93 | recorder.destroy() |
| 94 | }) |
| 95 | |
| 96 | return { |
| 97 | isRecording, |
| 98 | recordedHotkey, |
| 99 | startRecording: () => recorder.start(), |
| 100 | stopRecording: () => recorder.stop(), |
| 101 | cancelRecording: () => recorder.cancel(), |
| 102 | } |
| 103 | } |
no test coverage detected
searching dependent graphs…