Skip to content

Commit 6ab63e6

Browse files
committed
chore: append suffix Ref to ref variables and clear timeout on unmount
1 parent 144419c commit 6ab63e6

File tree

1 file changed

+35
-29
lines changed

1 file changed

+35
-29
lines changed

src/Common/Hooks/UseRegisterShortcut/UseRegisterShortcutProvider.tsx

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ const UseRegisterShortcutProvider = ({
2929
shortcutTimeout,
3030
children,
3131
}: UseRegisterShortcutProviderType) => {
32-
const disableShortcuts = useRef<boolean>(false)
33-
const shortcuts = useRef<Record<string, ShortcutType>>({})
34-
const keysDown = useRef<Set<string>>(new Set())
35-
const keyDownTimeout = useRef<ReturnType<typeof setTimeout>>(-1)
32+
const disableShortcutsRef = useRef<boolean>(false)
33+
const shortcutsRef = useRef<Record<string, ShortcutType>>({})
34+
const keysDownRef = useRef<Set<string>>(new Set())
35+
const keyDownTimeoutRef = useRef<ReturnType<typeof setTimeout>>(-1)
3636
const ignoredTags = ignoreTags ?? IGNORE_TAGS_FALLBACK
3737

3838
const registerShortcut: UseRegisterShortcutContextType['registerShortcut'] = useCallback(
@@ -43,79 +43,81 @@ const UseRegisterShortcutProvider = ({
4343
}
4444

4545
const match =
46-
shortcuts.current[id] && deepEquals(shortcuts.current[id].keys, keys) ? shortcuts.current[id] : null
46+
shortcutsRef.current[id] && deepEquals(shortcutsRef.current[id].keys, keys)
47+
? shortcutsRef.current[id]
48+
: null
4749

4850
if (match) {
4951
verifyCallbackStack(match.callbackStack)
5052
match.callbackStack.push(callback)
5153
return
5254
}
5355

54-
shortcuts.current[id] = { keys: processedKeys, callbackStack: [callback], description }
56+
shortcutsRef.current[id] = { keys: processedKeys, callbackStack: [callback], description }
5557
},
5658
[],
5759
)
5860

5961
const unregisterShortcut: UseRegisterShortcutContextType['unregisterShortcut'] = useCallback((keys) => {
6062
const { id } = preprocessKeys(keys)
6163

62-
if (!shortcuts.current[id]) {
64+
if (!shortcutsRef.current[id]) {
6365
return
6466
}
6567

66-
const { callbackStack } = shortcuts.current[id]
68+
const { callbackStack } = shortcutsRef.current[id]
6769
verifyCallbackStack(callbackStack)
6870
callbackStack.pop()
6971

7072
if (!callbackStack.length) {
7173
// NOTE: delete the shortcut only if all registered callbacks are unregistered
7274
// if 2 shortcuts are registered with the same keys then there needs to be 2 unregister calls
73-
delete shortcuts.current[id]
75+
delete shortcutsRef.current[id]
7476
}
7577
}, [])
7678

7779
const setDisableShortcuts: UseRegisterShortcutContextType['setDisableShortcuts'] = useCallback((shouldDisable) => {
78-
disableShortcuts.current = shouldDisable
80+
disableShortcutsRef.current = shouldDisable
7981
}, [])
8082

8183
const triggerShortcut: UseRegisterShortcutContextType['triggerShortcut'] = useCallback((keys) => {
8284
const { id } = preprocessKeys(keys)
8385

84-
if (!shortcuts.current[id]) {
86+
if (!shortcutsRef.current[id]) {
8587
return
8688
}
8789

88-
const { callbackStack } = shortcuts.current[id]
90+
const { callbackStack } = shortcutsRef.current[id]
8991
verifyCallbackStack(callbackStack)
9092

9193
// NOTE: call the last callback in the callback stack
9294
callbackStack[callbackStack.length - 1]()
9395
}, [])
9496

9597
const handleKeyupEvent = useCallback(() => {
96-
if (!keysDown.current.size) {
98+
if (!keysDownRef.current.size) {
9799
return
98100
}
99101

100-
const { id } = preprocessKeys(Array.from(keysDown.current.values()) as ShortcutType['keys'])
102+
const { id } = preprocessKeys(Array.from(keysDownRef.current.values()) as ShortcutType['keys'])
101103

102-
if (shortcuts.current[id]) {
103-
const { callbackStack } = shortcuts.current[id]
104+
if (shortcutsRef.current[id]) {
105+
const { callbackStack } = shortcutsRef.current[id]
104106
verifyCallbackStack(callbackStack)
105107
callbackStack[callbackStack.length - 1]()
106108
}
107109

108-
keysDown.current.clear()
110+
keysDownRef.current.clear()
109111

110-
if (keyDownTimeout.current > -1) {
111-
clearTimeout(keyDownTimeout.current)
112-
keyDownTimeout.current = -1
112+
if (keyDownTimeoutRef.current > -1) {
113+
clearTimeout(keyDownTimeoutRef.current)
114+
keyDownTimeoutRef.current = -1
113115
}
114116
}, [])
115117

116118
const handleKeydownEvent = useCallback((event: KeyboardEvent) => {
117-
if (keyDownTimeout.current === -1) {
118-
keyDownTimeout.current = setTimeout(() => {
119+
if (keyDownTimeoutRef.current === -1) {
120+
keyDownTimeoutRef.current = setTimeout(() => {
119121
handleKeyupEvent()
120122
}, shortcutTimeout ?? DEFAULT_TIMEOUT)
121123
}
@@ -131,26 +133,26 @@ const UseRegisterShortcutProvider = ({
131133
return
132134
}
133135

134-
if (!disableShortcuts.current) {
135-
keysDown.current.add(event.key.toUpperCase())
136+
if (!disableShortcutsRef.current) {
137+
keysDownRef.current.add(event.key.toUpperCase())
136138

137139
if (event.ctrlKey) {
138-
keysDown.current.add('CONTROL')
140+
keysDownRef.current.add('CONTROL')
139141
}
140142
if (event.metaKey) {
141-
keysDown.current.add('META')
143+
keysDownRef.current.add('META')
142144
}
143145
if (event.altKey) {
144-
keysDown.current.add('ALT')
146+
keysDownRef.current.add('ALT')
145147
}
146148
if (event.shiftKey) {
147-
keysDown.current.add('SHIFT')
149+
keysDownRef.current.add('SHIFT')
148150
}
149151
}
150152
}, [])
151153

152154
const handleBlur = useCallback(() => {
153-
keysDown.current.clear()
155+
keysDownRef.current.clear()
154156
}, [])
155157

156158
useEffect(() => {
@@ -162,6 +164,10 @@ const UseRegisterShortcutProvider = ({
162164
window.removeEventListener('keydown', handleKeydownEvent)
163165
window.removeEventListener('keyup', handleKeyupEvent)
164166
window.removeEventListener('blur', handleBlur)
167+
168+
if (keyDownTimeoutRef.current > -1) {
169+
clearTimeout(keyDownTimeoutRef.current)
170+
}
165171
}
166172
}, [handleKeyupEvent, handleKeydownEvent, handleBlur])
167173

0 commit comments

Comments
 (0)