1
-
2
-
3
- module . exports = ( getBallLevel ) => {
1
+ module . exports = ( getBallLevel , sleepFunction ) => {
4
2
getBallLevel = getBallLevel || ( level => Math . floor ( Math . random ( ) * level ) )
3
+ sleepFunction = sleepFunction || ( cb => setTimeout ( cb , 300 ) )
5
4
6
5
const thresholds = [ 0 , 0 , 5 , 30 , 90 , 150 , 230 , 280 , 350 , 450 ]
7
6
var blocksKilled = 0
8
7
var updateCallback = ( ) => { }
8
+ var queuedUpdate = null
9
9
var dotsId = 0
10
10
var state = {
11
11
dots : [ ] ,
@@ -30,7 +30,11 @@ module.exports = (getBallLevel) => {
30
30
31
31
function setState ( newState ) {
32
32
state = Object . assign ( { } , state , newState )
33
- updateCallback ( newState )
33
+ if ( queuedUpdate ) return
34
+ queuedUpdate = setTimeout ( ( ) => {
35
+ updateCallback ( state )
36
+ queuedUpdate = null
37
+ } )
34
38
}
35
39
36
40
function newDot ( x , level ) {
@@ -52,7 +56,6 @@ module.exports = (getBallLevel) => {
52
56
return
53
57
}
54
58
setState ( {
55
- ...state ,
56
59
floatingDots : state . floatingDots . map ( d => {
57
60
return {
58
61
...d ,
@@ -142,15 +145,14 @@ module.exports = (getBallLevel) => {
142
145
}
143
146
}
144
147
145
- const newDots = dots . filter ( d => keep [ d . x ] [ d . y ] ) . concat ( freshDots )
146
- return newDots
148
+ return dots . filter ( d => keep [ d . x ] [ d . y ] ) . concat ( freshDots )
147
149
}
148
150
149
151
function updateScore ( score ) {
150
- if ( typeof window === 'undefined' ) return
151
152
const highScore = Math . max ( score , state . highScore )
153
+ setState ( { score, highScore } )
154
+ if ( typeof window === 'undefined' ) return
152
155
window . localStorage [ 'score' ] = highScore
153
- setState ( { highScore, score } )
154
156
}
155
157
156
158
function moveDotsRight ( ) {
@@ -188,19 +190,17 @@ module.exports = (getBallLevel) => {
188
190
}
189
191
} )
190
192
setState ( {
191
- ...state ,
192
193
dots : inactiveDots . concat ( newDots ) ,
193
194
floatingDots : [ ]
194
195
} )
195
- setTimeout ( ( ) => cleanupBoard ( [ ] , state . dots ) , 300 )
196
+ sleepFunction ( ( ) => cleanupBoard ( [ ] , state . dots ) )
196
197
}
197
198
198
199
function cleanupBoard ( oldRun , newRun ) {
199
200
oldRun = newRun
200
201
newRun = cleanupDots ( oldRun )
201
202
const level = thresholds . findIndex ( l => l > blocksKilled )
202
203
setState ( {
203
- ...state ,
204
204
level : level < 0 ? colors . length - 1 : level ,
205
205
dots : newRun
206
206
} )
@@ -213,18 +213,16 @@ module.exports = (getBallLevel) => {
213
213
} )
214
214
}
215
215
return setState ( {
216
- ...state ,
217
216
floatingDots : [ newDot ( 4 ) , newDot ( 5 ) ]
218
217
} )
219
218
}
220
- setTimeout ( ( ) => {
219
+ sleepFunction ( ( ) => {
221
220
newRun = settleDots ( newRun )
222
221
setState ( {
223
- ...state ,
224
222
dots : newRun
225
223
} )
226
- setTimeout ( ( ) => cleanupBoard ( oldRun , newRun ) , 300 )
227
- } , 300 )
224
+ sleepFunction ( ( ) => cleanupBoard ( oldRun , newRun ) )
225
+ } )
228
226
}
229
227
230
228
function rotateDots ( ) {
@@ -287,7 +285,6 @@ module.exports = (getBallLevel) => {
287
285
} ) )
288
286
}
289
287
setState ( {
290
- ...state ,
291
288
floatingDots : newFloatingDots
292
289
} )
293
290
}
0 commit comments