-
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(keybinds): Priority queue for modal keybinds
Found a random todo
- Loading branch information
Showing
5 changed files
with
232 additions
and
20 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
export type SchedulePriority = | ||
| 'user-blocking' | ||
| 'user-visible' | ||
| 'background'; | ||
|
||
type KeybindHandler = (e: KeyboardEvent) => void | Promise<void>; | ||
|
||
const registeredHandlers = new Map<KeybindHandler, { | ||
priority: number; | ||
schedule: SchedulePriority; | ||
key: string; | ||
}>(); | ||
|
||
export function registerKeybindWithPriority(key: string, handler: KeybindHandler, priority = 0, schedule: SchedulePriority = 'user-blocking') { | ||
registeredHandlers.set(handler, { | ||
priority, | ||
key, | ||
schedule | ||
}) | ||
} | ||
|
||
export function unregisterKeybindWithPriority(handler: KeybindHandler) { | ||
registeredHandlers.delete(handler); | ||
} | ||
|
||
function catchAll(e: KeyboardEvent) { | ||
const entries = [...registeredHandlers.entries()]; | ||
const sorted = entries.toSorted(([_, { priority: a }], [__, { priority: b }]) => b - a); | ||
let maxPrio = 0; | ||
|
||
for (const [handler, { priority, key, schedule }] of sorted) { | ||
maxPrio = Math.max(maxPrio, priority); | ||
if (e.key !== key) return; | ||
|
||
if (priority < maxPrio) return; | ||
|
||
switch (schedule) { | ||
case "user-blocking": { | ||
queueMicrotask(() => handler(e)); | ||
break; | ||
} | ||
case "user-visible": { | ||
setTimeout(() => handler(e), 0); | ||
break; | ||
} | ||
case "background": { | ||
requestIdleCallback(() => handler(e)); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Needs to be called if you want to register handlers on key down | ||
*/ | ||
export function registerKeybindsWithPriority() { | ||
document.addEventListener('keydown', catchAll) | ||
} | ||
|
||
/** | ||
* Use whenever you need to clean up the registered handlers | ||
*/ | ||
export function disposeKeybindsWithPriority() { | ||
registeredHandlers.clear(); | ||
document.removeEventListener('keydown', catchAll) | ||
} |