From befe2263109eda7d192fd7d7cf8adbf399ac5852 Mon Sep 17 00:00:00 2001 From: WarningImHack3r <43064022+WarningImHack3r@users.noreply.github.com> Date: Mon, 4 May 2026 23:42:39 +0200 Subject: [PATCH 1/2] perf: use sets internally for PressedKeys --- .../pressed-keys/pressed-keys.svelte.ts | 23 ++++++++----------- pnpm-workspace.yaml | 10 ++++---- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/packages/runed/src/lib/utilities/pressed-keys/pressed-keys.svelte.ts b/packages/runed/src/lib/utilities/pressed-keys/pressed-keys.svelte.ts index 25e20abc..6a696283 100644 --- a/packages/runed/src/lib/utilities/pressed-keys/pressed-keys.svelte.ts +++ b/packages/runed/src/lib/utilities/pressed-keys/pressed-keys.svelte.ts @@ -3,7 +3,7 @@ import { createSubscriber } from "svelte/reactivity"; import { watch } from "$lib/utilities/watch/index.js"; import { defaultWindow, type ConfigurableWindow } from "$lib/internal/configurable-globals.js"; -const modifierKeys = ["meta", "control", "alt", "shift"]; +const modifierKeys = new Set(["meta", "control", "alt", "shift"]); export type PressedKeysOptions = ConfigurableWindow; /** @@ -12,7 +12,7 @@ export type PressedKeysOptions = ConfigurableWindow; * @see {@link https://runed.dev/docs/utilities/pressed-keys} */ export class PressedKeys { - #pressedKeys = $state([]); + #pressedKeys = new Set(); readonly #subscribe?: () => void; constructor(options: PressedKeysOptions = {}) { @@ -24,9 +24,7 @@ export class PressedKeys { this.#subscribe = createSubscriber((update) => { const keydown = on(window, "keydown", (e) => { const key = e.key.toLowerCase(); - if (!this.#pressedKeys.includes(key)) { - this.#pressedKeys.push(key); - } + this.#pressedKeys.add(key); update(); }); @@ -36,22 +34,22 @@ export class PressedKeys { // Special handling for modifier keys (meta, control, alt, shift) // This addresses issues with OS/browser intercepting certain key combinations // where non-modifier keyup events might not fire properly - if (modifierKeys.includes(key)) { + if (modifierKeys.has(key)) { // When a modifier key is released, clear all non-modifier keys // but keep other modifier keys that might still be pressed // This prevents keys from getting "stuck" in the pressed state - this.#pressedKeys = this.#pressedKeys.filter((k) => modifierKeys.includes(k)); + this.#pressedKeys = this.#pressedKeys.intersection(modifierKeys); } // Regular key removal - this.#pressedKeys = this.#pressedKeys.filter((k) => k !== key); + this.#pressedKeys.delete(key); update(); }); // Handle window blur events (switching applications, clicking outside browser) // Reset all keys when user shifts focus away from the window const blur = on(window, "blur", () => { - this.#pressedKeys = []; + this.#pressedKeys.clear(); update(); }); @@ -59,7 +57,7 @@ export class PressedKeys { // This catches cases where the window doesn't lose focus but the tab is hidden const visibilityChange = on(document, "visibilitychange", () => { if (document.visibilityState === "hidden") { - this.#pressedKeys = []; + this.#pressedKeys.clear(); update(); } }); @@ -75,13 +73,12 @@ export class PressedKeys { has(...keys: string[]): boolean { this.#subscribe?.(); - const normalizedKeys = keys.map((key) => key.toLowerCase()); - return normalizedKeys.every((key) => this.#pressedKeys.includes(key)); + return keys.every((key) => this.#pressedKeys.has(key.toLowerCase())); } get all(): string[] { this.#subscribe?.(); - return this.#pressedKeys; + return [...this.#pressedKeys]; } /** diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 5e495289..f817dc53 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -3,9 +3,9 @@ packages: - sites/* catalog: - "@sveltejs/kit": ^2.42.0 - "@sveltejs/vite-plugin-svelte": ^6.2.0 - "@types/node": ^20.19.16 + '@sveltejs/kit': ^2.42.0 + '@sveltejs/vite-plugin-svelte': ^6.2.0 + '@types/node': ^20.19.16 svelte: ^5.39.3 svelte-check: ^4.3.1 typescript: ^5.9.2 @@ -13,6 +13,8 @@ catalog: vitest: ^3.2.4 onlyBuiltDependencies: - - "@tailwindcss/oxide" + - '@tailwindcss/oxide' + - esbuild - msw + - sharp - workerd From 75507688d3ce803671c97066b56e070f6a18e627 Mon Sep 17 00:00:00 2001 From: WarningImHack3r <43064022+WarningImHack3r@users.noreply.github.com> Date: Mon, 4 May 2026 23:59:01 +0200 Subject: [PATCH 2/2] add changeset --- .changeset/spicy-bats-stare.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/spicy-bats-stare.md diff --git a/.changeset/spicy-bats-stare.md b/.changeset/spicy-bats-stare.md new file mode 100644 index 00000000..6aea6be8 --- /dev/null +++ b/.changeset/spicy-bats-stare.md @@ -0,0 +1,5 @@ +--- +"runed": patch +--- + +perf(pressed-keys): improve internal performance