diff --git a/.changeset/wild-snails-retire.md b/.changeset/wild-snails-retire.md new file mode 100644 index 0000000..ce75b6e --- /dev/null +++ b/.changeset/wild-snails-retire.md @@ -0,0 +1,5 @@ +--- +'@tanstack/hotkeys': patch +--- + +Remove redundant `setState` in `destroy()` and share a single `IDLE_STATE` constant across `stop()` and `cancel()` diff --git a/docs/reference/classes/HotkeyRecorder.md b/docs/reference/classes/HotkeyRecorder.md index d3c8bab..8d9237d 100644 --- a/docs/reference/classes/HotkeyRecorder.md +++ b/docs/reference/classes/HotkeyRecorder.md @@ -5,7 +5,7 @@ title: HotkeyRecorder # Class: HotkeyRecorder -Defined in: [recorder.ts:72](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L72) +Defined in: [recorder.ts:80](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L80) Framework-agnostic class for recording keyboard shortcuts. @@ -54,7 +54,7 @@ unsubscribe() new HotkeyRecorder(options): HotkeyRecorder; ``` -Defined in: [recorder.ts:86](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L86) +Defined in: [recorder.ts:93](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L93) #### Parameters @@ -74,7 +74,7 @@ Defined in: [recorder.ts:86](https://github.com/TanStack/hotkeys/blob/main/packa readonly store: Store; ``` -Defined in: [recorder.ts:77](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L77) +Defined in: [recorder.ts:85](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L85) The TanStack Store instance containing the recorder state. Use this to subscribe to state changes or access current state. @@ -87,7 +87,7 @@ Use this to subscribe to state changes or access current state. cancel(): void; ``` -Defined in: [recorder.ts:214](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L214) +Defined in: [recorder.ts:218](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L218) Cancel recording without saving. @@ -106,7 +106,7 @@ the onCancel callback if provided. destroy(): void; ``` -Defined in: [recorder.ts:259](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L259) +Defined in: [recorder.ts:260](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L260) Clean up event listeners and reset state. @@ -125,7 +125,7 @@ all event listeners are properly removed. setOptions(options): void; ``` -Defined in: [recorder.ts:95](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L95) +Defined in: [recorder.ts:102](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L102) Updates the recorder options, including callbacks. This allows framework adapters to sync callback changes without recreating the recorder. @@ -148,7 +148,7 @@ This allows framework adapters to sync callback changes without recreating the r start(): void; ``` -Defined in: [recorder.ts:109](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L109) +Defined in: [recorder.ts:116](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L116) Start recording a new hotkey. @@ -168,7 +168,7 @@ a valid hotkey is recorded, Escape is pressed, or stop/cancel is called. stop(): void; ``` -Defined in: [recorder.ts:194](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L194) +Defined in: [recorder.ts:201](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L201) Stop recording (same as cancel, but doesn't call onCancel). diff --git a/docs/reference/interfaces/HotkeyRecorderOptions.md b/docs/reference/interfaces/HotkeyRecorderOptions.md index 24e67df..fce18ff 100644 --- a/docs/reference/interfaces/HotkeyRecorderOptions.md +++ b/docs/reference/interfaces/HotkeyRecorderOptions.md @@ -5,7 +5,7 @@ title: HotkeyRecorderOptions # Interface: HotkeyRecorderOptions -Defined in: [recorder.ts:24](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L24) +Defined in: [recorder.ts:32](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L32) Options for configuring a HotkeyRecorder instance. @@ -17,7 +17,7 @@ Options for configuring a HotkeyRecorder instance. optional onCancel: () => void; ``` -Defined in: [recorder.ts:28](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L28) +Defined in: [recorder.ts:36](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L36) Optional callback when recording is cancelled (Escape pressed) @@ -33,7 +33,7 @@ Optional callback when recording is cancelled (Escape pressed) optional onClear: () => void; ``` -Defined in: [recorder.ts:30](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L30) +Defined in: [recorder.ts:38](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L38) Optional callback when shortcut is cleared (Backspace/Delete pressed) @@ -49,7 +49,7 @@ Optional callback when shortcut is cleared (Backspace/Delete pressed) onRecord: (hotkey) => void; ``` -Defined in: [recorder.ts:26](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L26) +Defined in: [recorder.ts:34](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L34) Callback when a hotkey is successfully recorded diff --git a/packages/hotkeys/src/recorder.ts b/packages/hotkeys/src/recorder.ts index 048ad1c..109796f 100644 --- a/packages/hotkeys/src/recorder.ts +++ b/packages/hotkeys/src/recorder.ts @@ -18,6 +18,14 @@ export interface HotkeyRecorderState { recordedHotkey: Hotkey | null } +/** + * Initial idle state for the recorder, used when not recording. + */ +const IDLE_STATE: HotkeyRecorderState = { + isRecording: false, + recordedHotkey: null, +} + /** * Options for configuring a HotkeyRecorder instance. */ @@ -74,10 +82,9 @@ export class HotkeyRecorder { * The TanStack Store instance containing the recorder state. * Use this to subscribe to state changes or access current state. */ - readonly store: Store = new Store({ - isRecording: false, - recordedHotkey: null, - }) + readonly store: Store = new Store( + IDLE_STATE, + ) #keydownHandler: ((event: KeyboardEvent) => void) | null = null #options: HotkeyRecorderOptions @@ -199,10 +206,7 @@ export class HotkeyRecorder { } // Update store state - this.store.setState(() => ({ - isRecording: false, - recordedHotkey: null, - })) + this.store.setState(() => IDLE_STATE) } /** @@ -219,10 +223,7 @@ export class HotkeyRecorder { } // Update store state - this.store.setState(() => ({ - isRecording: false, - recordedHotkey: null, - })) + this.store.setState(() => IDLE_STATE) // Call cancel callback this.#options.onCancel?.() @@ -258,9 +259,5 @@ export class HotkeyRecorder { */ destroy(): void { this.stop() - this.store.setState(() => ({ - isRecording: false, - recordedHotkey: null, - })) } }