Created
August 19, 2024 17:44
-
-
Save dearlordylord/9fc6b6b84ef9681f2ec839e7cbdc7193 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
type State = { ms: number, lightOn: boolean }; | |
const initialState = {ms: 0, lightOn: false}; | |
type Event = { type: 'TIME_PASSED'; ms: number }; | |
const reduce = (event: Event) => (state: State): State => ((newMs) => ((toggle) => toggle ? { | |
// ignore remaining time for simplicity | |
ms: 0, | |
lightOn: !state.lightOn | |
} : { | |
...state, | |
ms: newMs | |
})(newMs >= 3000))(event.ms + state.ms); | |
let state = initialState; | |
setInterval(() => { | |
state = reduce({type: 'TIME_PASSED', ms: 1000})(state); | |
console.log('lights on:', state.lightOn); | |
}, 1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment