Skip to content

Commit

Permalink
fix: init store after app init
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <tukon479@gmail.com>
  • Loading branch information
Innei committed Oct 12, 2024
1 parent b541b48 commit 728ea84
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
9 changes: 2 additions & 7 deletions apps/main/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ import { initializeSentry } from "./sentry"
import { router } from "./tipc"
import { createMainWindow, getMainWindow } from "./window"

const appFolder = {
prod: "Follow",
dev: "Follow (dev)",
}
if (process.argv.length === 3 && process.argv[2].startsWith("follow-dev:")) {
process.env.NODE_ENV = "development"
}
Expand All @@ -32,7 +28,8 @@ const isDev = process.env.NODE_ENV === "development"
* Mandatory and fast initializers for the app
*/
export function initializeAppStage0() {
app.setPath("appData", path.join(app.getPath("appData"), isDev ? appFolder.dev : appFolder.prod))
if (isDev) app.setPath("appData", path.join(app.getPath("appData"), "Follow (dev)"))
initializeSentry()
}
export const initializeAppStage1 = () => {
if (process.defaultApp) {
Expand All @@ -45,8 +42,6 @@ export const initializeAppStage1 = () => {
app.setAsDefaultProtocolClient(APP_PROTOCOL)
}

initializeSentry()

registerIpcMain(router)

if (app.dock) {
Expand Down
15 changes: 13 additions & 2 deletions apps/main/src/lib/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,26 @@ import { resolve } from "node:path"
import { app } from "electron"
import { JSONFileSyncPreset } from "lowdb/node"

const db = JSONFileSyncPreset(resolve(app.getPath("userData"), "db.json"), {}) as {
let db: {
data: Record<string, unknown>
write: () => void
read: () => void
}

const createOrGetDb = () => {
if (!db) {
db = JSONFileSyncPreset(resolve(app.getPath("userData"), "db.json"), {}) as typeof db
}
return db
}
export const store = {
get: (key: string) => db.data[key] as any,
get: (key: string) => {
const db = createOrGetDb()

return db.data[key] as any
},
set: (key: string, value: any) => {
const db = createOrGetDb()
db.data[key] = value
db.write()
},
Expand Down

0 comments on commit 728ea84

Please sign in to comment.