Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import {createSlice} from '@reduxjs/toolkit'; | ||
|
||
const gameSlice = createSlice({ | ||
name: 'game', | ||
initialState: {}, | ||
reducers: { | ||
tick: (state) => state, | ||
}, | ||
}); | ||
|
||
export default gameSlice; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import {configureStore} from '@reduxjs/toolkit'; | ||
import gameSlice from './gameSlice'; | ||
|
||
const store = configureStore({ | ||
reducer: { | ||
game: gameSlice.reducer, | ||
}, | ||
}); | ||
|
||
export default store; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import {start, stop} from './TickLoop'; | ||
|
||
jest.useFakeTimers(); | ||
jest.mock('LayoutAnimation'); | ||
|
||
it('starts and stops', () => { | ||
const callback = jest.fn(); | ||
|
||
start(callback, undefined, 20); | ||
jest.advanceTimersByTime(100); | ||
|
||
expect(callback).toHaveBeenCalledTimes(5); | ||
|
||
stop(); | ||
jest.advanceTimersByTime(100); | ||
|
||
expect(callback).toHaveBeenCalledTimes(5); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
let action: ((delta: number) => void) | undefined; | ||
let lastTime: number; | ||
let timer: number; | ||
|
||
export const start = ( | ||
callback: (delta: number) => void, | ||
time: number = Date.now(), | ||
delay?: number, | ||
) => { | ||
stop(); | ||
lastTime = time; | ||
action = callback; | ||
timer = setInterval(onFiredTimer, delay); | ||
}; | ||
|
||
const onFiredTimer = () => { | ||
if (!lastTime) { | ||
lastTime = Date.now(); | ||
return; | ||
} | ||
const current = Date.now(); | ||
const delta = current - lastTime; | ||
lastTime = current; | ||
|
||
action && action(delta); | ||
}; | ||
|
||
export const stop = () => { | ||
if (timer) { | ||
clearInterval(timer); | ||
} | ||
action = undefined; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters