Skip to content

Commit cadf868

Browse files
committed
[feat] Remove unnecessary key listener calls
1 parent dde686e commit cadf868

File tree

3 files changed

+5
-12
lines changed

3 files changed

+5
-12
lines changed

src/components/atoms/Game.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export function Game({
9292
* MARK: Init
9393
*/
9494

95-
const keydownListener = $.on(document, 'keydown', (e) => {
95+
$.on(document, 'keydown', (e) => {
9696
if (e.key === 'p') {
9797
isPaused = !isPaused;
9898
if (isPaused) $.audio.pause();
@@ -108,10 +108,7 @@ export function Game({
108108

109109
$.audio.register(data.gameSound);
110110

111-
el.$unmount = () => {
112-
$.audio.stop();
113-
keydownListener();
114-
};
111+
el.$unmount = () => $.audio.stop();
115112
}}
116113
</Script>
117114
</div>

src/routes/snake/Game.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ export function SnakeGame() {
271271
clearSegmentTimer();
272272
});
273273

274-
const keyboardListener = $.on(document, 'keydown', (e) => {
274+
$.on(document, 'keydown', (e) => {
275275
if (isPaused) return;
276276
switch (e.key) {
277277
case 'ArrowUp':
@@ -288,7 +288,6 @@ export function SnakeGame() {
288288
el.$unmount = () => {
289289
clearTimer();
290290
clearSegmentTimer();
291-
keyboardListener();
292291
};
293292
}}
294293
</Script>

src/routes/tetris/Game.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ export function TetrisGame() {
237237

238238
el.$subscribe('tetris:gameover', () => clearTimer());
239239

240-
const keydownhandler = $.on(document, 'keydown', (e) => {
240+
$.on(document, 'keydown', (e) => {
241241
if (isPaused || !tick) return;
242242
let dir: TetrisGameEvents['tetris:evt:move'] | null = null;
243243
switch (e.key) {
@@ -260,10 +260,7 @@ export function TetrisGame() {
260260
if (dir) el.$publish('tetris:evt:move', dir);
261261
});
262262

263-
el.$unmount = () => {
264-
keydownhandler();
265-
clearTimer();
266-
};
263+
el.$unmount = () => clearTimer();
267264
}}
268265
</Script>
269266
</Game>

0 commit comments

Comments
 (0)