Simplify bitwisecmm (#16)

This commit is contained in:
Borys Levytskyi
2021-01-14 20:07:59 +02:00
committed by GitHub
parent 49e5265985
commit f671b32b63
16 changed files with 85 additions and 193 deletions

View File

@@ -9,6 +9,12 @@ export type PersistedAppData = {
debugMode: boolean | null;
}
export type CommandResultView = {
key: number,
input: string,
view: JSX.Element
};
export type AppStateChangeHandler = (state: AppState) => void;
export default class AppState {
@@ -17,15 +23,15 @@ export default class AppState {
emphasizeBytes: boolean;
debugMode: boolean = false;
uiTheme: string;
handlers: AppStateChangeHandler[];
commandResults: any[];
changeHandlers: AppStateChangeHandler[];
commandResults: CommandResultView[];
persistedVersion: number;
wasOldVersion: boolean;
env: string;
constructor(persistData : PersistedAppData, env: string) {
this.commandResults = [];
this.handlers = [];
this.changeHandlers = [];
this.uiTheme = persistData.uiTheme || 'midnight';
this.env = env;
@@ -35,9 +41,10 @@ export default class AppState {
this.debugMode = env !== 'prod' || persistData.debugMode === true;
}
addCommandResult(result : any) {
this.commandResults.unshift(result);
log.debug("result added", result);
addCommandResult(input : string, view : JSX.Element) {
const key = generateKey();
this.commandResults.unshift({key, input, view});
log.debug(`command result added: ${input}`);
this.triggerChanged();
}
@@ -52,11 +59,11 @@ export default class AppState {
}
onChange(handler : AppStateChangeHandler) {
this.handlers.push(handler);
this.changeHandlers.push(handler);
}
triggerChanged() {
this.handlers.forEach(h => h(this));
this.changeHandlers.forEach(h => h(this));
}
setUiTheme(theme: string) {
@@ -77,4 +84,8 @@ export default class AppState {
debugMode: this.debugMode
}
}
};
};
function generateKey() : number {
return Math.ceil(Math.random()*10000000) ^ Date.now(); // Because why the hell not...
}