import log from 'loglevel' ; const APP_VERSION = 7 ; export type PersistedAppData = { emphasizeBytes : boolean ; uiTheme : string ; version : number ; debugMode : boolean | null ; pageVisistsCount : number ; donationClicked : boolean ; } ; export type CommandResultView = { key : number ; input : string ; view : JSX . Element ; } ; export type AppStateChangeHandler = ( state : AppState ) => void ; export default class AppState { version : number = APP_VERSION ; emphasizeBytes : boolean ; debugMode : boolean = false ; uiTheme : string ; changeHandlers : AppStateChangeHandler [ ] ; commandResults : CommandResultView [ ] ; persistedVersion : number ; wasOldVersion : boolean ; env : string ; pageVisitsCount : number ; donationClicked : boolean ; constructor ( persistData , env ) { this . commandResults = [ ] ; this . changeHandlers = [ ] ; this . uiTheme = persistData . uiTheme || 'midnight' ; this . env = env ; this . emphasizeBytes = persistData . emphasizeBytes || true ; this . persistedVersion = persistData . version || 0.1 ; this . wasOldVersion = persistData . version != null && this . version > this . persistedVersion ; this . debugMode = env !== 'prod' || persistData . debugMode === true ; this . pageVisitsCount = persistData . pageVisistsCount || 0 ; this . donationClicked = persistData . donationClicked ; } addCommandResult ( input , view ) { const key = generateKey ( ) ; this . commandResults . unshift ( { key , input , view } ) ; log . debug ( `command result added: ${ input }` ) ; this . triggerChanged ( ) ; } clearCommandResults ( ) { this . commandResults = [ ] ; this . triggerChanged ( ) ; } removeResult ( index ) { if ( index < 0 || index >= this . commandResults . length ) return ; this . commandResults . splice ( index , 1 ) ; this . triggerChanged ( ) ; } toggleEmphasizeBytes ( ) { this . emphasizeBytes = ! this . emphasizeBytes ; this . triggerChanged ( ) ; } onChange ( handler ) { this . changeHandlers . push ( handler ) ; } triggerChanged ( ) { this . changeHandlers . forEach ( h => h ( this ) ) ; } setUiTheme ( theme ) { this . uiTheme = theme ; this . triggerChanged ( ) ; } toggleDebugMode ( ) { this . debugMode = ! this . debugMode ; this . triggerChanged ( ) ; } registerVisit ( ) { this . pageVisitsCount ++ ; this . triggerChanged ( ) ; } onDonationClicked ( ) { if ( this . donationClicked === true ) return false ; this . donationClicked = true ; this . triggerChanged ( ) ; return true ; } getPersistData ( ) { return { emphasizeBytes : this . emphasizeBytes , uiTheme : this . uiTheme , version : this . version , debugMode : this . debugMode , pageVisistsCount : this . pageVisitsCount , donationClicked : this . donationClicked } ; } } ; function generateKey ( ) { return Math . ceil ( Math . random ( ) * 10000000 ) ^ Date . now ( ) ; }