Skip to content

Commit

Permalink
feat(client-electron): save state to files immediately
Browse files Browse the repository at this point in the history
  • Loading branch information
marcincichocki authored Oct 22, 2023
1 parent 4dc552a commit 6ae3bfb
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/electron/main/store/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ export class Store {
private state = this.getInitialState();

private middlewares: Middleware[] = [];
private stateImmediate: NodeJS.Immediate = null;

constructor(
private worker: WebContents,
Expand All @@ -163,12 +164,20 @@ export class Store {
}

dispatch(action: Action, notify = false) {
if (process.env.NODE_ENV === 'production') {
clearImmediate(this.stateImmediate);
}

this.applyMiddleware(action);
this.state = appReducer(this.state, action);

if (notify) {
this.notify(action);
}

if (process.env.NODE_ENV === 'production') {
this.stateImmediate = setImmediate(() => this.preserveState());
}
}

getState() {
Expand All @@ -177,6 +186,8 @@ export class Store {

dispose() {
if (process.env.NODE_ENV === 'production') {
clearImmediate(this.stateImmediate);

this.preserveState();
}

Expand Down Expand Up @@ -248,7 +259,11 @@ export class Store {
settings: { ...this.settings.store, screenshotDir },
status: WorkerStatus.Bootstrap,
updateStatus: null,
stats: this.stats.store,
stats: {
...this.stats.store,
countSuccessSession: 0,
countErrorSession: 0,
},
analysis: null,
};
}
Expand Down Expand Up @@ -300,10 +315,6 @@ export class Store {
private preserveState() {
this.history.set('data', this.state.history);
this.settings.set(this.state.settings);
this.stats.set({
...this.state.stats,
countSuccessSession: 0,
countErrorSession: 0,
});
this.stats.set(this.state.stats);
}
}

0 comments on commit 6ae3bfb

Please sign in to comment.