Skip to content

Commit da6ddd3

Browse files
committed
modern classes and lint fix
1 parent d67272a commit da6ddd3

File tree

5 files changed

+170
-175
lines changed

5 files changed

+170
-175
lines changed

packages/keys/src/key-state.ts

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export type KeyStateListener = (keys: Array<string>) => void
6161
* ```
6262
*/
6363
export class KeyStateTracker {
64-
private static instance: KeyStateTracker | null = null
64+
static #instance: KeyStateTracker | null = null
6565

6666
/**
6767
* The TanStack Store instance containing the tracker state.
@@ -71,102 +71,102 @@ export class KeyStateTracker {
7171
getDefaultKeyStateTrackerState(),
7272
)
7373

74-
private heldKeysSet: Set<string> = new Set()
75-
private keydownListener: ((event: KeyboardEvent) => void) | null = null
76-
private keyupListener: ((event: KeyboardEvent) => void) | null = null
77-
private blurListener: (() => void) | null = null
74+
#heldKeysSet: Set<string> = new Set()
75+
#keydownListener: ((event: KeyboardEvent) => void) | null = null
76+
#keyupListener: ((event: KeyboardEvent) => void) | null = null
77+
#blurListener: (() => void) | null = null
7878

7979
private constructor() {
80-
this.setupListeners()
80+
this.#setupListeners()
8181
}
8282

8383
/**
8484
* Gets the singleton instance of KeyStateTracker.
8585
*/
8686
static getInstance(): KeyStateTracker {
87-
if (!KeyStateTracker.instance) {
88-
KeyStateTracker.instance = new KeyStateTracker()
87+
if (!KeyStateTracker.#instance) {
88+
KeyStateTracker.#instance = new KeyStateTracker()
8989
}
90-
return KeyStateTracker.instance
90+
return KeyStateTracker.#instance
9191
}
9292

9393
/**
9494
* Resets the singleton instance. Useful for testing.
9595
*/
9696
static resetInstance(): void {
97-
if (KeyStateTracker.instance) {
98-
KeyStateTracker.instance.destroy()
99-
KeyStateTracker.instance = null
97+
if (KeyStateTracker.#instance) {
98+
KeyStateTracker.#instance.destroy()
99+
KeyStateTracker.#instance = null
100100
}
101101
}
102102

103103
/**
104104
* Sets up the keyboard event listeners.
105105
*/
106-
private setupListeners(): void {
106+
#setupListeners(): void {
107107
if (typeof document === 'undefined') {
108108
return // SSR safety
109109
}
110110

111-
this.keydownListener = (event: KeyboardEvent) => {
111+
this.#keydownListener = (event: KeyboardEvent) => {
112112
const key = normalizeKeyName(event.key)
113-
if (!this.heldKeysSet.has(key)) {
114-
this.heldKeysSet.add(key)
113+
if (!this.#heldKeysSet.has(key)) {
114+
this.#heldKeysSet.add(key)
115115
this.#syncState()
116116
}
117117
}
118118

119-
this.keyupListener = (event: KeyboardEvent) => {
119+
this.#keyupListener = (event: KeyboardEvent) => {
120120
const key = normalizeKeyName(event.key)
121-
if (this.heldKeysSet.has(key)) {
122-
this.heldKeysSet.delete(key)
121+
if (this.#heldKeysSet.has(key)) {
122+
this.#heldKeysSet.delete(key)
123123
this.#syncState()
124124
}
125125
}
126126

127127
// Clear all keys when window loses focus (keys might be released while not focused)
128-
this.blurListener = () => {
129-
if (this.heldKeysSet.size > 0) {
130-
this.heldKeysSet.clear()
128+
this.#blurListener = () => {
129+
if (this.#heldKeysSet.size > 0) {
130+
this.#heldKeysSet.clear()
131131
this.#syncState()
132132
}
133133
}
134134

135-
document.addEventListener('keydown', this.keydownListener)
136-
document.addEventListener('keyup', this.keyupListener)
137-
window.addEventListener('blur', this.blurListener)
135+
document.addEventListener('keydown', this.#keydownListener)
136+
document.addEventListener('keyup', this.#keyupListener)
137+
window.addEventListener('blur', this.#blurListener)
138138
}
139139

140140
/**
141141
* Syncs the internal Set to the Store state.
142142
*/
143143
#syncState(): void {
144144
this.store.setState(() => ({
145-
heldKeys: Array.from(this.heldKeysSet),
145+
heldKeys: Array.from(this.#heldKeysSet),
146146
}))
147147
}
148148

149149
/**
150150
* Removes the keyboard event listeners.
151151
*/
152-
private removeListeners(): void {
152+
#removeListeners(): void {
153153
if (typeof document === 'undefined') {
154154
return
155155
}
156156

157-
if (this.keydownListener) {
158-
document.removeEventListener('keydown', this.keydownListener)
159-
this.keydownListener = null
157+
if (this.#keydownListener) {
158+
document.removeEventListener('keydown', this.#keydownListener)
159+
this.#keydownListener = null
160160
}
161161

162-
if (this.keyupListener) {
163-
document.removeEventListener('keyup', this.keyupListener)
164-
this.keyupListener = null
162+
if (this.#keyupListener) {
163+
document.removeEventListener('keyup', this.#keyupListener)
164+
this.#keyupListener = null
165165
}
166166

167-
if (this.blurListener) {
168-
window.removeEventListener('blur', this.blurListener)
169-
this.blurListener = null
167+
if (this.#blurListener) {
168+
window.removeEventListener('blur', this.#blurListener)
169+
this.#blurListener = null
170170
}
171171
}
172172

@@ -187,7 +187,7 @@ export class KeyStateTracker {
187187
*/
188188
isKeyHeld(key: string): boolean {
189189
const normalizedKey = normalizeKeyName(key)
190-
return this.heldKeysSet.has(normalizedKey)
190+
return this.#heldKeysSet.has(normalizedKey)
191191
}
192192

193193
/**
@@ -231,8 +231,8 @@ export class KeyStateTracker {
231231
* Destroys the tracker and removes all listeners.
232232
*/
233233
destroy(): void {
234-
this.removeListeners()
235-
this.heldKeysSet.clear()
234+
this.#removeListeners()
235+
this.#heldKeysSet.clear()
236236
this.store.setState(() => getDefaultKeyStateTrackerState())
237237
}
238238
}

0 commit comments

Comments
 (0)