@@ -34,7 +34,7 @@ export type TetrisGameEvents = {
3434
3535export function TetrisGame ( ) {
3636 return (
37- < GameModal columns = { TETRIS_CFG . COLS } rows = { TETRIS_CFG . ROWS } copyWidth = { 45 } >
37+ < GameModal columns = { TETRIS_CFG . COLS } copyWidth = { 45 } >
3838 < GameConfig preview = { PREVIEW } />
3939 < GameSidebar evtStart = "tetris:start" evtEnd = "tetris:gameover" width = { 35 } >
4040 < GameExplanation
@@ -118,8 +118,13 @@ export function TetrisGame() {
118118 render ( ) ;
119119 }
120120
121- function setTimer ( ) {
121+ function clearTimer ( ) {
122122 if ( tick ) clearInterval ( tick ) ;
123+ tick = null ;
124+ }
125+
126+ function setTimer ( ) {
127+ clearTimer ( ) ;
123128 tick = setInterval ( ( ) => {
124129 if ( ! isPaused ) {
125130 el . $publish ( 'tetris:evt:move' , 'down' ) ;
@@ -226,12 +231,9 @@ export function TetrisGame() {
226231
227232 el . $subscribe ( 'tetris:pause' , ( ) => ( isPaused = ! isPaused ) ) ;
228233
229- el . $subscribe ( 'tetris:gameover' , ( ) => {
230- if ( tick ) clearInterval ( tick ) ;
231- tick = null ;
232- } ) ;
234+ el . $subscribe ( 'tetris:gameover' , ( ) => clearTimer ( ) ) ;
233235
234- $ . on ( document , 'keydown' , ( e ) => {
236+ const keydownhandler = $ . on ( document , 'keydown' , ( e ) => {
235237 if ( isPaused || ! tick ) return ;
236238 let dir : TetrisGameEvents [ 'tetris:evt:move' ] | null = null ;
237239 switch ( e . key ) {
@@ -253,6 +255,11 @@ export function TetrisGame() {
253255 }
254256 if ( dir ) el . $publish ( 'tetris:evt:move' , dir ) ;
255257 } ) ;
258+
259+ el . $unmount = ( ) => {
260+ keydownhandler ( ) ;
261+ clearTimer ( ) ;
262+ } ;
256263 } }
257264 </ Script >
258265 </ Game >
0 commit comments