Skip to content

Instantly share code, notes, and snippets.

@dearlordylord
Created August 19, 2024 17:44
Show Gist options
  • Save dearlordylord/9fc6b6b84ef9681f2ec839e7cbdc7193 to your computer and use it in GitHub Desktop.
Save dearlordylord/9fc6b6b84ef9681f2ec839e7cbdc7193 to your computer and use it in GitHub Desktop.
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