1
0
mirror of https://github.com/mgerb/ps-launcher synced 2026-01-09 01:52:57 +00:00

data persistence changes

This commit is contained in:
2017-10-26 19:39:11 -05:00
parent f584a5a2f9
commit bfce7938e9
4 changed files with 53 additions and 32 deletions

View File

@@ -5,3 +5,5 @@ declare module JSX {
'webview': any; 'webview': any;
} }
} }
declare const VERSION: any;

View File

@@ -1,4 +1,12 @@
# Work in progress # PS Launcher
### Work in progress
## Development Environment
- Clone project
- [Install yarn](https://yarnpkg.com/en/)
- `yarn install`
- `yarn run dev` - compile javascript
- `yarn start` - start electron app
## TODO ## TODO
- Add modals for server editing - Add modals for server editing

View File

@@ -8,8 +8,6 @@ interface Props {
AppState?: AppState; AppState?: AppState;
} }
declare const VERSION: any;
@inject('AppState') @inject('AppState')
@observer @observer
export class Header extends React.Component<Props, any> { export class Header extends React.Component<Props, any> {

View File

@@ -18,19 +18,20 @@ export interface ServerType {
} }
export class AppState { export class AppState {
private appPath: string;
private persistentFilePath: string;
constructor() { // read only app data
this.appPath = remote.app.getPath('appData') + '/' + remote.app.getName(); private readonly appPath: string = remote.app.getPath('appData') + '/' + remote.app.getName();
this.persistentFilePath = this.appPath + '/state.json'; private readonly persistentFilePath: string = this.appPath + '/app-state.json';
this.bootstrap(); private readonly appVersion: string = VERSION;
}
@observable public expansions: { [key: string]: ExpansionType }; @observable public expansions: { [key: string]: ExpansionType };
@observable public isBootstrapped: boolean = false; @observable public isBootstrapped: boolean = false;
@observable public selectedExpKey: string = 'vanilla'; @observable public selectedExpKey: string = 'vanilla';
constructor() {
this.bootstrap();
}
@computed @computed
public get selectedExpansion(): ExpansionType { public get selectedExpansion(): ExpansionType {
return _.get(this.expansions, `[${this.selectedExpKey}]`); return _.get(this.expansions, `[${this.selectedExpKey}]`);
@@ -50,53 +51,65 @@ export class AppState {
@action @action
public setSelectedServerIndex(index: number): void { public setSelectedServerIndex(index: number): void {
this.selectedExpansion.selectedServerIndex = index; this.selectedExpansion.selectedServerIndex = index;
this.updateFile(this.expansions); this.persistState();
} }
@action @action
public setSelectedExpansion(exp: string): void { public setSelectedExpansion(exp: string): void {
this.selectedExpKey = exp; this.selectedExpKey = exp;
this.persistState();
} }
@action @action
public setDirectory(dir: string): void { public setDirectory(dir: string): void {
this.selectedExpansion.directory = dir; this.selectedExpansion.directory = dir;
this.updateFile(this.expansions); this.persistState();
} }
// bootstrap application // bootstrap application
// creates directory and state.json in appData // creates directory and persistent store in appData
@action @action
private bootstrap(): void { private bootstrap(): void {
if (!fs.statSync(this.appPath).isDirectory()) { if (!fs.statSync(this.appPath).isDirectory()) {
fs.mkdirSync(this.appPath); fs.mkdirSync(this.appPath);
} }
fs.stat(this.persistentFilePath, err => { fs.readFile(this.persistentFilePath, (err, data) => {
// create file if not exists
if (err) { runInAction(() => {
runInAction(() => { // create file if not exists
if (err) {
this.expansions = persistentStateSeed(); this.expansions = persistentStateSeed();
this.isBootstrapped = true; this.isBootstrapped = true;
}); this.persistState();
this.updateFile(this.expansions); } else {
} else { // TODO: future note - grab app version here and update
fs.readFile(this.persistentFilePath, (err, data) => { // any persisted state accordingly after app has been updated
if (!err) { const storedData = JSON.parse(data.toString()) as any;
runInAction(() => { this.expansions = storedData.expansions;
this.expansions = JSON.parse(data.toString()) as any; this.selectedExpKey = storedData.selectedExpKey;
this.isBootstrapped = true; this.isBootstrapped = true;
}); }
} });
});
}
}); });
} }
private updateFile(exp: any): Promise<void> { // save state of app to file in app data
private persistState(): Promise<void> {
// select what we want to persist
const { appVersion, expansions, selectedExpKey } = this;
// create new object of what we want to persist
const persistedState = {
appVersion,
expansions,
selectedExpKey,
};
// write our new object to a file
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
fs.writeFile(this.persistentFilePath, JSON.stringify(exp, null, 2), {}, err => { fs.writeFile(this.persistentFilePath, JSON.stringify(persistedState, null, 2), {}, err => {
err ? reject(err) : resolve(); err ? reject(err) : resolve();
}); });
}); });