mirror of
https://github.com/BorysLevytskyi/BitwiseCmd.git
synced 2026-01-24 20:54:10 +01:00
Simplify bitwisecmm (#16)
This commit is contained in:
@@ -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...
|
||||
}
|
||||
Reference in New Issue
Block a user