Skip to content

Commit 728ea84

Browse files
committed
fix: init store after app init
Signed-off-by: Innei <tukon479@gmail.com>
1 parent b541b48 commit 728ea84

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

apps/main/src/init.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ import { initializeSentry } from "./sentry"
1919
import { router } from "./tipc"
2020
import { createMainWindow, getMainWindow } from "./window"
2121

22-
const appFolder = {
23-
prod: "Follow",
24-
dev: "Follow (dev)",
25-
}
2622
if (process.argv.length === 3 && process.argv[2].startsWith("follow-dev:")) {
2723
process.env.NODE_ENV = "development"
2824
}
@@ -32,7 +28,8 @@ const isDev = process.env.NODE_ENV === "development"
3228
* Mandatory and fast initializers for the app
3329
*/
3430
export function initializeAppStage0() {
35-
app.setPath("appData", path.join(app.getPath("appData"), isDev ? appFolder.dev : appFolder.prod))
31+
if (isDev) app.setPath("appData", path.join(app.getPath("appData"), "Follow (dev)"))
32+
initializeSentry()
3633
}
3734
export const initializeAppStage1 = () => {
3835
if (process.defaultApp) {
@@ -45,8 +42,6 @@ export const initializeAppStage1 = () => {
4542
app.setAsDefaultProtocolClient(APP_PROTOCOL)
4643
}
4744

48-
initializeSentry()
49-
5045
registerIpcMain(router)
5146

5247
if (app.dock) {

apps/main/src/lib/store.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,26 @@ import { resolve } from "node:path"
33
import { app } from "electron"
44
import { JSONFileSyncPreset } from "lowdb/node"
55

6-
const db = JSONFileSyncPreset(resolve(app.getPath("userData"), "db.json"), {}) as {
6+
let db: {
77
data: Record<string, unknown>
88
write: () => void
99
read: () => void
1010
}
1111

12+
const createOrGetDb = () => {
13+
if (!db) {
14+
db = JSONFileSyncPreset(resolve(app.getPath("userData"), "db.json"), {}) as typeof db
15+
}
16+
return db
17+
}
1218
export const store = {
13-
get: (key: string) => db.data[key] as any,
19+
get: (key: string) => {
20+
const db = createOrGetDb()
21+
22+
return db.data[key] as any
23+
},
1424
set: (key: string, value: any) => {
25+
const db = createOrGetDb()
1526
db.data[key] = value
1627
db.write()
1728
},

0 commit comments

Comments
 (0)