Skip to content

Commit

Permalink
fix: Javascript error on launch
Browse files Browse the repository at this point in the history
  • Loading branch information
sekwah41 committed Sep 30, 2023
1 parent ffdbd6e commit 83056fd
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 11 deletions.
4 changes: 2 additions & 2 deletions app/main/src/helpers/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { v4 as uuid } from "uuid";

import store from "../store";

const userId = store.get("userId") || uuid();
const userId = store.safeGet("userId") || uuid();

store.set("userId", userId);
store.safeSet("userId", userId);

function activateUser() {
const user = ua("UA-172128342-2", userId);
Expand Down
14 changes: 7 additions & 7 deletions app/main/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const getFrameHeight = () => {
if (isWindow()) {
return 502;
} else {
if (store.get("useNativeTitlebar")) {
if (store.safeGet("useNativeTitlebar")) {
return 488;
}
return 502;
Expand All @@ -91,9 +91,9 @@ function createMainWindow() {
resizable: true,
maximizable: false,
show: false,
frame: store.get("useNativeTitlebar"),
frame: store.safeGet("useNativeTitlebar"),
icon: getIcon(),
backgroundColor: store.get("isDarkMode") ? "#141e25" : "#fff",
backgroundColor: store.safeGet("isDarkMode") ? "#141e25" : "#fff",
webPreferences: {
contextIsolation: true,
backgroundThrottling: false,
Expand Down Expand Up @@ -372,7 +372,7 @@ ipcMain.on(SET_COMPACT_MODE, (e, args) => {
});

ipcMain.on(SET_UI_THEME, (e, { isDarkMode }) => {
store.set("isDarkMode", isDarkMode);
store.safeSet("isDarkMode", isDarkMode);
});

ipcMain.on(SET_SHOW, () => {
Expand Down Expand Up @@ -406,8 +406,8 @@ ipcMain.on(SET_CLOSE, (e, { closeToTray }) => {
});

ipcMain.on(SET_NATIVE_TITLEBAR, (e, { useNativeTitlebar }) => {
if (store.get("useNativeTitlebar") !== useNativeTitlebar) {
store.set("useNativeTitlebar", useNativeTitlebar);
if (store.safeGet("useNativeTitlebar") !== useNativeTitlebar) {
store.safeSet("useNativeTitlebar", useNativeTitlebar);
setTimeout(() => {
app.relaunch();
app.exit();
Expand All @@ -421,7 +421,7 @@ ipcMain.on(TRAY_ICON_UPDATE, (e, dataUrl) => {
});

ipcMain.on(SET_OPEN_AT_LOGIN, (e, { openAtLogin }) => {
store.set("openAtLogin", openAtLogin);
store.safeSet("openAtLogin", openAtLogin);
app.setLoginItemSettings({
openAtLogin: openAtLogin,
openAsHidden: openAtLogin,
Expand Down
46 changes: 44 additions & 2 deletions app/main/src/store.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Store from "electron-store";
import Store, { Options } from "electron-store";
import { nativeTheme } from "electron";
import { isWindow } from "./helpers";
import ElectronStore from "electron-store";

type StoreProps = {
userId?: string;
Expand All @@ -10,7 +11,48 @@ type StoreProps = {
openAtLogin?: boolean;
};

const store = new Store<StoreProps>({
/**
* Was going to make SafeStore extend Store but it didn't seem to work how it should. (Class constructor ElectronStore cannot be invoked without 'new')
*
* This also ensures that we can force calling the store safely. Though I have switched the names to safeGet and safeSet to make it more clear.
*/
class SafeStore<
T extends Record<string, any> = Record<string, unknown>
> {
private store: ElectronStore<T>;
constructor(props: Options<T>) {
this.store = new Store<T>(props);
}

/**
* Safely set a value in the store and catch errors
* @param key
* @param value
*/
safeSet<Key extends keyof T>(key: Key, value?: T[Key]) {
try {
this.store.set(key, value);
} catch (error) {
console.error("[Store] Safe Set", error);
}
}

/**
* Safely get a value from the store and catch errors
* @param key
*/
safeGet<Key extends keyof T>(key: Key): T[Key] | undefined {
try {
return this.store.get(key);
} catch (error) {
console.error("[Store] Safe Get", error);
}
return undefined;
}
}

// Wrap the store due to a delete issue
const store = new SafeStore<StoreProps>({
defaults: {
isDarkMode: nativeTheme.shouldUseDarkColors,
useNativeTitlebar: !isWindow(),
Expand Down

0 comments on commit 83056fd

Please sign in to comment.