Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch electron-store errors #4552

Merged
merged 1 commit into from
Mar 13, 2019
Merged

Catch electron-store errors #4552

merged 1 commit into from
Mar 13, 2019

Conversation

spoenemann
Copy link
Contributor

Fixes #4551.

  • Catch errors thrown by electron-store while saving the window state.
  • Added setTimeout to decrease the number of save operations. This significantly reduces the frequency of thrown errors.
  • When the window is maximized, don't store its bounds, but keep the previously stored bounds instead. This way the previous bounds are restored when unmaximizing.
  • Simplified parts of the code.

Signed-off-by: Miro Spönemann <[email protected]>
@paul-marechal
Copy link
Member

Does that mean that in the case where you would get an error, it wouldn't crash but it also wouldn't actually save the geometry?

@spoenemann
Copy link
Contributor Author

Correct, but it might save the geometry on the next call of that function.

I have not been able to find out what is the root cause of the problem, and I doubt that would be feasible because it occurs only in a very specific environment, not on every Windows system. So you could argue that my change does not actually fix #4551, but at least it makes sure that the error is not propagated to the user and that the error becomes so rare that the geometry is stored "often enough".

Copy link
Member

@jankeromnes jankeromnes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix! Generally looks good to me, so approving in the interest of time, but please have a look at my questions and nit-pickings below (feel free to ignore those that are not useful.)

I see that you've added a try { } catch (e) { console.error('...', e) } in the state-saving function. But you've also done some minor refactoring, and added debouncing on 'move' and 'resize' events. It would have been nice to mention the debouncing in the commit message as well.

const saveWindowStateDelayed = () => {
if (delayedSaveTimeout) {
clearTimeout(delayedSaveTimeout);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: You don't need to check for if (delayedSaveTimeout), because Passing an invalid ID to clearTimeout() silently does nothing; no exception is thrown.

Or, if you really want to implement an if, please fix the logic by setting delayedSaveTimeout = undefined after clearing the timeout.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The link you provided points to the web API. Does it also hold for the Node.js API?

}
};
let delayedSaveTimeout;
const saveWindowStateDelayed = () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I think the correct term for "Delayed" is "Debounced". If you agree, you could replace all occurrences of "Delayed" with "Debounced".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think both terms are valid...

try {
let bounds;
if (newWindow.isMaximized()) {
bounds = electronStore.get(WINDOW_STATE, {});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the point of re-doing electronStore.get(WINDOW_STATE, {}) when the window is maximized? Also:

  • Couldn't you just re-use windowState from above?

  • It seems your are calling electronStore.set() with the same values you got from electronStore.get(). Maybe just skip saving window state when it's maximized?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the point of re-doing electronStore.get(WINDOW_STATE, {}) when the window is maximized?

It makes sure that the bounds are kept, so after unmaximizing we get the same bounds we had before maximizing.

Couldn't you just re-use windowState from above?

No, the stored bounds might have been changed in the meantime. We could write the bounds into the local variable on every store operation if we want to avoid the additional get.

It seems your are calling electronStore.set() with the same values you got from electronStore.get().

No, the isMaximized property is changed.

@paul-marechal
Copy link
Member

paul-marechal commented Mar 13, 2019

sindresorhus/electron-store#31 (comment)

Maybe just a case of adding a debouncer?

edit: Ah @jankeromnes you beat me to it :)

@paul-marechal
Copy link
Member

paul-marechal commented Mar 13, 2019

Maybe just a case of adding a debouncer?

Which you did, didn't see the setTimeout before, my bad :)

Copy link
Member

@paul-marechal paul-marechal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@spoenemann spoenemann merged commit 785e74a into master Mar 13, 2019
@spoenemann spoenemann deleted the msp_issue4551 branch March 13, 2019 13:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants